[BugFix] Set Default State For **kwargs in API to Make Them Optional. #6252
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This Tags on to #6250, #6248, #6240.
When
parameter.kind == Parameter.VAR_KEYWORD
, it correctly cannot be assigned a default state because **kwargs cannot have one.However, Fast API does not see it the same way and will make it a required field. In both, the body, and URL parameters.
To get around this, we can set:
parameter.kind == Parameter.POSITIONAL_OR_KEYWORD
, then assign it asOptional[Dict[str, Any]]
, and give it a default state ofNone
.The end result is that
**kwargs
can be a valid parameter for the API that gets assigned to the body of the request.Note that
**kwargs
could be anything -**extra_params
.