Bradley
Bradley

Reputation: 2141

Using Swagger 2.0, How Can I Show a model that is an array that accepts multiple types?

I'm using app.swaggerhub.com to generate a swagger 2.0 spec.

My code looks like this:

036  definitions:

---    // code that defines 'fieldRequirement' and 'slideRequirement'

106    requirement:
107      type: array
108      items:
109        oneOf:
110          - $ref: '#/definitions/fieldRequirement'
111          - $ref: '#/definitions/slideRequirement'

This returns 3 errors on the same line (106):

106 should NOT have additional properties additionalProperty: oneOf
106 should be array
106 should match some schema in anyOf

What I'm trying to accomplish is a model that allows either a fieldRequirement or a slideRequirement as an array element in requirement.

Upvotes: 0

Views: 370

Answers (1)

Nelson G.
Nelson G.

Reputation: 5441

oneOf is not supported by swagger 2.0, only by swagger 3.0.

However, you can use polymorphism with field discriminator if you can't upgrade to swagger 3.0, in your case, you need a file like :

requirement:
  type: array
  items:
    - $ref: '#/definitions/Requirement'

...

definitions:
  Requirement:
    type: object
    discriminator: requirementType
    properties:
      requirementType:
        type: string
    required:
    - requirementType
  FieldRequirement:
    allOf:
    - $ref: '#/definitions/Requirement'
    - type: object
      properties:
        ...
  SlideRequirement:
    allOf:
    - $ref: '#/definitions/Requirement'
    - type: object
      properties:
        ...

Upvotes: 1

Related Questions