Big Smile
Big Smile

Reputation: 1124

How to define Object with multiple properties including an array of objects in OpenAPI?

I am struggling to define a request body in OpenApi. I want to force the user to send me data in the following shape:

{
    "test": {
        "a": "a",
        "b": "b",
        "abc": [
            {
                "type": "abc",
                "name": "name"
            },
            {
                "type": "abc",
                "name": "name"
            }
            ...more
        ]
    }
}

This is what I tried but it does not seem to work:

 schemas:
    SampleRequest:
      required:
        - abc
      properties:
        abc: 
          $ref: "#/components/schemas/ABCType"

    ABCType:
      type: object
      required:
        - test
      properties: 
        test: 
          type: array 
          items:
            type: object
        properties: 
          type: 
            type: string
          name:
            type: string

Any help appreciated. Thank you

Upvotes: 0

Views: 1420

Answers (1)

BenceL
BenceL

Reputation: 844

With the currect json I couldn't make such sense but created this. This created a json which replicates yours.

ABCType:
  type: object
  additionalProperties: false
  required: 
  - a
  - b
  - abc
  properties:
    a:
      description: A of ABCType
      type: string
    b:
      description: B of ABCType
      type: string
    abc:
      description: Array of ABCType
      type: array
      items:
        $ref: '#/components/schemas/ABCArrayValue'
ABCArrayValue:
  type: object
  additionalProperties: false
  required:
  - type
  - name
  properties:
    type:
      description: Type of this array value
      type: string
    name:
      description: Name of this array value
      type: string

Upvotes: 2

Related Questions