Ertuğrul Altınboğa
Ertuğrul Altınboğa

Reputation: 2607

How to make swagger examples component with allOf

components:
 examples:
    J:
      value:
        name: my name
        
    J2:
      value:
        allOf: 
          - $ref: '#/components/examples/J'
          - id: 123

Use J2 in response body examples:

responses:
  200:
    content:
      application/json:
        examples:
          test:
            $ref: '#/components/examples/J2'

Current result:

{
  "$ref": "#/components/examples/J",
  "id": 123
}

Expected result

{
  "name": "my name",
  "id": 123
}

Related: https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/

Swagger schema properties ignored when using $ref - why?

Upvotes: 3

Views: 7725

Answers (3)

Jean-Charles
Jean-Charles

Reputation: 31

Nisha Dave response working with https://editor-next.swagger.io/

snippet code Result

But there is a warning:

Warning message

Upvotes: 0

Nisha Dave
Nisha Dave

Reputation: 789

You cannot combine at example level but you can combine examples at path level.

This is invalid

components:
 examples:
    J:
      value:
        name: my name
        
    J2:
      value:
        allOf: 
          - $ref: '#/components/examples/J'
          - id: 123

But you can do this

components:
 examples:
    J:
      value:
        name: my name
        
    J2:
      value:
        id: 123

content:
  application/json:
    schema:
      $ref: '#/components/schemas/MyObject'
    examples:
      objectExample:
        allOf :
         - $ref: '#/components/examples/J'
         - $ref: '#/components/examples/J2'

Upvotes: 0

Helen
Helen

Reputation: 97590

This is not supported.

The value is meant to be the literal value of the example. As a result, value does not support allOf and $ref. OpenAPI does not have a way to merge the values of multiple example components.

Upvotes: 1

Related Questions