]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
🐛 Fix JSON Schema for dataclasses, supporting the fixes in Pydantic 1.9 (#4272)
authorEric Jolibois <em.jolibois@gmail.com>
Sun, 12 Dec 2021 11:28:35 +0000 (12:28 +0100)
committerGitHub <noreply@github.com>
Sun, 12 Dec 2021 11:28:35 +0000 (12:28 +0100)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
tests/test_tutorial/test_dataclasses/test_tutorial002.py

index 10d8d227dcb82bc276a203a39ace3dbfd5c5fb42..34aeb0be5f54a3a05f9e5e4d26722c40809c6f81 100644 (file)
@@ -1,3 +1,5 @@
+from copy import deepcopy
+
 from fastapi.testclient import TestClient
 
 from docs_src.dataclasses.tutorial002 import app
@@ -29,7 +31,7 @@ openapi_schema = {
         "schemas": {
             "Item": {
                 "title": "Item",
-                "required": ["name", "price", "tags"],
+                "required": ["name", "price"],
                 "type": "object",
                 "properties": {
                     "name": {"title": "Name", "type": "string"},
@@ -51,7 +53,18 @@ openapi_schema = {
 def test_openapi_schema():
     response = client.get("/openapi.json")
     assert response.status_code == 200
-    assert response.json() == openapi_schema
+    # TODO: remove this once Pydantic 1.9 is released
+    # Ref: https://github.com/samuelcolvin/pydantic/pull/2557
+    data = response.json()
+    alternative_data1 = deepcopy(data)
+    alternative_data2 = deepcopy(data)
+    alternative_data1["components"]["schemas"]["Item"]["required"] = ["name", "price"]
+    alternative_data2["components"]["schemas"]["Item"]["required"] = [
+        "name",
+        "price",
+        "tags",
+    ]
+    assert alternative_data1 == openapi_schema or alternative_data2 == openapi_schema
 
 
 def test_get_item():