5

I have a very simple REST API (Spring-Boot) which should accept a list of strings as query parameters:

@ApiOperation(value = "Get the value drivers for a part by its as JSON output")
@GetMapping(AppConstants.VDJSON)
ValueDriver getValueDriverJson(
        @RequestParam(value="pnrList")
        final ArrayList<String> pnrList);

This generates a swagger output like this:

"/vdbptool/api/v1/vdjson": {

"get": {
    "tags": [
        "value-driver-codes-controller"
    ],
    "summary": "Get the value drivers for a part by its PNR in LIST format as JSON output",
    "operationId": "getValueDriverJsonUsingGET",
    "parameters": [
        {
            "name": "pnrList",
            "in": "query",
            "description": "pnrList",
            "required": true,
            "style": "form",
            "explode": true,
            "schema": {
                "type": "string"
            }
        }
    ],.

in swagger ui this looks like

enter image description here

The problem seems to be the type of the parameter, which is "string" instead of "array" - whatever I have tried.

Expected Output: enter image description here

My Gradle:

dependencies {

...

implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation 'io.springfox:springfox-oas:3.0.0'

What am I missing here in order to get the proper parameter type ("array") generated into the OAS 3.0 spec via Springfox ...?

Axel Amthor
  • 10,980
  • 1
  • 25
  • 44

2 Answers2

1

With Spring specifically, you have to use the more generic List<String> for it to interpret your parameter as an array of strings. Technically, there is no difference between List<String> and ArrayList<String>, but Spring chooses to use List<String>.

So your code should be updated to:

@ApiOperation(value = "Get the value drivers for a part by its as JSON output")
@GetMapping(AppConstants.VDJSON)
ValueDriver getValueDriverJson(
        @RequestParam(value="pnrList")
        final List<String> pnrList);

So your Swagger output should then be:

"/vdbptool/api/v1/vdjson": {

"get": {
    "tags": [
        "value-driver-codes-controller"
    ],
    "summary": "Get the value drivers for a part by its PNR in LIST format as JSON output",
    "operationId": "getValueDriverJsonUsingGET",
"parameters": [
        {
            "name": "pnrList",
            "in": "query",
            "description": "pnrList",
            "required": true,
            "style": "form",
            "explode": true,
            "schema": {
                "type": "array",
                "items": {
                    "type": "string"
                }
            }
        }
    ],

djmonki
  • 3,020
  • 7
  • 18
  • 3
    Actually, `List` represents anything that implements `List`, including, but not limited to `ArrayList`. Hence, there are obvious technical differences between the two. If you implement `List` as a new class of your own, then you will be able to use that instead of `ArrayList` as your object. – Lajos Arpad Mar 26 '23 at 09:01
  • Good info @LajosArpad – djmonki Mar 26 '23 at 09:56
  • Already tested with SpringFox: List, ArrayList: same result, Springfox renders a string type, not an array of strings. – Axel Amthor Mar 26 '23 at 12:56
  • @AxelAmthor - Does this help https://github.com/springfox/springfox/issues/3629#issuecomment-1368022213 – djmonki Mar 26 '23 at 19:15
  • @djmonki Thanks, but actually I have tested that as well. It doesn't work, see here: https://stackoverflow.com/questions/66214455/swagger-springfox-how-to-generate-schema-type-array-for-query-param-liststrin?rq=1 – Axel Amthor Mar 27 '23 at 05:20
1

Finally solved the problem. Thanks to this guide: https://springdoc.org/migrating-from-springfox.html I have easily changed from SpringFox to Openapi-ui. Gradle:

    //implementation 'io.springfox:springfox-boot-starter:3.0.0'
    //implementation 'io.springfox:springfox-oas:3.0.0'

    // https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui
    implementation 'org.springdoc:springdoc-openapi-ui:1.6.15'

Output now:

enter image description here

And:

enter image description here

So, I assume that SpringFox is slightly outdated, last changes almost three years ago: enter image description here

Axel Amthor
  • 10,980
  • 1
  • 25
  • 44