-
Notifications
You must be signed in to change notification settings - Fork 415
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
feat: Capability for Pydantic-based OpenAPI response schemas #3795
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
8df4d0a
to
8024333
Compare
8024333
to
63b244f
Compare
Uffizzi Ephemeral Environment Deploying☁️ https://app.uffizzi.com/github.com/Flagsmith/flagsmith/pull/3795 ⚙️ Updating now by workflow run 8817120325. What is Uffizzi? Learn more! |
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.
Added some general comments and also tested the swagger auto generation by running the code locally and viewing the generated docs.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3795 +/- ##
==========================================
- Coverage 95.96% 95.92% -0.05%
==========================================
Files 1126 1125 -1
Lines 35786 35530 -256
==========================================
- Hits 34342 34081 -261
- Misses 1444 1449 +5 ☔ View full report in Codecov by Sentry. |
1c06394
to
f3b132f
Compare
return elem | ||
|
||
|
||
class PydanticResponseCapableSwaggerAutoSchema(SwaggerAutoSchema): |
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.
Since this is the entry point to this logic for anyone coming across the class reference in the settings file, I think it would be good to add a docstring to this class with a short summary explaining why we're overriding the default behaviour.
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.
Done.
f3b132f
to
415c925
Compare
415c925
to
ec2eed3
Compare
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.
The rest of it looks 👌🏼 but I'm concerned about type
not being set as intentioned. Assuming that is fixed feel free to merge this PR.
api/api/openapi.py
Outdated
definitions = self.components.with_scope(SCHEMA_DEFINITIONS) | ||
|
||
for status_code in list(response_serializers): | ||
if isinstance(response_serializers[status_code], type) and issubclass( |
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.
I believe this if check will always fail since type
is being passed in as a built-in function.
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.
type
is, in fact, a class:
>>> type
<class 'type'>
>>> type.__mro__
(<class 'type'>, <class 'object'>)
So the instance check works as intended:
>>> obj = object()
>>> isinstance(obj, type)
False
>>> class Class: pass
...
>>> isinstance(Class, type)
True
Additionally, you can see the coverage report to make sure the code path is being run.
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.
Well, it's good to see that type
is set as intentioned, but it's a surprising check that at least warrants a comment so that confused developers are set right.
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.
Changed to inspect.isclass
for more clarity.
- Add `exclude_model_fields` utility function - Add `PydanticResponseCapableSwaggerAutoSchema` drf-yasg view inspector - Fix invalid nullable field schema generation
3fdf3a8
to
11c2d21
Compare
Thanks for submitting a PR! Please check the boxes below:
pre-commit
to check lintingdocs/
if required so people know about the feature!Changes
This PR solves incorrect rendering of
Optional
fields of the environment document Swagger spec, and adds a generalised way to use Pydantic models to describe API responses withswagger_auto_schema
.exclude_model_fields
utility functionPydanticResponseCapableSwaggerAutoSchema
drf-yasg view inspectorHow did you test this code?