Skip to content

Is oneOf supported inside #/components/schemas section? #1758

Closed
@mindstretch23

Description

@mindstretch23

I tried to answer this by reading this entire thread at #333, but still not 100% sure.

I know the oneOf keyword can be used inside the requestBody object to reference alternative schemas. However, can someone confirm if oneOf is supported within the #/components/schemas section as well?

For example:

components:
  schemas:
    ID:
      oneOf:
      - $ref: '#/components/schemas/ID1'
      - $ref: '#/components/schemas/ID2'

👆 doesn't throw an error in SwaggerHub editor, but it does not populate the interactive documentation (i.e. the ID object is missing in the interactive documentation)

I also tried this, which throws the error "bad indentation of a mapping entry"in the SwaggerHub editor:

components:
  schemas:
    ID:
      type: object
      oneOf:
      - $ref: '#/components/schemas/ID1'
      - $ref: '#/components/schemas/ID2'

I would like to use oneOf inside a schema that is referenced by a request body (as shown above). This creates much less repetition in my case because the two alternatives schemas that could be referenced in the request body with oneOf only differ by one property. One schema needs ID1 and the other needs ID2. There's about 7 other properties that are common across the two schemas, so if I use the oneOf keyword in the requestBody to reference alternative scheams (the way I know it works), then I'm repeating those 7 common properties.

Can someone help confirm if I can instead use oneOf inside #/components/schemas to avoid this repetition?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions