Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportString fails to generate JsonSchema - in fastapi openapi docs generation #9327

Closed
1 task done
amitschang opened this issue Apr 26, 2024 · 3 comments · Fixed by #9344
Closed
1 task done

ImportString fails to generate JsonSchema - in fastapi openapi docs generation #9327

amitschang opened this issue Apr 26, 2024 · 3 comments · Fixed by #9344
Labels
bug V2 Bug related to Pydantic V2 good first issue help wanted Pull Request welcome

Comments

@amitschang
Copy link
Contributor

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

We use pydantic with FastApi and have a model which contains an ImportString field associated with an endpoint and found that the openapi docs generation failed due to ImportString. Underlying issue is that pydantic could not create JsonSchema for the object, the error looks like:

 raise PydanticInvalidForJsonSchema(f'Cannot generate a JsonSchema for {error_info}')
pydantic.errors.PydanticInvalidForJsonSchema: Cannot generate a JsonSchema for core_schema.PlainValidatorFunctionSchema ({'type': 'no-info', 'function': <function import_string at 0x105a2c360>})

A workaround was to create a type-annotated version:

ImportString = Annotated[
    pydantic.ImportString, pydantic.WithJsonSchema({'type': 'string', 'description': 'fully qualified class name'})
]

I'm not sure if this is intended, but the docs don't seem to explicitly mention the json schema serialization.

Example Code

# Issue was in FastApi docs generation, but the below produces an error that 
# looks the same
import pydantic

pydantic.TypeAdapter(pydantic.ImportString).json_schema()

Python, Pydantic & OS Version

python -c "import pydantic.version; print(pydantic.version.version_info())"
             pydantic version: 2.7.1
        pydantic-core version: 2.18.2
          pydantic-core build: profile=release pgo=true
                 install path: /Users/arik/.miniconda3/lib/python3.11/site-packages/pydantic
               python version: 3.11.5 (main, Sep 11 2023, 08:31:25) [Clang 14.0.6 ]
                     platform: macOS-13.5-arm64-arm-64bit
             related packages: typing_extensions-4.9.0 fastapi-0.108.0
                       commit: unknown
@amitschang amitschang added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Apr 26, 2024
@sydney-runkle
Copy link
Member

@amitschang,

Ah, nice catch. We'll want to add json schema support to the ImportString type. PR welcome!

@sydney-runkle sydney-runkle added good first issue help wanted Pull Request welcome and removed pending Awaiting a response / confirmation labels Apr 28, 2024
@amitschang
Copy link
Contributor Author

@sydney-runkle, thanks for looking! I'll see if I have some time to work on such a PR

@amitschang
Copy link
Contributor Author

@sydney-runkle , I made a PR - I can't seem to add the necessary labels to it (ready for review and relnotes-fix), but it is there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 good first issue help wanted Pull Request welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants