Open
Description
Using Swagger UI v2.2.3
Swagger file:
{
"swagger": "2.0",
"info": {
"title": "Animals yo!",
"description": "An API for animals",
"version": "1.0.0"
},
"produces": [
"application/json"
],
"paths": {
"/animal": {
"get": {
"summary": "Retrieves an animal",
"responses": {
"200": {
"description": "Animal",
"schema": {
"$ref": "#/definitions/Animal"
}
}
}
},
"post": {
"summary": "Animal",
"parameters": [
{
"in": "body",
"name": "animal",
"required": true,
"schema": {
"$ref": "#/definitions/Animal"
}
}
],
"responses": {
"200": {
"description": "Animal",
"schema": {
"$ref": "#/definitions/Animal"
}
}
}
}
}
},
"definitions": {
"Animal": {
"additionalProperties": true,
"discriminator": "animalType",
"required": [
"name",
"animalType"
],
"properties": {
"name": {
"type": "string"
},
"animalType": {
"type": "string",
"enum": [
"Cat",
"Dog"
]
}
}
},
"Cat": {
"allOf": [
{
"$ref": "#/definitions/Animal"
}
],
"required": [
"huntingSkill"
],
"properties": {
"huntingSkill": {
"type": "string",
"description": "Skill of hunting",
"default": "lazy",
"enum": [
"lazy",
"clueless",
"adventurous",
"aggressive"
]
}
}
},
"Dog": {
"allOf": [
{
"$ref": "#/definitions/Animal"
}
],
"required": [
"packSize"
],
"properties": {
"packSize": {
"type": "integer",
"format": "int32",
"description": "Size of the pack",
"default": 0,
"minimum": 0
}
}
}
}
}
What I expect to see:
Under GET /animals
some indication that a Dog
or Cat
may actually be returned with corresponding huntingSkill
or packSize
parameters.
Under POST /animals
parameters section a text box corresponding to huntingSkill
when animalType
dropdown is set to Cat
, and a packSize
text box when animalType
is set to Dog
What I actually see:
Basic Animal
represented for both GET
and POST
.
If I change the schema $ref
in GET
or POST
to one of the options then the model will show all fields for an Animal
plus the specific type of animal. However the animalType
parameter can still be changed to the other type.
Activity
helgridly commentedon Oct 27, 2016
I'm also seeing this. There's a lot of conflicting information across Swagger repos regarding support for
allOf
anddiscriminator
but as an end-user the above snippet is a succinct demonstration of how it doesn't work. I can confirm it still doesn't work in v2.2.5.chriswebster commentedon Nov 21, 2016
I see the same in v2.2.6
ringgelerch commentedon Aug 10, 2017
This is a very important feature for us as well. We use a workaround at the moment:
For the GET method we define multiple responses like "200 Cat" and "200 Dog".
For PUT and POST we use different paths like "path/to/animals/animal (Cat)". To use the "try out" feature of swagger ui we created a PUT and POST with the valid path "path/to/animals/animal". To send a request just copy the body content from the Cat PUT or POST to the generic one.
It would be greate to see the discriminator be supported very soon, because with upper workaround our api spec is hard to maintain and the rendered ui is not easy to read.
oneOf
feature supported? xerions/phoenix_swagger#136raderio commentedon Apr 3, 2018
An ETA when it will be supported?
an example how it works in ReDoc
https://github.com/Rebilly/ReDoc/blob/master/docs/images/discriminator-demo.gif
From https://github.com/Rebilly/ReDoc
webron commentedon Apr 3, 2018
@raderio at the moment, it's not in our immediate plans.
mikeholler commentedon Aug 13, 2019
It's pretty surprising to me that this isn't supported out of the box (or being prioritized). The way it shows just the "parent" and doesn't give anybody an idea of the other types available in any way is... not great, to put it lightly.
It's going to be very unintuitive for users that want to try our routes that use polymorphic inputs and outputs.
pschichtel commentedon Jul 16, 2021
Would a pull request be welcome for this?
elear commentedon Aug 26, 2021
@pschichtel certainly by me. I'd be happy to test.
KartikSoneji commentedon Oct 8, 2022
Hi, I looked at swagger-ui with the posted spec but I can't see a good place to add a dropdown for
animalType
for the POST request.Also, I'm not sure how the optional fields should appear in the GET schema, maybe instead of the Animal schema there should be two different schemas for Dog and Cat?
YousefHaggy commentedon Nov 14, 2024
Is this on the roadmap in 2024?
YousefHaggy commentedon Nov 25, 2024
Created a PR for this #10224