]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
Fix bug, update tests to work with latest pydantic
authorDavid Montague <35119617+dmontagu@users.noreply.github.com>
Thu, 13 Jul 2023 16:25:49 +0000 (10:25 -0600)
committerDavid Montague <35119617+dmontagu@users.noreply.github.com>
Thu, 13 Jul 2023 16:25:49 +0000 (10:25 -0600)
fastapi/_compat.py
tests/test_filter_pydantic_sub_model_pv2.py
tests/test_multi_body_errors.py

index fa0065c3ad1302c57d7fcd34b5bc8a0c29e1694c..a6d20270a130a65de7f85f12208a5183f1c5c6e5 100644 (file)
@@ -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
index 656332a010249091df3633db7d826282f1f9cfe4..ae12179bdf3149a137bdde2910cedba24547bcb0 100644 (file)
@@ -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"),
             }
         )
index aa989c612874f404e189d3e169ab0e975a4f879b..82f1d434679e2b48d0c31642c59d15276919bbf1 100644 (file)
@@ -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",
                 },