0

I described my controller using Swagger but when I tried to extract .yaml description of controller, as response of endpoints I found list of objects. How to make Swagger describe those list as list of particular objects such as list of cars, list of houses, list of animals, etc. and then to describe what particular object like car, house or animal is. My case is:

/dummy_endpoint:
get:
  tags:
    - foo-controller
  summary: Get foo list
  description: Send GET request to obtain foo list
  operationId: findAllFooUsingGET
  produces:
     - application/json
  responses:
    '200':
      description: Foo list obtained successfully
      schema:
        type: array
        items:
          type: object
    '401':
      description: Unauthorized
    '403':
      description: Forbidden
    '404':
      description: Not Found

What I want to get:

/dummy_endpoint:
  get:
    tags:
      - foo-controller
    summary: Get foo list
    description: Send GET request to obtain foo list
    operationId: findAllFooUsingGET
    produces:
      - application/json
    responses:
      '200':
        description: Foo list obtained successfully
        schema:
          type: array
          items:
            type: Foo
      '401':
        description: Unauthorized
      '403':
        description: Forbidden
      '404':
        description: Not Found
definitions:
  Foo:
    type: object
    properties:
      id:
        type: integer
        format: int32
      name:
        type: String
2
  • 1
    What version of swagger are you using? Commented Dec 17, 2021 at 12:29
  • 1
    I'm using 2.0 version. Commented Dec 17, 2021 at 12:45

2 Answers 2

1

I assume OpenAPI version 2.0 (based on the syntax in example). If you're using 3.0, let me know.

What you are looking for is ref. Check-in Swagger specification in section Input and Output Models and here in section Array and Multi-Value Parameters about arrays.

For example:

...
responses:
  '200':
    description: Foo list obtained successfully
    schema:
      type: array
      items:
        $ref: "#/definitions/Foo"
...
definitions:
  Foo:
    type: object
    properties:
      ...
Sign up to request clarification or add additional context in comments.

3 Comments

Is there way for automatically adding those ref params, for example through @ApiResponse annotation ?
Are you talking about an API for Java?
It's available, you have value schema in @ApiResponse annotation. Here is example (2nd with output for class User) in section Operation Annotations. For your case schema = @Schema(implementation = Foo.class).
0

Thing that solved my problem was the usage of responseContainer property from @ResponseApi annotation, where I put type of response container like List, Array etc. and putting into response property type of objects that are stored into container.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.