Skip to content

Commit b0cd4d7

Browse files
PrettyWoodtiangolo
andauthored
🐛 Fix JSON Schema for dataclasses, supporting the fixes in Pydantic 1.9 (fastapi#4272)
Co-authored-by: Sebastián Ramírez <[email protected]>
1 parent 0a87bc8 commit b0cd4d7

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

tests/test_tutorial/test_dataclasses/test_tutorial002.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from copy import deepcopy
2+
13
from fastapi.testclient import TestClient
24

35
from docs_src.dataclasses.tutorial002 import app
@@ -29,7 +31,7 @@
2931
"schemas": {
3032
"Item": {
3133
"title": "Item",
32-
"required": ["name", "price", "tags"],
34+
"required": ["name", "price"],
3335
"type": "object",
3436
"properties": {
3537
"name": {"title": "Name", "type": "string"},
@@ -51,7 +53,18 @@
5153
def test_openapi_schema():
5254
response = client.get("/openapi.json")
5355
assert response.status_code == 200
54-
assert response.json() == openapi_schema
56+
# TODO: remove this once Pydantic 1.9 is released
57+
# Ref: https://github.com/samuelcolvin/pydantic/pull/2557
58+
data = response.json()
59+
alternative_data1 = deepcopy(data)
60+
alternative_data2 = deepcopy(data)
61+
alternative_data1["components"]["schemas"]["Item"]["required"] = ["name", "price"]
62+
alternative_data2["components"]["schemas"]["Item"]["required"] = [
63+
"name",
64+
"price",
65+
"tags",
66+
]
67+
assert alternative_data1 == openapi_schema or alternative_data2 == openapi_schema
5568

5669

5770
def test_get_item():

0 commit comments

Comments
 (0)