1

Previously in SpringBoot v2.5.7, I had this Spring REST controller method. It had a TestCriteria type DTO, as the path param.

@GetMapping(path = "/test")
public void test(TestCriteria testCriteria) {

}

And the TestCriteria class was like this. (Language is an enum that can take either EN or FR).

public class TestCriteria {
    @ApiModelProperty(allowEmptyValue = true)
    List<Language> langauges;
    
}

I used Springfox Swagger (springfox-boot-starter v3) and the Swagger UI looked like this:

But later I had to upgrade SpringBoot to v3, and use Springdoc and OpenAPI v3 for Swagger. Now the TestCriteria class looks like this:

public class TestCriteria {

    @Schema(type="array")
    @Parameter(allowEmptyValue = true)
    List<Langauge> languages;

}

Now Swagger UI does not display languages as a value-selectable field, but as an object. enter image description here

I compared the generated OpenAPI definition as well and found,

previous API docs -

  parameters:
    - name: languages
      in: query
      required: false
      type: array
      items:
        type: string
        enum:
          - EN
          - FR
      collectionFormat: multi
      enum:
        - EN
        - FR

new API doc -

parameters:
  - name: testCriteria
    in: query
    required: true
    schema:
      $ref: '#/components/schemas/TestCriteria'

Is there a way I can provide the previous Swagger UI view, where the user can select a value from the enum list, rather than getting user's input via an object representation?

Helen
  • 87,344
  • 17
  • 243
  • 314
code99
  • 227
  • 4
  • 11

1 Answers1

4

Finally got the Swagger UI displayed as expected.

In the controller method argument, we need to add the @ParameterObject annotation:

@GetMapping(path = "/test")
public void test(@ParameterObject TestCriteria testCriteria) {

}

This is explained in the Springfox to Springdoc migration doc:

If you’re using an object to capture multiple request query params, annotate that method argument with @ParameterObject.

Helen
  • 87,344
  • 17
  • 243
  • 314
code99
  • 227
  • 4
  • 11