sn0rk
sn0rk

Reputation: 113

How can I override examples in openapi?

I have the response structure:

responses:
  '200':
    description: OK
    content:
      application/json:
        schema:
          type: object
          properties:
            data:
              allOf:
              - { $ref: ../../components/schemas/Product.yaml }
              example:
                active: false

And the Product.yaml one:

type: object
description: Product
properties:
  id:
    description: ID
    type: integer
    example: 1
  name:
    description: Name
    type: string
    example: 'fresh baguette'
  active:
    description: Is active
    type: boolean
    example: true

So I wanna override active example and when I do it like this:

data:
  allOf:
  - { $ref: ../../components/schemas/Product.yaml }
  example:
    active: false

In redoc ui under the "Response samples" I see the only active property.

My question is how can I override an example of only active property without overriding each of product properties?

Upvotes: 6

Views: 7632

Answers (2)

Rohan Pillai
Rohan Pillai

Reputation: 967

There is a hack to override individual properties.

responses:
  '200':
    description: OK
    content:
      application/json:
        schema:
          allOf:
            - $ref: ../../components/schemas/Product.yaml
            - type: object
              properties:
                 active: 
                   example: false

If you have more properties to override, just keep adding to the object.

- type: object
  properties:
    name:
      example: "fresh croissant"
    active: 
      example: false

However, there is a problem with this method. If active was an array and you had defined it as [ "true" ] in Product.yaml, and you try to override it as an empty array in the response, it won't do that. It'll display it as [ "true" ].

Upvotes: 4

Helen
Helen

Reputation: 97590

There's no way to override an individual property value in an example. You need to provide the whole example:

responses:
  '200':
    description: OK
    content:
      application/json:
        schema:
          type: object
          properties:
            data:
              $ref: ../../components/schemas/Product.yaml

          example:
            data:
              id: 1
              name: fresh baguette
              active: false

Upvotes: 6

Related Questions