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] = {} 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

4
tests/test_filter_pydantic_sub_model_pv2.py

@ -1,7 +1,7 @@
from typing import Optional from typing import Optional
import pytest import pytest
from dirty_equals import IsDict from dirty_equals import HasRepr, IsDict
from fastapi import Depends, FastAPI from fastapi import Depends, FastAPI
from fastapi.exceptions import ResponseValidationError from fastapi.exceptions import ResponseValidationError
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
@ -66,7 +66,7 @@ def test_validator_is_cloned(client: TestClient):
"loc": ("response", "name"), "loc": ("response", "name"),
"msg": "Value error, name must end in A", "msg": "Value error, name must end in A",
"input": "modelX", "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"), "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"], "loc": ["body", 0, "age"],
"msg": "Input should be greater than 0", "msg": "Input should be greater than 0",
"input": -1.0, "input": -1.0,
"ctx": {"gt": 0.0}, "ctx": {"gt": "0"},
"url": match_pydantic_error_url("greater_than"), "url": match_pydantic_error_url("greater_than"),
} }
] ]
@ -84,9 +84,22 @@ def test_put_incorrect_body_multiple():
"input": {"age": "five"}, "input": {"age": "five"},
"url": match_pydantic_error_url("missing"), "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", "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", "msg": "Input should be a valid decimal",
"input": "five", "input": "five",
}, },
@ -97,9 +110,22 @@ def test_put_incorrect_body_multiple():
"input": {"age": "six"}, "input": {"age": "six"},
"url": match_pydantic_error_url("missing"), "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", "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", "msg": "Input should be a valid decimal",
"input": "six", "input": "six",
}, },

Loading…
Cancel
Save