Skip to content

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

Open
@wallseat

Description

@wallseat

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
    ],
 }
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2pull-request-welcomeIf a PR were to be made, we would help get it merged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions