manchenkoff / openapi3-parser

OpenAPI 3 parser to use a specification inside of the code in your projects
https://pypi.org/project/openapi3-parser/
MIT License
65 stars 34 forks source link

[BUG] Incorrect handelling of Null Fields #65

Closed danjones1618 closed 1 year ago

danjones1618 commented 1 year ago

Describe the bug There are two ways of representing a 'Nullable' field in OpenAPI. The first is to mark the field as not required and the second is to set the type to be union-ed with Null. The second method has the implication that the key must be sent with an explicit Null value whereas the first suggests.

When a spec uses AnyOf with string and null, the parser crashes. As null is a unknown data type.

To Reproduce Parse the following spec and see it throw an exception of openapi_parser.errors.ParserError: Invalid schema type 'null'

{
  "openapi": "3.1.0",
  "info": {
    "title": "API",
    "version": "1.0.0"
  },
  "paths": {
    "/business/": {
      "patch": {
        "tags": [
          "business"
        ],
        "summary": "Patch Business",
        "description": "Allows updates of business fileds to the business\nassociated with the authenticated user",
        "operationId": "patch_business_business__patch",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/BusinessPatch"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "string",
                  "title": "Response Patch Business Business  Patch"
                }
              }
            }
          },
          "422": {
            "description": "Validation Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HTTPValidationError"
                }
              }
            }
          }
        },
        "security": [
          {
            "OAuth2PasswordBearer": []
          }
        ]
      }
    }
  },
  "components": {
    "schemas": {
      "BusinessPatch": {
        "properties": {
          "slug": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Slug"
          },
          "name": {
            "anyOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ],
            "title": "Name"
          }
        },
        "type": "object",
        "required": [
          "slug",
          "name"
        ],
        "title": "BusinessPatch"
      },
      "HTTPValidationError": {
        "properties": {
          "detail": {
            "items": {
              "$ref": "#/components/schemas/ValidationError"
            },
            "type": "array",
            "title": "Detail"
          }
        },
        "type": "object",
        "title": "HTTPValidationError"
      },
      "ValidationError": {
        "properties": {
          "loc": {
            "items": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "integer"
                }
              ]
            },
            "type": "array",
            "title": "Location"
          },
          "msg": {
            "type": "string",
            "title": "Message"
          },
          "type": {
            "type": "string",
            "title": "Error Type"
          }
        },
        "type": "object",
        "required": [
          "loc",
          "msg",
          "type"
        ],
        "title": "ValidationError"
      }
    },
    "securitySchemes": {
      "OAuth2PasswordBearer": {
        "type": "oauth2",
        "flows": {
          "password": {
            "scopes": {},
            "tokenUrl": "auth/token"
          }
        }
      }
    }
  }
}

Expected behavior Not to crash.

I've created #64 to fix this issue

System details (please complete the following information):

Additional context Add any other context about the problem here.

manchenkoff commented 1 year ago

Hey @danjones1618, thanks a lot for your contribution! PR has been merged, package v1.1.12 now contains your changes, feel free to update 👍