I need to add multiple examples for the @Schema in springdoc-openapi

Viewed 22

I need to add multiple examples for the @Schema in springdoc-openapi.

I know that we can use multiple examples using @ExampleObject like this:

@PostMapping("/")
    public Sample createSample(@Parameter(description="Sample description", examples = {
            @ExampleObject(name="foo", description = "na",summary = "na",value = "{\n" +
                    "  \"id\": 10,\n" +
                    "  \"name\": \"ashith\",\n" +
                    "  \"description\": \"none\"\n" +
                    "}"),
            @ExampleObject(name="bar",description = "na",summary = "na",value = "{\n" +
                    "  \"id\": 20,\n" +
                    "  \"name\": \"Akshatha\",\n" +
                    "  \"description\": \"ok\"\n" +
                    "}")
    }
    )
    @RequestBody Sample sample) {

And I know that the @Schema has example property that can be used like this:

@Schema(description = "Sample information")
public class Sample{
    @Schema(example = "2")
    private String id;
    @Schema(example = "foo" )
    private String name;

But I'd like to write these examples in the model class. Why?

  • Because that way we keep the code cleaner, and
  • The examples will always be up to date.

Is it possible? Does anyone have managed to do this?

0 Answers
Related