How do I specify allowed values for global header parameters in Springfox v3?

Viewed 1780

I try to migrate our swagger ui configuration of springfox from version 2.9.2 to version 3.0.0.

In version 2.9.2 I could do something like this to specify what values where allowed for my global headers:

@Bean
public Docket swaggerDocket() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            [...]
            .build()
            .groupName("Some API")
            .directModelSubstitute(LocalDate.class, java.sql.Date.class)
            .useDefaultResponseMessages(false)
            .globalOperationParameters(
                    Arrays.asList(new ParameterBuilder()
                            .name("Environment")
                            .modelRef(new ModelRef("string"))
                            .allowableValues(new AllowableListValues(List.of("A","B","C"), "string"))
                            .parameterType("header")
                            .required(true)
                            .build()));
}

but in version 3.0.0 "globalOperationParameters" is marked as deprecated. I tried to use the new "globalRequestParameters" instead to get the same result as in the old version:

@Bean
public Docket swaggerDocket(TypeResolver typeResolver) {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            [...]
            .build()
            .groupName("Some API")                
            .directModelSubstitute(LocalDate.class, java.sql.Date.class)
            .useDefaultResponseMessages(false)
            .globalRequestParameters(List.of(createRequestParameter("Environment", true)));
}

private RequestParameter createRequestParameter(String headerName, boolean required) {
    return new RequestParameterBuilder()
            .name(headerName)
            .required(required)
            .query(q -> q.model(modelSpecificationBuilder -> modelSpecificationBuilder.scalarModel(ScalarType.STRING)))
            .in(ParameterType.HEADER)
            .build();
}

Is there a way to add a list of allowed values?

1 Answers

Figured this out after messing around with intelliJ auto-complete. In the query, use the model and then the enumerationFacet methods. I'll keep it quick and easy:

In your createRequestParameter use the Code:

final String bestCookie = "Peanut Butter Chocolate Chunk";
new RequestParameterBuilder()
        .in("path")
        .name("Best Cookies")
        .description("Put your favorite cookie here")
        .query(q ->
            q.model(m -> m.scalarModel(ScalarType.STRING))
                    .defaultValue(bestCookie)
                    .enumerationFacet(e -> e.allowedValues(
                            new AllowableListValues(
                                    List.of(bestCookie, "There's no other best cookies",
                                            "You heard what I said"),
                                    "string")))
        )
        .required(true)
        .build();

I would include images of the drop down, but I don't have the reputation to post them.

Related