Gobliins
Gobliins

Reputation: 4026

Swagger UI displaying array example as null

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?

Upvotes: 1

Views: 4770

Answers (2)

Elad Rubi
Elad Rubi

Reputation: 643

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 
    ]

Upvotes: 0

Helen
Helen

Reputation: 97677

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

Upvotes: 1

Related Questions