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

feat: Capability for Pydantic-based OpenAPI response schemas #3795

Merged
merged 6 commits into from
Apr 25, 2024

Conversation

khvn26
Copy link
Member

@khvn26 khvn26 commented Apr 18, 2024

Thanks for submitting a PR! Please check the boxes below:

  • I have run pre-commit to check linting
  • I have added information to docs/ if required so people know about the feature!
  • I have filled in the "Changes" section below?
  • I have filled in the "How did you test this code" section below?
  • I have used a Conventional Commit title for this Pull Request

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 with swagger_auto_schema.

  • Add exclude_model_fields utility function
  • Add PydanticResponseCapableSwaggerAutoSchema drf-yasg view inspector
  • Fix invalid nullable field schema generation

How did you test this code?

  • Observed correct Swagger schema locally.
  • Added a unit test.

@khvn26 khvn26 requested a review from matthewelwell April 18, 2024 08:12
Copy link

vercel bot commented Apr 18, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 25, 2024 8:15pm
flagsmith-frontend-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 25, 2024 8:15pm
flagsmith-frontend-staging ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 25, 2024 8:15pm

Copy link
Contributor

github-actions bot commented Apr 18, 2024

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!

Copy link
Contributor

@matthewelwell matthewelwell left a 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.

api/util/pydantic.py Show resolved Hide resolved
api/api/openapi.py Show resolved Hide resolved
api/api/openapi.py Show resolved Hide resolved
api/api/openapi.py Outdated Show resolved Hide resolved
api/api/openapi.py Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Apr 22, 2024

Codecov Report

Attention: Patch coverage is 98.07692% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 95.92%. Comparing base (507080e) to head (ec2eed3).
Report is 6 commits behind head on main.

❗ Current head ec2eed3 differs from pull request most recent head 11c2d21. Consider uploading reports for the commit 11c2d21 to get more accurate results

Files Patch % Lines
api/api/openapi.py 96.00% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

return elem


class PydanticResponseCapableSwaggerAutoSchema(SwaggerAutoSchema):
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

@zachaysan zachaysan left a 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.

definitions = self.components.with_scope(SCHEMA_DEFINITIONS)

for status_code in list(response_serializers):
if isinstance(response_serializers[status_code], type) and issubclass(
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

khvn26 added 6 commits April 25, 2024 21:08
 - Add `exclude_model_fields` utility function
 - Add `PydanticResponseCapableSwaggerAutoSchema` drf-yasg view inspector
 - Fix invalid nullable field schema generation
@khvn26 khvn26 force-pushed the feat/pydantic-response-schemas branch from 3fdf3a8 to 11c2d21 Compare April 25, 2024 20:08
@khvn26 khvn26 added this pull request to the merge queue Apr 25, 2024
Merged via the queue into main with commit 609deaa Apr 25, 2024
18 of 21 checks passed
@khvn26 khvn26 deleted the feat/pydantic-response-schemas branch April 25, 2024 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issue related to the REST API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants