3

I'm manually writing swagger documentation. But I'm getting some error Should be object

I don't know why I'm getting error in below code.

events:
        $ref: "#/def/Events"

Events:
    type: "object"
    properties:
      oneOf:
        - $ref: '#/def/ClassRef'
        - $ref: '#/def/TypeRef'
        - $ref: '#/def/EventRef'
        - $ref: '#/def/MarketRef'
        - $ref: '#/def/OutcomeRef'

  ClassRef:  # <--- I get the "should be object" error here
    type: "object"
    properties:
      classRef:
        type: "string"
        
  TypeRef:
    type: "object"
    properties:
      typeRef:
        type: "string"
        
  EventRef:
    type: "object"
    properties:
      eventRef:
        type: "string"
        
  MarketRef:
    type: "object"
    properties:
      marketRef:
        type: "string"
  
  OutcomeRef:
    type: "object"
    properties:
      outcomeRef:
        type: "string"

I'm getting this error in ClassRef line. Can some one help me please

Tms91
  • 3,456
  • 6
  • 40
  • 74
Neetha123
  • 33
  • 1
  • 1
  • 5

1 Answers1

1

OpenAPI 2.0 (swagger: '2.0') does not support oneOf, you need OpenAPI 3.0 (openapi: 3.0.0) to use oneOf.

Assuming you use OpenAPI 3.0, and all schemas live in the components/schemas section, change the Events schema as follows:

Events:
  oneOf:
    - $ref: '#/components/schemas/ClassRef'
    - $ref: '#/components/schemas/TypeRef'
    - $ref: '#/components/schemas/EventRef'
    - $ref: '#/components/schemas/MarketRef'
    - $ref: '#/components/schemas/OutcomeRef'
Helen
  • 87,344
  • 17
  • 243
  • 314
  • But if we use 2.0, how to resolve this? We are still using Swagger 2.0 – Neetha123 May 05 '20 at 15:42
  • In 2.0 the most you can do is define `Events` as just `type: object`, that is, a free-form object with arbitrary properties. But if you need `oneOf` you need to migrate to OpenAPI 3.0. There are tools to [convert OpenAPI 2.0 to OpenAPI 3.0](https://stackoverflow.com/q/59749513/113116). – Helen May 05 '20 at 19:29
  • Thank you. You have saved my day – Neetha123 May 06 '20 at 14:30