Discussions

Ask a Question
Back to All

Polymorphism support with allOf

Hello, I'm trying to use oneOf and allOf in my specification to support polymorphism in readme. Experimenting using example in here, my OAS looks like this:

{
  "openapi": "3.1.0",
  "info": {
    "title": "Polymorphism support",
    "version": "1.0.0"
  },
  "paths": {
    "/pets": {
      "patch": {
        "summary": "oneOf request with a nested allOf",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "oneOf": [
                  {
                    "$ref": "#/components/schemas/Cat"
                  },
                  {
                    "$ref": "#/components/schemas/Dog"
                  }
                ],
                "discriminator": {
                  "propertyName": "pet_type"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "default response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Pet"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Pet": {
        "type": "object",
        "required": ["pet_type"],
        "properties": {
          "pet_type": {
            "type": "string"
          }
        },
        "discriminator": {
          "propertyName": "pet_type"
        },
        "oneOf": [
          {
            "$ref": "#/components/schemas/Cat"
          },
          {
            "$ref": "#/components/schemas/Dog"
          }
        ]
      },
      "Dog": {
        "title": "Woof",
        "allOf": [
          {
            "$ref": "#/components/schemas/Pet"
          },
          {
            "type": "object",
            "properties": {
              "bark": {
                "type": "boolean"
              },
              "breed": {
                "type": "string",
                "enum": ["Dingo", "Husky", "Retriever", "Shepherd"]
              }
            }
          }
        ]
      },
      "Cat": {
        "title": "Meow",
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/Pet"
          },
          {
            "type": "object",
            "properties": {
              "hunts": {
                "type": "boolean"
              },
              "age": {
                "type": "integer"
              }
            }
          }
        ]
      }
    }
  }
}

This generates an UI that looks like

Notice that request and response are both empty despite the specification defining them. But, when I remove allOf clauses in Dog and Cat like below, with no other modifications, it correctly displays both options.

   "Dog":{
      "title":"Woof",
      "type":"object",
      "properties":{
         "bark":{
            "type":"boolean"
         },
         "breed":{
            "type":"string",
            "enum":[
               "Dingo",
               "Husky",
               "Retriever",
               "Shepherd"
            ]
         }
      }
   },
   "Cat":{
      "title":"Meow",
      "type":"object",
      "properties":{
         "hunts":{
            "type":"boolean"
         },
         "age":{
            "type":"integer"
         }
      }
   }
Generated by OAS that removed allOf from Cat and Dog

Generated by OAS that removed allOf from Cat and Dog

What may be my problem? Is allOf not supported? But the example used it successfully.

Thanks!