Browse Source

update mode and default_schema

fix tests to work on python3.8
pull/12375/head
Florian Maurer 10 months ago
parent
commit
169e5b2b4e
No known key found for this signature in database GPG Key ID: D5B29CD0C9E06237
  1. 29
      fastapi/_compat.py
  2. 12
      tests/test_multi_body_errors.py

29
fastapi/_compat.py

@ -81,7 +81,6 @@ if PYDANTIC_V2:
general_plain_validator_function as with_info_plain_validator_function, # noqa: F401 general_plain_validator_function as with_info_plain_validator_function, # noqa: F401
) )
class GenerateJsonSchema(_GenerateJsonSchema): class GenerateJsonSchema(_GenerateJsonSchema):
def __init__( def __init__(
self, by_alias: bool = True, ref_template: str = DEFAULT_REF_TEMPLATE 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().generate_inner(schema["schema"])
return super().nullable_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( def generate_definitions(
self, self,
inputs: Sequence[ inputs: Sequence[
tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema] Tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema]
], ],
) -> tuple[ ) -> Tuple[
dict[tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], Dict[Tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue],
dict[DefsRef, JsonSchemaValue], Dict[DefsRef, JsonSchemaValue],
]: ]:
# Avoid circular import - Maybe there's a better way to check if it's a Param # Avoid circular import - Maybe there's a better way to check if it's a Param
from fastapi.params import Param from fastapi.params import Param
@ -116,7 +126,7 @@ if PYDANTIC_V2:
) )
for key, mode, schema in inputs: for key, mode, schema in inputs:
self.mode = mode self._mode = mode
self.skip_null_schema = isinstance(key, ModelField) and isinstance( self.skip_null_schema = isinstance(key, ModelField) and isinstance(
key.field_info, Param key.field_info, Param
) )
@ -124,9 +134,12 @@ if PYDANTIC_V2:
definitions_remapping = self._build_definitions_remapping() 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: 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_schema = self.generate_inner(schema)
json_schemas_map[(key, mode)] = definitions_remapping.remap_json_schema( json_schemas_map[(key, mode)] = definitions_remapping.remap_json_schema(
json_schema json_schema

12
tests/test_multi_body_errors.py

@ -83,7 +83,11 @@ def test_put_incorrect_body_multiple():
}, },
{ {
"type": "decimal_parsing", "type": "decimal_parsing",
"loc": ["body", 0, "age"], "loc": [
"body",
0,
"age",
],
"msg": "Input should be a valid decimal", "msg": "Input should be a valid decimal",
"input": "five", "input": "five",
}, },
@ -95,7 +99,11 @@ def test_put_incorrect_body_multiple():
}, },
{ {
"type": "decimal_parsing", "type": "decimal_parsing",
"loc": ["body", 1, "age"], "loc": [
"body",
1,
"age",
],
"msg": "Input should be a valid decimal", "msg": "Input should be a valid decimal",
"input": "six", "input": "six",
}, },

Loading…
Cancel
Save