cancel
Showing results for 
Search instead for 
Did you mean: 

schema via oneOf with additional common properties

SOLVED
kip
New Contributor

schema via oneOf with additional common properties

Hey everyone,

 

I am trying to achieve the following, but with the commented lines uncommented. Uncommented it is not syntactically correct, but semantically I am trying to add BodyCommon's properties to the POST body, in addition to either BodyTypeA or BodyTypeB.

 

openapi: "3.0.1"

info:

servers: 

paths:

components:

  requestBodies:

    SomeClientPostBody:
      required: true
      content:
        application/json:
          schema:
            oneOf:
              - $ref: "#/components/schemas/BodyTypeA"
              - $ref: "#/components/schemas/BodyTypeB"
#          properties:
#            $ref: "#/components/schemas/BodyCommon"

  schemas:

    BodyTypeA:
      properties:
        type_a_property:
          type: integer

    BodyTypeB:
      properties:
        type_b_property:
          type: string

    BodyCommon:
      properties:
        common_property:
          type: integer

How can I fix this?

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator

Re: schema via oneOf with additional common properties

You need an "allOf" of BodyCommon and "oneOf":

components:
  schemas:
    SomeClientPostBody:
      required: true
      content:
        application/json:
          schema:
            allOf:
              - oneOf:
                  - $ref: "#/components/schemas/BodyTypeA"
                  - $ref: "#/components/schemas/BodyTypeB"
              - $ref: "#/components/schemas/BodyCommon"

 


Helen Kosova
SmartBear Technical Writer
________________________
Vote up helpful replies.
Accept this reply if you think it's the best solution to your question.
2 REPLIES 2
Highlighted
Moderator

Re: schema via oneOf with additional common properties

You need an "allOf" of BodyCommon and "oneOf":

components:
  schemas:
    SomeClientPostBody:
      required: true
      content:
        application/json:
          schema:
            allOf:
              - oneOf:
                  - $ref: "#/components/schemas/BodyTypeA"
                  - $ref: "#/components/schemas/BodyTypeB"
              - $ref: "#/components/schemas/BodyCommon"

 


Helen Kosova
SmartBear Technical Writer
________________________
Vote up helpful replies.
Accept this reply if you think it's the best solution to your question.
kip
New Contributor

Re: schema via oneOf with additional common properties

Thank you so much, Helen. That worked.

New Here?
Join us and watch the welcome video:
API Testing Mistake #2
APITestingMistake#2
Top Kudoed Authors