Skip to content

Discriminator does not switch schema #2438

Open
@5thWall

Description

@5thWall

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

helgridly commented on Oct 27, 2016

@helgridly

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

chriswebster commented on Nov 21, 2016

@chriswebster

I see the same in v2.2.6

ringgelerch

ringgelerch commented on Aug 10, 2017

@ringgelerch

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

raderio commented on Apr 3, 2018

@raderio
webron

webron commented on Apr 3, 2018

@webron
Contributor

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

mikeholler

mikeholler commented on Aug 13, 2019

@mikeholler

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

pschichtel commented on Jul 16, 2021

@pschichtel

Would a pull request be welcome for this?

elear

elear commented on Aug 26, 2021

@elear

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

KartikSoneji

KartikSoneji commented on Oct 8, 2022

@KartikSoneji

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

YousefHaggy commented on Nov 14, 2024

@YousefHaggy

Is this on the roadmap in 2024?

YousefHaggy

YousefHaggy commented on Nov 25, 2024

@YousefHaggy

Created a PR for this #10224

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @webron@5thWall@pschichtel@helgridly@elear

      Issue actions

        Discriminator does not switch schema · Issue #2438 · swagger-api/swagger-ui