Forum Discussion

tobias_wolff's avatar
tobias_wolff
Occasional Visitor
4 years ago

Example composition

Hi,

I have recently discovered the possibility to add examples to components, request bodies and responses - which btw. is a great feature. I got it working to a certain extend by defining examples in the "components/examples" section and reusing them in the request bodies and response in the paths section. I now wonder, if there also is a way to do a composition on examples using $refs. I tried the following:

components:
  examples:
    ProductResponse:
      value:
        id: 1
        name: Test Product One
    ProductListResponse:
      value:
        count: 1
        data:
          - $ref: '#/components/examples/ProductResponse'

But using this in a response as follows:

      responses:
        200:
          description: |
            Returns a (possibly empty) list of products.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ProductListResponse'
              examples:
                ProductListResponse:
                  $ref: '#/components/examples/ProductListResponse'

will render the "Example Value" section in the swagger UI as such:

Can you, please, let me know if I am missing something?

 

Thanks,

   Tobias.


  • I now wonder, if there also is a way to do a composition on examples using $refs.

    Example values do not support $ref. Each example must be a complete literal example.

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    I now wonder, if there also is a way to do a composition on examples using $refs.

    Example values do not support $ref. Each example must be a complete literal example.