Skip to content

Commit

Permalink
fix(OpenAPI): Correctly handle Annotated NewType (#3615)
Browse files Browse the repository at this point in the history
  • Loading branch information
sherbang authored Jul 8, 2024
1 parent 3a6a293 commit 86e2912
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion litestar/_openapi/schema_generation/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def for_field_definition(self, field_definition: FieldDefinition) -> Schema | Re
def for_new_type(self, field_definition: FieldDefinition) -> Schema | Reference:
return self.for_field_definition(
FieldDefinition.from_kwarg(
annotation=unwrap_new_type(field_definition.raw),
annotation=unwrap_new_type(field_definition.annotation),
name=field_definition.name,
default=field_definition.default,
)
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_openapi/test_parameters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dataclasses
from typing import TYPE_CHECKING, Any, List, Optional, Type, cast
from uuid import UUID

Expand Down Expand Up @@ -418,3 +419,22 @@ async def handler(

app = Litestar([handler])
assert app.openapi_schema.paths["/"].get.parameters[0].schema.type == OpenAPIType.STRING # type: ignore[index, union-attr]


def test_unwrap_annotated_new_type() -> None:
FancyString = NewType("FancyString", str)

@dataclasses.dataclass
class TestModel:
param: Annotated[FancyString, "foo"]

@get("/")
async def handler(
param: TestModel,
) -> None:
return None

app = Litestar([handler])

testmodel_schema_name = app.openapi_schema.paths["/"].get.parameters[0].schema.value # type: ignore[index, union-attr]
assert app.openapi_schema.components.schemas[testmodel_schema_name].properties["param"].type == OpenAPIType.STRING # type: ignore[index, union-attr]

0 comments on commit 86e2912

Please sign in to comment.