vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
7.23k stars 429 forks source link

[BUG] Unable to generate pydantic-core schema for <class 'function'>. #970

Open alessandrosp opened 11 months ago

alessandrosp commented 11 months ago

Describe the bug When I try to pass a function to auth I receive an error from Pydantic about it being Unable to generate pydantic-core schema for <class 'function'>.

Versions (please complete the following information):

Detailed description When I try to run this snippet that's specified in the official documentation:

def ip_whitelist(request):
    if request.META["REMOTE_ADDR"] == "8.8.8.8":
        return "8.8.8.8"

@api.get("/ipwhiltelist", auth=ip_whitelist)
def ipwhiltelist(request):
    return f"Authenticated client, IP = {request.auth}"

I run into the following problem:

_pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'function'>. Set \arbitrary_types_allowed=True\ in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.``

If you got this error by calling handler(<some type>) within \__get_pydantic_core_schema__\ then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.``_
vitalik commented 11 months ago

Hi @alessandrosp

cannot reproduce this - maybe you did some modification ?

SCR-20231205-rqlc