Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discriminator does not switch schema #2438

Open
5thWall opened this issue Oct 3, 2016 · 11 comments · May be fixed by #10224
Open

Discriminator does not switch schema #2438

5thWall opened this issue Oct 3, 2016 · 11 comments · May be fixed by #10224

Comments

@5thWall
Copy link

5thWall commented Oct 3, 2016

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.

@helgridly
Copy link

I'm also seeing this. There's a lot of conflicting information across Swagger repos regarding support for allOf and discriminator 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
Copy link

I see the same in v2.2.6

@ringgelerch
Copy link

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.

@raderio
Copy link

raderio commented 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
Copy link
Contributor

webron commented Apr 3, 2018

@raderio at the moment, it's not in our immediate plans.

@mikeholler
Copy link

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
Copy link

Would a pull request be welcome for this?

@elear
Copy link

elear commented Aug 26, 2021

@pschichtel certainly by me. I'd be happy to test.

@KartikSoneji
Copy link

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?

image

@YousefHaggy
Copy link

YousefHaggy commented Nov 14, 2024

Is this on the roadmap in 2024?

@YousefHaggy
Copy link

Created a PR for this #10224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.