Skip to content

Route params (query or post) with future annotations gives "not fully defined" error #246

Closed
@ashb

Description

@ashb

Minimal repro example:

from __future__ import annotations

from pydantic import BaseModel

from cadwyn import Cadwyn, Version, VersionBundle, VersionedAPIRouter


class MyModel(BaseModel):
    hello: str


router = VersionedAPIRouter()


@router.get("/")
async def root(input: MyModel):
    return {"hello": input.hello}


app = Cadwyn(versions=VersionBundle(Version("2000-01-01")))
app.generate_and_include_versioned_routers(router)

If you run this app and navigate to the docs page (http://127.0.0.1:8000/docs?version=2000-01-01) you get an error:

...
  File "/Users/ash/code/airflow/cadwyn-tmp/.venv/lib/python3.12/site-packages/starlette/routing.py", line 73, in app
    response = await f(request)
               ^^^^^^^^^^^^^^^^
  File "/Users/ash/code/airflow/cadwyn-tmp/.venv/lib/python3.12/site-packages/cadwyn/applications.py", line 303, in openapi_jsons
    get_openapi(
  File "/Users/ash/code/airflow/cadwyn-tmp/.venv/lib/python3.12/site-packages/fastapi/openapi/utils.py", line 493, in get_openapi
    field_mapping, definitions = get_definitions(
                                 ^^^^^^^^^^^^^^^^
  File "/Users/ash/code/airflow/cadwyn-tmp/.venv/lib/python3.12/site-packages/fastapi/_compat.py", line 231, in get_definitions
    field_mapping, definitions = schema_generator.generate_definitions(
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ash/code/airflow/cadwyn-tmp/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 361, in generate_definitions
    self.generate_inner(schema)
  File "/Users/ash/code/airflow/cadwyn-tmp/.venv/lib/python3.12/site-packages/pydantic/json_schema.py", line 441, in generate_inner
    if 'ref' in schema:
       ^^^^^^^^^^^^^^^
  File "<frozen _collections_abc>", line 813, in __contains__
  File "/Users/ash/code/airflow/cadwyn-tmp/.venv/lib/python3.12/site-packages/pydantic/_internal/_mock_val_ser.py", line 41, in __getitem__
    return self._get_built().__getitem__(key)
           ^^^^^^^^^^^^^^^^^
  File "/Users/ash/code/airflow/cadwyn-tmp/.venv/lib/python3.12/site-packages/pydantic/_internal/_mock_val_ser.py", line 58, in _get_built
    raise PydanticUserError(self._error_message, code=self._code)
pydantic.errors.PydanticUserError: `TypeAdapter[typing.Annotated[ForwardRef('MyModel'), Query(PydanticUndefined)]]` is not fully defined; you should define `typing.Annotated[ForwardRef('MyModel'), Query(PydanticUndefined)]` and all referenced types, then call `.rebuild()` on
 the instance.

Removing the future import on line 1 gets rid of the problem.

This only applies if the MyModel is used as an input param to the function, it works fine with future annotations for return values.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions