diff --git a/fastapi/_compat.py b/fastapi/_compat.py index 492322337..0a6fecad4 100644 --- a/fastapi/_compat.py +++ b/fastapi/_compat.py @@ -81,7 +81,6 @@ if PYDANTIC_V2: general_plain_validator_function as with_info_plain_validator_function, # noqa: F401 ) - class GenerateJsonSchema(_GenerateJsonSchema): def __init__( self, by_alias: bool = True, ref_template: str = DEFAULT_REF_TEMPLATE @@ -96,14 +95,25 @@ if PYDANTIC_V2: return super().generate_inner(schema["schema"]) return super().nullable_schema(schema) + def default_schema( + self, schema: core_schema.WithDefaultSchema + ) -> JsonSchemaValue: + json_schema = super().default_schema(schema) + if ( + self.skip_null_schema + and json_schema.get("default", PydanticUndefined) is None + ): + json_schema.pop("default") + return json_schema + def generate_definitions( self, inputs: Sequence[ - tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema] + Tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema] ], - ) -> tuple[ - dict[tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], - dict[DefsRef, JsonSchemaValue], + ) -> Tuple[ + Dict[Tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], + Dict[DefsRef, JsonSchemaValue], ]: # Avoid circular import - Maybe there's a better way to check if it's a Param from fastapi.params import Param @@ -116,7 +126,7 @@ if PYDANTIC_V2: ) for key, mode, schema in inputs: - self.mode = mode + self._mode = mode self.skip_null_schema = isinstance(key, ModelField) and isinstance( key.field_info, Param ) @@ -124,9 +134,12 @@ if PYDANTIC_V2: definitions_remapping = self._build_definitions_remapping() - json_schemas_map: dict[tuple[JsonSchemaKeyT, JsonSchemaMode], DefsRef] = {} + json_schemas_map: Dict[Tuple[JsonSchemaKeyT, JsonSchemaMode], DefsRef] = {} for key, mode, schema in inputs: - self.mode = mode + self._mode = mode + self.skip_null_schema = isinstance(key, ModelField) and isinstance( + key.field_info, Param + ) json_schema = self.generate_inner(schema) json_schemas_map[(key, mode)] = definitions_remapping.remap_json_schema( json_schema diff --git a/tests/test_multi_body_errors.py b/tests/test_multi_body_errors.py index 0102f0f1a..5db4a6f9f 100644 --- a/tests/test_multi_body_errors.py +++ b/tests/test_multi_body_errors.py @@ -83,7 +83,11 @@ def test_put_incorrect_body_multiple(): }, { "type": "decimal_parsing", - "loc": ["body", 0, "age"], + "loc": [ + "body", + 0, + "age", + ], "msg": "Input should be a valid decimal", "input": "five", }, @@ -95,7 +99,11 @@ def test_put_incorrect_body_multiple(): }, { "type": "decimal_parsing", - "loc": ["body", 1, "age"], + "loc": [ + "body", + 1, + "age", + ], "msg": "Input should be a valid decimal", "input": "six", },