Skip to content

πŸ› Make sure a parameter defined as required is kept required in OpenAPI even if defined as optional in another dependency - #4319

Merged
tiangolo merged 8 commits into
fastapi:masterfrom
chardigio:enforce-once-required-parameter
Sep 3, 2022
Merged

tiangolo merged 8 commits into
fastapi:masterfrom
chardigio:enforce-once-required-parameter

Conversation

@chardigio

@chardigio chardigio commented Dec 21, 2021 •

Copy link
Copy Markdown
Contributor

Summary
In the case where a parameter is looked for by multiple dependencies, but only required by some of them, we should reflect that in the openapi spec as required=True.

Currently
Right now the openapi spec indeterminately displays the parameter being required/optional. However, omitting the parameter will always result in a 422.

Suggested
We ought not change anything about how the server acts, however we should show that the parameter is required in the openapi spec.

Rationale
As is, this is misleading and confusing for anyone trying to work with OpenAPI where this kind of situation arises. This becomes especially important when using inheritance with class-based views. Often times you may want to look for a certain field in a base view, but only require it in some of the views that inherit from it.

@cikay cikay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The commit message should be "Simplify conditions"

@chardigio

Copy link
Copy Markdown
Contributor Author

@tiangolo any chance you can check this out? It would help clear up our docs a lot πŸ™πŸΌ

@tiangolo tiangolo changed the title Enforce once-required parameter πŸ› Make sure a parameter defined as required is kept required in OpenAPI even if defined as optional in another dependency Sep 3, 2022
@codecov

codecov Bot commented Sep 3, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #4319 (230cdcc) into master (fbe1a80) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master     #4319   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          538       539    +1     
  Lines        13872     13902   +30     
=========================================
+ Hits         13872     13902   +30     
Impacted Files Coverage Ξ”
fastapi/openapi/utils.py 100.00% <100.00%> (ΓΈ)
tests/test_enforce_once_required_parameter.py 100.00% <100.00%> (ΓΈ)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@tiangolo

tiangolo commented Sep 3, 2022

Copy link
Copy Markdown
Member

Thanks for the contribution @cd17822! 🍰

I updated the test to have in mind the new schemas for validation errors. And I refactored the implementation to account for the location of parameters, from a recent PR.

Thanks for the interest here @cikay, nevertheless, it's not necessary to ask for some type of commit messages. I make sure the final commit message is consistent with the rest and then squash and merge. It's better to check if the implementation is correct, if it has tests, if the tests break before the fix and pass after the fix. Thanks!

This will be available in the next FastAPI version, 0.82.0, probably in the next hours. πŸŽ‰

@github-actions

github-actions Bot commented Sep 3, 2022

Copy link
Copy Markdown
Contributor

πŸ“ Docs preview for commit bafa232 at: https://63137cf667cd57337c0f7914--fastapi.netlify.app

@tiangolo
tiangolo enabled auto-merge (squash) September 3, 2022 16:13
@github-actions

github-actions Bot commented Sep 3, 2022

Copy link
Copy Markdown
Contributor

πŸ“ Docs preview for commit d2e3ae3 at: https://63137f52ea02943531f61914--fastapi.netlify.app

@tiangolo
tiangolo merged commit 56f887d into fastapi:master Sep 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants