cancel
Showing results for 
Search instead for 
Did you mean: 

schema via oneOf with additional common properties

SOLVED
Highlighted
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
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
________________________

 Join SmartBear Connect next April in Boston to meet with software development, API and testing experts from around the world! Get your ticket today!

View solution in original post

2 REPLIES 2
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
________________________

 Join SmartBear Connect next April in Boston to meet with software development, API and testing experts from around the world! Get your ticket today!

View solution in original post

kip
New Contributor

Re: schema via oneOf with additional common properties

Thank you so much, Helen. That worked.