swagger-api / swagger-api/swagger-ui

Order of "null" in anyOf/oneOf array change the behaivor of produced docs

Open
#10,225 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

P2 pull-request-welcome
Dominant language
JavaScript
Stars
29k
Forks
9.3k
Avg merge
2d 23h
Merged PRs (30d)
25

Description

Q&A
  • OS: Any
  • Browser: Any
  • Method of installation: dist assets
  • Swagger-UI version: 3.18.2
  • Swagger/OpenAPI version: OpenAPI 3.1
Content & configuration

Order of "null" in "anyOf"/"oneOf" array change displaing info in SwaggerUI

This OAS (where null is the first element)

{
  "info": {
    "title": "Test API",
    "version": "0.0.1"
  },
  "openapi": "3.1.0",
  "servers": [
    {
      "url": "/"
    }
  ],
  "paths": {
    "/": {
      "get": {
        "summary": "Test",
        "operationId": "Test",
        "parameters": [
          {
            "name": "environment",
            "in": "query",
            "schema": {
              "anyOf": [
                {
                  "type": "null"
                },
                {
                  "$ref": "#/components/schemas/Environment"
                }
              ],
              "title": "Environment",
              "description": "The environment to filter by."
            },
            "description": "The environment to filter by.",
            "required": false,
            "deprecated": false,
            "allowEmptyValue": false,
            "allowReserved": false
          }
        ],
        "responses": {
          "200": {
            "description": "Request fulfilled, document follows",
            "headers": {
              
            },
            "content": {
              "application/json": {
                "schema": {
                  "anyOf": [
                    {
                      "$ref": "#/components/schemas/Environment"
                    },
                    {
                      "type": "null"
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Bad request syntax or unsupported method",
            "content": {
              "application/json": {
                "schema": {
                  "properties": {
                    "status_code": {
                      "type": "integer"
                    },
                    "detail": {
                      "type": "string"
                    },
                    "extra": {
                      "additionalProperties": {
                        
                      },
                      "type": [
                        "null",
                        "object",
                        "array"
                      ]
                    }
                  },
                  "type": "object",
                  "required": [
                    "detail",
                    "status_code"
                  ],
                  "description": "Validation Exception",
                  "examples": [
                    {
                      "status_code": 400,
                      "detail": "Bad Request",
                      "extra": {
                        
                      }
                    }
                  ]
                }
              }
            }
          }
        },
        "deprecated": false
      }
    }
  },
  "components": {
    "schemas": {
      "Environment": {
        "type": "string",
        "enum": [
          "dev",
          "test",
          "qa"
        ],
        "title": "Environment"
      }
    }
  }
}

Produce this generated UI:
image

This OAS (where null is the last):

{
  "info": {
    "title": "Test API",
    "version": "0.0.1"
  },
  "openapi": "3.1.0",
  "servers": [
    {
      "url": "/"
    }
  ],
  "paths": {
    "/": {
      "get": {
        "summary": "Test",
        "operationId": "Test",
        "parameters": [
          {
            "name": "environment",
            "in": "query",
            "schema": {
              "anyOf": [
                {
                  "$ref": "#/components/schemas/Environment"
                },
                {
                  "type": "null"
                }
              ],
              "title": "Environment",
              "description": "The environment to filter by."
            },
            "description": "The environment to filter by.",
            "required": false,
            "deprecated": false,
            "allowEmptyValue": false,
            "allowReserved": false
          }
        ],
        "responses": {
          "200": {
            "description": "Request fulfilled, document follows",
            "headers": {
              
            },
            "content": {
              "application/json": {
                "schema": {
                  "anyOf": [
                    {
                      "$ref": "#/components/schemas/Environment"
                    },
                    {
                      "type": "null"
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "description": "Bad request syntax or unsupported method",
            "content": {
              "application/json": {
                "schema": {
                  "properties": {
                    "status_code": {
                      "type": "integer"
                    },
                    "detail": {
                      "type": "string"
                    },
                    "extra": {
                      "additionalProperties": {
                        
                      },
                      "type": [
                        "null",
                        "object",
                        "array"
                      ]
                    }
                  },
                  "type": "object",
                  "required": [
                    "detail",
                    "status_code"
                  ],
                  "description": "Validation Exception",
                  "examples": [
                    {
                      "status_code": 400,
                      "detail": "Bad Request",
                      "extra": {
                        
                      }
                    }
                  ]
                }
              }
            }
          }
        },
        "deprecated": false
      }
    }
  },
  "components": {
    "schemas": {
      "Environment": {
        "type": "string",
        "enum": [
          "dev",
          "test",
          "qa"
        ],
        "title": "Environment"
      }
    }
  }
}

Produce this:
image

Swagger-UI configuration options:

SwaggerUI(
 {
    url: 'XXX'
    dom_id: '#swagger-container',
    deepLinking: true,
    showExtensions: true,
    showCommonExtensions: true,
    presets: [
        SwaggerUIBundle.presets.apis,
        SwaggerUIBundle.SwaggerUIStandalonePreset
    ],
 }
)

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 report in Swagger UI using the supplied OpenAPI 3.1 document, changing only the order of the anyOf/oneOf entries. Trace the schema rendering path for the parameter and response, then verify that equivalent schemas produce the same generated UI regardless of whether null comes first or last.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.