springdoc-openapi how to display an array of strings as a response

7.6k views Asked by At

There are no good examples out on the web of how to get the following output using the springdocs-openapi library (1.5.7). I'm looking to get the following output:

[
  "A", "B", "C"
]

This is the code based on what examples are provided.

@Operation(summary = "")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "OK",
                    content = {@Content(mediaType = "application/json",
                            array = @ArraySchema(schema = @Schema(implementation = String.class)),
                            examples = {@ExampleObject("A"), @ExampleObject("B"), @ExampleObject("C")}
                    )})

This results in the following output

[
  "string"
]

How is the output listed above ["A","B","C"] possible through springdocs-openapi library?

1

There are 1 answers

1
Debargha Roy On BEST ANSWER

You are using the @ExampleObject incorrectly. The value attribute (also the default attribute if you don't specify anything) takes a JSON serialized object of the example payload.

Thus to get ["A", "B"], you don't need multiple @ExampleObject, rather you need one annotation for one example.

Thus updating the code as shown below should be helpful

@Operation(summary = "Some method")
@ApiResponses(value = {
    @ApiResponse(responseCode = "200", description = "OK", content = {
        @Content(
            mediaType = MediaType.APPLICATION_JSON_VALUE,
            array = @ArraySchema(schema = @Schema(implementation = String.class)),
            examples = {
                @ExampleObject("[\"A\", \"B\"]")
            }
        )
    })
})

Shown below is the output of the above code

To specify multiple examples, there should be multiple example objects as shown below

@Operation(summary = "Some method")
@ApiResponses(value = {
    @ApiResponse(responseCode = "200", description = "OK", content = {
        @Content(
            mediaType = MediaType.APPLICATION_JSON_VALUE,
            array = @ArraySchema(schema = @Schema(implementation = String.class)),
            examples = {
                @ExampleObject(name = "Example 1", summary = "Summary 1", description = "Some desc", value = "[\"A\", \"B\"]"),
                @ExampleObject(name = "Example 2", summary = "Summary 2", description = "Some desc", value = "[\"C\", \"D\"]")
            }
        )
    })
})

NOTE: The name attribute of @ExampleObject is used to identify the example internally in the specification file.

"responses": {
  "200": {
    "description": "OK",
    "content": {
      "application/json": {
        "schema": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "examples": {
          "Example 1": {
            "summary": "Summary 1",
            "description": "Some desc",
            "value": [
              "A",
              "B"
            ]
          },
          "Example 2": {
            "summary": "Summary 2",
            "description": "Some desc",
            "value": [
              "C",
              "D"
            ]
          }
        }
      }
    }
  }
}

And the output is as shown below enter image description here