Skip to content

Bug: Incorrect analysis of annotations in pydantic #4501

@JCHacking

Description

@JCHacking

Description

It seems that whether a field is required or not is analyzed incorrectly when it has annotations in a union.

This is because, for example, the use of annotation within a union is not contemplated here.
https://github.com/litestar-org/litestar/blob/main/litestar/typing.py#L296

URL to code causing the issue

No response

MCVE

from typing import Annotated

from litestar import Litestar, post
from litestar.openapi import OpenAPIConfig
from litestar.openapi.plugins import RedocRenderPlugin
from litestar.plugins.pydantic import PydanticPlugin
from pydantic import BaseModel, Field


class Object_(BaseModel):
    password: Annotated[Annotated[str, "TEST"] | Annotated[None, "TEST2"], Field(default=None)]

@post(
    path="/",
    sync_to_thread=False,
)
def test(data: Object_) -> None:
    return None


app = Litestar(
    route_handlers=[test],
    plugins=[PydanticPlugin(prefer_alias=True)],
    openapi_config=OpenAPIConfig(
        title="TEST",
        summary="TEST",
        version="latest",
        path="/docs",
        render_plugins=[
            RedocRenderPlugin(
                version="latest", js_url="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"
            )
        ],
    ),
    debug=True,
)

Steps to reproduce

  1. Execute uvicorn with app
  2. See that in the openapi, the password field is set as required.
{
  "info": {
    "title": "TEST",
    "version": "latest",
    "summary": "TEST"
  },
  "openapi": "3.1.0",
  "servers": [
    {
      "url": "/"
    }
  ],
  "paths": {
    "/": {
      "post": {
        "summary": "Test",
        "operationId": "Test",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Object_"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "Document created, URL follows",
            "headers": {

            }
          },
          "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": {
      "Object_": {
        "properties": {
          "password": {
            "oneOf": [
              {
                "type": "string"
              },
              {
                "type": "null"
              }
            ]
          }
        },
        "type": "object",
        "required": [
          "password"
        ],
        "title": "Object_"
      }
    }
  }
}

Screenshots

No response

Logs


Litestar Version

2.18.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🐛This is something that is not working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions