Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mindstretch23 opened this issue Nov 24, 2018 · 7 comments
Closed

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

mindstretch23 opened this issue Nov 24, 2018 · 7 comments

Comments

@mindstretch23
Copy link

mindstretch23 commented Nov 24, 2018

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?

@MikeRalphson
Copy link
Member

oneOf can be used inside any schema object, including those under components/schemas.

@mindstretch23
Copy link
Author

mindstretch23 commented Nov 24, 2018

@MikeRalphson Thanks.

Which syntax from my original comment is correct? Both seem to have issues.

  1. When using the 1st one, SwaggerHub editor doesn't throw an error, but it does not populate the interactive documentation (i.e. the ID object is missing in the interactive documentation). Is this as-expected?

  2. When using the 2nd one, the editor throws the error "bad indentation of a mapping entry"in the SwaggerHub editor

@mindstretch23 mindstretch23 changed the title Is oneOf supported inside #/components/schemas secton? Is oneOf supported inside #/components/schemas section? Nov 24, 2018
@mindstretch23
Copy link
Author

mindstretch23 commented Nov 24, 2018

I think what I'm actually seeing is automatic examples are not populating with oneOf

I think thats a known limitation as described here, https://app.swaggerhub.com/help/openapi-3-support

Does that make sense? If so I can close this out

@MikeRalphson
Copy link
Member

Yes, I think that's the case. BTW both of your examples are technically fine.

@mindstretch23
Copy link
Author

@MikeRalphson Thanks. I wonder why the 2nd one throws an error in the editor then = /

@MikeRalphson
Copy link
Member

I suspect the editor might be doing some preprocessing on the yaml, but that's just a guess, and heading off topic for this repo.

@mindstretch23
Copy link
Author

Got it. Thanks for your help @MikeRalphson. Closing this out now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants