openapiswagger-codegen

Generating multiple OpenAPI examples when oneOf is being used with discriminator


In my response schema, I have some polymorphism.

Here is my base object.

PersonResourceAbstract:
  type: object
  properties:
    id: 
      description: blah
      type: string
  oneOf:
    - $ref: '#/components/schemas/PersonResourceSummary'
    - $ref: '#/components/schemas/PersonResourceDetail'
  discriminator:
    propertyName: discriminatorView
    mapping:
      summary: '#/components/schemas/PersonResourceSummary'
      detail: '#/components/schemas/PersonResourceDetail' 

The summary and detail just have different levels of detail. I was looking for a way where it was possible to see examples for both when PersonResourceAbstract is indicated as a return object for a request.

Doing this manually is a good bit of work, would love if docs generation just gave me this? like the way it gives you JSON with default values when you only use one object.

Any help appreciated.


Solution

  • One shall use the examples propertie:

      /your-route:
        get:
          requestBody:
            content:
              application/json:
                schema:
                    $ref: '#/components/schemas/PersonResourceAbstractRequest'
          responses:
            '200':
              content:
                application/json:
                  schema:
                    $ref: '#/components/schemas/PersonResourceAbstractResponse'                 
                  examples: # here
                    example1: 
                      summary: example1
                      value:
                        key: value
                    example2: 
                        $ref: '#/components/examples/example2' #you might create it as a component
    

    related answer