beanie: [BUG] pydantic.errors.PydanticInvalidForJsonSchema occured when APIRouter's response_model has PydanticObjectId Field
Describe the bug pydantic.errors.PydanticInvalidForJsonSchema occured when APIRouter’s response_model has PydanticObjectId Field It occurs to beanie 1.28
To Reproduce
@sample_router.post("$", response_model=PydanticObjectId)
async def create_sample():
sample = await self.sample_service.create()
return sample.id
Stack trace
Traceback (most recent call last):
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 401, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
self.scope, self.receive, self.send
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 70, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
await super().__call__(scope, receive, send)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\applications.py", line 123, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
raise exc
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
await self.app(scope, receive, _send)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\middleware\cors.py", line 85, in __call__
await self.app(scope, receive, send)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\middleware\exceptions.py", line 65, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
raise exc
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
await app(scope, receive, sender)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\routing.py", line 756, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\routing.py", line 776, in app
await route.handle(scope, receive, send)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\routing.py", line 297, in handle
await self.app(scope, receive, send)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\routing.py", line 77, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\_exception_handler.py", line 64, in wrapped_app
raise exc
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\_exception_handler.py", line 53, in wrapped_app
await app(scope, receive, sender)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\starlette\routing.py", line 72, in app
response = await func(request)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\fastapi\applications.py", line 1009, in openapi
return JSONResponse(self.openapi())
~~~~~~~~~~~~^^
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\fastapi\applications.py", line 981, in openapi
self.openapi_schema = get_openapi(
~~~~~~~~~~~^
title=self.title,
^^^^^^^^^^^^^^^^^
...<11 lines>...
separate_input_output_schemas=self.separate_input_output_schemas,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\fastapi\openapi\utils.py", line 475, in get_openapi
field_mapping, definitions = get_definitions(
~~~~~~~~~~~~~~~^
fields=all_fields,
^^^^^^^^^^^^^^^^^^
...<2 lines>...
separate_input_output_schemas=separate_input_output_schemas,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\fastapi\_compat.py", line 229, in get_definitions
field_mapping, definitions = schema_generator.generate_definitions(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
inputs=inputs
^^^^^^^^^^^^^
)
^
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\pydantic\json_schema.py", line 379, in generate_definitions
self.generate_inner(schema)
~~~~~~~~~~~~~~~~~~~^^^^^^^^
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\pydantic\json_schema.py", line 552, in generate_inner
json_schema = current_handler(schema)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\pydantic\_internal\_schema_generation_shared.py", line 37, in __call__
return self.handler(core_schema)
~~~~~~~~~~~~^^^^^^^^^^^^^
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\pydantic\json_schema.py", line 527, in new_handler_func
json_schema = js_modify_function(schema_or_field, current_handler)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\beanie\odm\fields.py", line 178, in __get_pydantic_json_schema__
json_schema = handler(schema)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\pydantic\_internal\_schema_generation_shared.py", line 37, in __call__
return self.handler(core_schema)
~~~~~~~~~~~~^^^^^^^^^^^^^
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\pydantic\json_schema.py", line 511, in handler_func
json_schema = generate_for_schema_type(schema_or_field)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\pydantic\json_schema.py", line 1263, in json_or_python_schema
return self.generate_inner(schema['json_schema'])
~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\pydantic\json_schema.py", line 552, in generate_inner
json_schema = current_handler(schema)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\pydantic\_internal\_schema_generation_shared.py", line 37, in __call__
return self.handler(core_schema)
~~~~~~~~~~~~^^^^^^^^^^^^^
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\pydantic\json_schema.py", line 511, in handler_func
json_schema = generate_for_schema_type(schema_or_field)
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\pydantic\json_schema.py", line 1014, in function_plain_schema
return self.handle_invalid_for_json_schema(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
schema, f'core_schema.PlainValidatorFunctionSchema ({schema["function"]})'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "C:\Users\hagd0\AppData\Local\pypoetry\Cache\virtualenvs\ceep-api-B8fTZraC-py3.13\Lib\site-packages\pydantic\json_schema.py", line 2185, in handle_invalid_for_json_schema
raise PydanticInvalidForJsonSchema(f'Cannot generate a JsonSchema for {error_info}')
pydantic.errors.PydanticInvalidForJsonSchema: Cannot generate a JsonSchema for core_schema.PlainValidatorFunctionSchema ({'type': 'with-info', 'function': <bound method PydanticObjectId.validate of <class 'beanie.odm.fields.PydanticObjectId'>>})
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 15 (8 by maintainers)
Commits related to this issue
- Lock Beanie version to 1.27.0 in project configuration. Reason: https://github.com/BeanieODM/beanie/issues/1087 This change downgrades the Beanie package from 1.28.0 to 1.27.0 in `pyproject.toml` and... — committed to Star-Solution-FZCO/snail-orbit by deleted user 2 years ago
- Update python packages, use poetry 2.0 - Updated beanie to version 1.29.0 (since https://github.com/BeanieODM/beanie/issues/1087 is fixed); - Modified the author field format; - Update other python p... — committed to Star-Solution-FZCO/snail-orbit by deleted user 2 years ago
Hi @hagd0520, thank you for the report. This issue will be solved by #1080.
This prob is solved from 1.29.0 ver. tyvm.
I found workaround, plz check it 🐍
That is not the wanted solution as it will not show “that field actually is bson ObjectId” on client side. I guess we need fix…