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

🐛 Make sure a parameter defined as required is kept required in OpenAPI even if defined as optional in another dependency #4319

Merged
merged 8 commits into from Sep 3, 2022

Conversation

chardigio
Copy link
Contributor

@chardigio chardigio commented Dec 21, 2021

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.

Copy link
Contributor

@cikay cikay left a comment

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
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
Copy link

codecov bot commented Sep 3, 2022

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
Copy link
Owner

tiangolo commented Sep 3, 2022

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
Copy link
Contributor

github-actions bot commented Sep 3, 2022

📝 Docs preview for commit bafa232 at: https://63137cf667cd57337c0f7914--fastapi.netlify.app

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

github-actions bot commented Sep 3, 2022

📝 Docs preview for commit d2e3ae3 at: https://63137f52ea02943531f61914--fastapi.netlify.app

@tiangolo tiangolo merged commit 56f887d into tiangolo: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.

None yet

3 participants