Ishara M
Ishara M

Reputation: 287

Swagger UI OpenAPI 3, query parameters displayed as an object instead of value-changeable fields

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?

Upvotes: 3

Views: 8368

Answers (1)

Ishara M
Ishara M

Reputation: 287

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) {

}

Or, alternatively, annotate the param class:

@ParameterObject
public class 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.

Upvotes: 7

Related Questions