Browse Source

Fix bug, update tests to work with latest pydantic

feat/custom-generate-json-schema
David Montague 2 years ago
parent
commit
a687898916
  1. 3
      fastapi/_compat.py
  2. 4
      tests/test_filter_pydantic_sub_model_pv2.py
  3. 32
      tests/test_multi_body_errors.py

3
fastapi/_compat.py

@ -117,6 +117,9 @@ if PYDANTIC_V2:
json_schemas_map: dict[tuple[JsonSchemaKeyT, JsonSchemaMode], DefsRef] = {}
for key, mode, schema in inputs:
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

4
tests/test_filter_pydantic_sub_model_pv2.py

@ -1,7 +1,7 @@
from typing import Optional
import pytest
from dirty_equals import IsDict
from dirty_equals import HasRepr, IsDict
from fastapi import Depends, FastAPI
from fastapi.exceptions import ResponseValidationError
from fastapi.testclient import TestClient
@ -66,7 +66,7 @@ def test_validator_is_cloned(client: TestClient):
"loc": ("response", "name"),
"msg": "Value error, name must end in A",
"input": "modelX",
"ctx": {"error": "name must end in A"},
"ctx": {"error": HasRepr("ValueError('name must end in A')")},
"url": match_pydantic_error_url("value_error"),
}
)

32
tests/test_multi_body_errors.py

@ -51,7 +51,7 @@ def test_jsonable_encoder_requiring_error():
"loc": ["body", 0, "age"],
"msg": "Input should be greater than 0",
"input": -1.0,
"ctx": {"gt": 0.0},
"ctx": {"gt": "0"},
"url": match_pydantic_error_url("greater_than"),
}
]
@ -84,9 +84,22 @@ def test_put_incorrect_body_multiple():
"input": {"age": "five"},
"url": match_pydantic_error_url("missing"),
},
{
"ctx": {"class": "Decimal"},
"input": "five",
"loc": ["body", 0, "age", "is-instance[Decimal]"],
"msg": "Input should be an instance of Decimal",
"type": "is_instance_of",
"url": match_pydantic_error_url("is_instance_of"),
},
{
"type": "decimal_parsing",
"loc": ["body", 0, "age"],
"loc": [
"body",
0,
"age",
"function-after[to_decimal(), union[float,int,constrained-str]]",
],
"msg": "Input should be a valid decimal",
"input": "five",
},
@ -97,9 +110,22 @@ def test_put_incorrect_body_multiple():
"input": {"age": "six"},
"url": match_pydantic_error_url("missing"),
},
{
"ctx": {"class": "Decimal"},
"input": "six",
"loc": ["body", 1, "age", "is-instance[Decimal]"],
"msg": "Input should be an instance of Decimal",
"type": "is_instance_of",
"url": match_pydantic_error_url("is_instance_of"),
},
{
"type": "decimal_parsing",
"loc": ["body", 1, "age"],
"loc": [
"body",
1,
"age",
"function-after[to_decimal(), union[float,int,constrained-str]]",
],
"msg": "Input should be a valid decimal",
"input": "six",
},

Loading…
Cancel
Save