-
Notifications
You must be signed in to change notification settings - Fork 272
feat(fastapi): use temp_pydantic_v1_params for pydantic v1/v1_on_v2 modes #11326
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
Conversation
…odes Co-Authored-By: thomas@buildwithfern.com <tjb9dcshop@gmail.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
🌱 Seed Test SelectorSelect languages to run seed tests for:
How to use: Click the ⋯ menu above → "Edit" → check the boxes you want → click "Update comment". Tests will run automatically and snapshots will be committed to this PR. |
Co-Authored-By: thomas@buildwithfern.com <tjb9dcshop@gmail.com>
| if uses_pydantic_v1_params: | ||
| EVALUATED_VARIABLE_NAME = "evaluated" | ||
| writer.write(f"{EVALUATED_VARIABLE_NAME} = ") | ||
| writer.write_reference(FastAPI.evaluate_forwardref) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is correct, but you also need to evaluate forward refs in the else block below for pydantic v2. forward refs need to be evaluated for all fastapi params that use Annotated, regardless of pydantic version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback! I've addressed both comments:
-
evaluate_forwardref for all params: Now applied to ALL FastAPI params that use
Annotated, not just v1/v1_on_v2 mode. The pydantic-v2 fixture now also includes theevaluate_forwardrefcall. -
Comment linking to FastAPI issue: Added a comment in the generated code that links to Can't use
AnnotatedwithForwardReffastapi/fastapi#13056
See commit 5d7ebbe for the changes.
| writer.write("]") | ||
| if uses_pydantic_v1_params: | ||
| EVALUATED_VARIABLE_NAME = "evaluated" | ||
| writer.write(f"{EVALUATED_VARIABLE_NAME} = ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you also write a comment into the generated code that links to the fastapi issue that this works around? here is the link: fastapi/fastapi#13056
…to FastAPI issue Co-Authored-By: thomas@buildwithfern.com <tjb9dcshop@gmail.com>
tjb9dc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we're here, let's remove response_model specification from our generated stubs. It is not necessary when the type is the same as the return type. See: https://docs.astral.sh/ruff/rules/fast-api-redundant-response-model/
Co-Authored-By: thomas@buildwithfern.com <tjb9dcshop@gmail.com>
…/git-manager.devin.ai/proxy/github.com/fern-api/fern into devin/1766079445-fastapi-pydantic-v1-params
|
Done! I've removed the redundant The change only removes See commit 01eff13. |
Actually, the proper thing is to include them but type them as “None” |
|
Thanks for the feedback! I have two questions:
|
Co-Authored-By: thomas@buildwithfern.com <tjb9dcshop@gmail.com>
…f evaluate_forwardref Co-Authored-By: thomas@buildwithfern.com <tjb9dcshop@gmail.com>
Description
Refs: Request from @tjb9dc via Slack
Adjusts the FastAPI generator to use FastAPI's
temp_pydantic_v1_paramsmodule for parameter markers (Path, Query, Header, Body, File) when in pydantic v1 or v1_on_v2 mode. Additionally, usestyping.get_type_hints()to resolve forward references before applying parameter annotations for ALL pydantic versions.Reference: https://github.com/fastapi/fastapi/blob/master/fastapi/temp_pydantic_v1_params.py
Related issue: fastapi/fastapi#13056
Changes Made
FastAPIParamsclass that generates version-aware imports based on pydantic compatibility modeFastApiGeneratorContextto expose afastapi_paramsinstanceself._context.fastapi_params.X()instead of staticFastAPI.X()methodsendpoint_generator.pyto usetyping.get_type_hints()for forward reference resolution (cleaner than per-parameterevaluate_forwardrefcalls)response_model=Nonefor all pydantic model endpoints to disable response model validation/serialization (per https://fastapi.tiangolo.com/tutorial/response-model/#disable-response-model)Testing
fastapi.temp_pydantic_v1_params.Body()etc.Human Review Checklist
response_model=Nonedoesn't affect OpenAPI schema generation negatively (FastAPI should still infer from wrapper's return type annotation)typing.get_type_hints()correctly resolves forward references in all edge casesUpdates Since Last Revision
evaluate_forwardref()calls to usingtyping.get_type_hints()once per endpoint (per reviewer feedback with diff from Candid API)response_modelhandling: now explicitly setsresponse_model=Nonefor all pydantic model endpoints instead of omitting it (per reviewer feedback citing https://fastapi.tiangolo.com/tutorial/response-model/#disable-response-model)Link to Devin run: https://app.devin.ai/sessions/75b0c5ac25314f3d8bec4af9136950d4
Requested by: thomas@buildwithfern.com (@tjb9dc)