✅ Add tests for nullable parameters and defaults#14829
Draft
YuriiMotov wants to merge 11 commits intomasterfrom
Draft
✅ Add tests for nullable parameters and defaults#14829YuriiMotov wants to merge 11 commits intomasterfrom
YuriiMotov wants to merge 11 commits intomasterfrom
Conversation
YuriiMotov
commented
Feb 6, 2026
| "type": "missing", | ||
| "loc": ["body", "int_val"], | ||
| "msg": "Field required", | ||
| "input": IsOneOf(None, {}), |
Member
Author
There was a problem hiding this comment.
I think ideally we should fix this as well - make parameters declared as model and non-model parameters return the same value. And, it should be probably {}.
This will likely be fixed as a side effect if we address all other issues
| "file": { | ||
| "title": "File", | ||
| "anyOf": [{"type": "string", "format": "binary"}, {"type": "null"}], | ||
| "default": "default", # <= Default value for file looks strange to me |
Member
Author
There was a problem hiding this comment.
Suggested change
| "default": "default", # <= Default value for file looks strange to me | |
| "default": "default", |
Does it make sense to have default value for File field?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
There are currently several issues around nullable parameters and parameters with defaults:
Nonefor Form parameters declared as Pydantic model - this leads to validation error for optionalintparameter when empty string is submittednullvalues are treated as missing value for non-Model Body parametersNone, it's not reflected in schemaFormparametersdoesn’t seem to work correctly -
Nonevalues are then being dropped. So, it’s equal to not providing the value. But people want to be able to distinguish between no value and explicitly passed empty string (ecplicitNonefor nullable parameter)null-value body is treated as missing (it’s valid body in latest specs)bytesFile parameter, but it doesn’t work (raisesAttributeErroras FastAPI attempts to use it as file object)This PR is intended to expose these issues for different types of parameters.
Tests structure
Tests for each type of parameter (
Query,Form,Body, etc..) in general follow the pattern:None(optional); 3 - default is notNone):Bodyalso have single parameters withembed=False)str,int,list[int]Body)nullvalues (and withnull-body in case ofBody) when it's possibeBeforeValidator). Also checkfields_setfor parameters declared as modelOpen questions
Fileparameters? I can't come up with use case when it might be needed. And it doesn't work currentlyFormfields, how should we treat the empty string values?