swagger-api / swagger-api/swagger-ui

Discriminator does not switch schema

Open
#2,438 11 comments 33 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

P2 type: feature version: 3.x
Dominant language
JavaScript
Stars
29k
Forks
9.3k
Avg merge
2d 23h
Merged PRs (30d)
25

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior in Swagger UI v2.2.3 with the supplied Swagger document, then inspect the GET and POST model and parameter rendering for the Animal discriminator. Done means GET indicates Cat and Dog-specific fields and POST updates its inputs when animalType changes between Cat and Dog.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.