Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Comments

✅ Add tests for nullable parameters and defaults#14829

Draft
YuriiMotov wants to merge 11 commits intomasterfrom
add-tests-for-parameter-defaults
Draft

✅ Add tests for nullable parameters and defaults#14829
YuriiMotov wants to merge 11 commits intomasterfrom
add-tests-for-parameter-defaults

Conversation

@YuriiMotov
Copy link
Member

@YuriiMotovYuriiMotov commentedFeb 5, 2026
edited
Loading

Description

There are currently several issues around nullable parameters and parameters with defaults:

  • inability to distinguish explicitly passed values from defaults (if no value received, the default value is passed to parameter validator)
  • parameters declared as Pydantic model behave different from non-model parameters (different behavior and error messages)
    • empty strings are not replaced byNone for Form parameters declared as Pydantic model - this leads to validation error for optionalint parameter when empty string is submitted
    • title of Header parameters contains hyphens for non-model parameters (for model parameters hyphens are replaced by whitespaces)
    • for Header parameters declared as Pydantic model, field names use underscore instead of hyphens in error response
    • null values are treated as missing value for non-Model Body parameters
    • missing or invalid body value results in different errors for parameters declared as Body model and parameters declared as several Body parameters
  • if the default value isNone, it's not reflected in schema
  • Handling of empty strings forForm parameters

doesn’t seem to work correctly -None values 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 (ecplicitNone for nullable parameter)

  • null-value body is treated as missing (it’s valid body in latest specs)
  • It’s possible to specify default value forbytes File parameter, but it doesn’t work (raisesAttributeError as 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:

  • For different default values ( 1 -without default value (nullable required); 2 -default isNone (optional); 3 -default is notNone):
    • Tests for:non-model parameters andparameters declared as Pydantic model (forBody also have single parameters withembed=False)
      • Declare parameters with different field types (all nullable):str,int,list[int]
      • Test:
        • openapi schema
        • request with missing parameter values (and with missing body in case ofBody)
        • request with explicitnull values (and withnull-body in case ofBody) when it's possibe
        • request with non-default value
      • In tests check:
        • Data passed to field validator is as expected (checked usingBeforeValidator). Also checkfields_set for parameters declared as model
        • Schema and error messages are correct and equal in case of parameters declared as model and non-model parameters
        • Values after validation are correct

Open questions

  • Should we prohibit default value forFile parameters? I can't come up with use case when it might be needed. And it doesn't work currently
  • ForForm fields, how should we treat the empty string values?

@codspeed-hq
Copy link

Merging this PR willnot alter performance

✅ 20 untouched benchmarks


Comparingadd-tests-for-parameter-defaults (7fed267) withmaster (0e68d36)1

Open in CodSpeed

Footnotes

  1. No successful run was found onmaster (64d0ee9) during the generation of this report, so0e68d36 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

"type": "missing",
"loc": ["body", "int_val"],
"msg": "Field required",
"input": IsOneOf(None, {}),
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

Suggested change
"default":"default",# <= Default value for file looks strange to me
"default":"default",

Does it make sense to have default value forFile field?

@YuriiMotovYuriiMotov marked this pull request as ready for reviewFebruary 6, 2026 16:00
@YuriiMotovYuriiMotov marked this pull request as draftFebruary 8, 2026 19:09
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

@YuriiMotov

[8]ページ先頭

©2009-2026 Movatter.jp