litestar-org / litestar

Production-ready, Light, Flexible and Extensible ASGI API framework | Effortlessly Build Performant APIs
https://litestar.dev/
MIT License
5.65k stars 384 forks source link

Bug: OpenAPI docs parse pydantic Field() #1541

Closed SanderWegter closed 1 year ago

SanderWegter commented 1 year ago

Description

Referencing a discussion on Discord: https://discord.com/channels/919193495116337154/1093516724864811038

It seems that the pydantic Field() in the model isn't parsed and generating title/description fields in the openapi.json.

Running on docker python:3.11-alpine

URL to code causing the issue

No response

MCVE

from starlite import Starlite, post, Controller
from pydantic import BaseModel, Field
class SomeRequestData(BaseModel):
    hostname: str = Field(title="The hostname", description="Some description")
    interface: str = Field(title="Interface", description="Interface to use")
class SomeController(Controller):
    path = "/somepath"
    @post(
        path="/test", summary="testing", tags=["Test"], description="Test description"
    )
    async def request_example(self, data: SomeRequestData) -> str:
        return "test"
app = Starlite([SomeController])

Steps to reproduce

No response

Screenshots

Current (as in 2.0.0a4): Current

Expected (as in 1.51.9): Expected

Logs

No response

Litestar Version

litestar==2.0.0a4

Platform

chris-telemetry commented 1 year ago

I looked into this a bit since we happened to notice it today as well.

It looks like it applies to both request bodies and responses.

Looking at the source it appears litestart._signature.field.SignatureField doesn't support descriptions. There's an extra attribute but that appears to be used for creating examples.

SanderWegter commented 1 year ago

It looks like it applies to both request bodies and responses.

Correct.

I went down the rabbit hole and tried to figure out why this happens. Looks like construct_open_api_with_schema_class isn't called anymore from app.py:update_openapi_schema when I'm comparing it to 1.51.10.

Simply adding this doesn't work because the OpenAPI class isnt based on pydantic BaseModel anymore but the litestar BaseSchemaObject.

The function construct_open_api_with_schema_class relies on the BaseModel.copy(deep=True) which doesn't exist on BaseSchemaObject.