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

fix(openapi): validate response serialization when falsy #6119

Merged
merged 11 commits into from
Feb 20, 2025

Conversation

anafalcao
Copy link
Contributor

@anafalcao anafalcao commented Feb 18, 2025

Issue number: #5887

Summary

Changes

This PR fixes a bug where OpenAPI validation was not validating response serialization when the response body was a falsy value (e.g., False, 0, empty string, etc.), making the the response body validation is more strict.
It also removes Pydantic V1 compatibility.

User experience

Before:

class Todo(BaseModel):
    title: str
    status: Literal["Pending", "Done"]

@app.get("/none")
def return_none() -> Todo:
    return None # Returns 200

@app.get("/empty_list")
def return_empty_list() -> Todo:
    return list() # Returns 200

After:

class Todo(BaseModel):
    title: str
    status: Literal["Pending", "Done"]

@app.get("/none")
def return_none() -> Optional[Todo]:
    return None # Returns 200 -> Optional 

@app.get("/empty_list")
def return_empty_list() -> Todo:
    return list() # Returns 422 - validation error

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Feb 18, 2025
@github-actions github-actions bot added the bug Something isn't working label Feb 18, 2025
Copy link

codecov bot commented Feb 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.19%. Comparing base (d9719b9) to head (9517dec).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #6119      +/-   ##
===========================================
- Coverage    96.28%   96.19%   -0.10%     
===========================================
  Files          235      235              
  Lines        11229    11226       -3     
  Branches       830      828       -2     
===========================================
- Hits         10812    10799      -13     
- Misses         327      337      +10     
  Partials        90       90              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Feb 18, 2025
@anafalcao anafalcao marked this pull request as ready for review February 18, 2025 21:22
@anafalcao anafalcao requested a review from a team February 18, 2025 21:22
Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

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

Hey @anafalcao! Another super nice PR to fix an important bug! I left some comments to avoid any breaking change from our side.

@anafalcao anafalcao requested a review from a team as a code owner February 19, 2025 13:51
Copy link

@dreamorosi dreamorosi self-requested a review February 20, 2025 09:01
@leandrodamascena leandrodamascena merged commit 240177d into develop Feb 20, 2025
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working event_handlers size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Open API Validation not validating response serialization when body is Falsy.
3 participants