2

I am trying to add swagger documentation to my project. I need to add multiple examples for the @Parameter in @RequestBody for the Sample.class, The following piece of code is how its mentioned to be written in the docs.

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


The yaml output being generated is the following:

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
  - url: 'http://localhost:8080/'
    description: Generated server url
paths:
  /api/another/:
    post:
      tags:
        - another-controller
      operationId: createSample
      requestBody:
        description: Sample description
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Sample'
        required: true
      responses:
        '200':
          description: default response
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/Sample'
        '400':
          description: default response
          content:
            '*/*':
              schema:
                type: string
        '404':
          description: default response
          content:
            '*/*':
              schema:
                type: string

This seems to be missing the examples that should have been added as per the code

AshithR
  • 98
  • 1
  • 9

1 Answers1

3

@Parameter uses ParameterIn, which specifies if it is header or query or path as provided by openapi (refer below).

package io.swagger.v3.oas.annotations.enums;

public enum ParameterIn {
    DEFAULT(""),
    HEADER("header"),
    QUERY("query"),
    PATH("path"),
    COOKIE("cookie");

    private String value;

    private ParameterIn(String value) {
        this.value = value;
    }

    public String toString() {
        return String.valueOf(this.value);
    }
}

Instead you should use RequestBody from openapi library, as below

@io.swagger.v3.oas.annotations.parameters.RequestBody(content = {
        @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
            @ExampleObject(value = "{\"key1\": \"example1\", \"key2\":\"example1\", \"key3\":\"example1\"}"),
            @ExampleObject(value = "{\"key1\": \"example2\", \"key2\":\"example2\", \"key3\":\"example2\"}")
            })
    })

You can add this either at method or parameter level.

Abhinav manthri
  • 151
  • 1
  • 14