Forum Discussion

kip's avatar
kip
New Contributor
6 years ago
Solved

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?

  • 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"

     

2 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    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"

     

    • kip's avatar
      kip
      New Contributor

      Thank you so much, Helen. That worked.