1

I use OpenAPI 3.0.0 and want to pass an array of Items as a parameter in the requestBody. My API definition looks like this:

post:
  tags:
    - test
  summary: Test dummy
  operationId: requestBodyTests
  requestBody:
    description: test the body
    required: true
    content:
      application/json:
        schema:
          $ref: '#/components/schemas/Items'


components:
  schemas:
  Items:
    type: array
    items:
      $ref: '#/components/schemas/Item'
    examples:
      - id: bla
        text: blubb
    - id: bla
      text: blubb

  Item:
    type: object
    properties:
      id:
        type: string
      name:
        type: string

Swagger UI displays the request body example as follows: null?

and the request body schema as follows:

orderedmap wtf?

Why does it show an orderedmap instead of my normal objects?

Can someone tell me how to do the spec right for having the array with items correct?

2 Answers 2

1

Examples inside schemas use the example keyword (singular), not examples (plural).

Also, your YAML indentation is wrong - Items and Item must be indented under schemas, list items in the example must have the same indent, etc. If you paste your spec into http://editor.swagger.io, it will point out syntax errors.

Here's the fixed version:

components:
  schemas:
    Items:
      type: array
      items:
        $ref: '#/components/schemas/Item'
      example:   # <------
        - id: bla
          text: blubb
        - id: bla
          text: blubb

    Item:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
Sign up to request clarification or add additional context in comments.

Comments

0

Moreover, you get an example as 'orderedmap' because the example field is A free-form property. But represent examples that cannot be naturally represented in JSON or YAML, a string value can be used to contain the example with escaping where necessary. (OpenAPI spec)

We can write an example as 'string' in both ways:

1.

example: '[ currency: USD, amount: 123 ]'
  example: |
    [ 
      currency: USD, 
      amount: 123 
    ]

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.