Skip to content

Commit

Permalink
pydatnic 2.9 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalik committed Sep 16, 2024
1 parent 500823b commit 7c39ecc
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 30 deletions.
11 changes: 7 additions & 4 deletions tests/test_annotated.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import List

from typing_extensions import Annotated
from util import pydantic_ref_fix

from ninja import Body, Cookie, Form, Header, NinjaAPI, Path, Query, Schema
from ninja.testing import TestClient
Expand Down Expand Up @@ -192,10 +193,12 @@ def test_openapi_schema():
"requestBody": {
"content": {
"application/json": {
"schema": {
"allOf": [{"$ref": "#/components/schemas/Payload"}],
"examples": [{"p": "test", "t": 42}],
}
"schema": pydantic_ref_fix(
{
"$ref": "#/components/schemas/Payload",
"examples": [{"p": "test", "t": 42}],
}
)
}
},
"required": True,
Expand Down
15 changes: 9 additions & 6 deletions tests/test_orm_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.contrib.postgres import fields as ps_fields
from django.db import models
from django.db.models import Manager
from util import pydantic_ref_fix

from ninja.errors import ConfigError
from ninja.orm import create_schema
Expand Down Expand Up @@ -284,16 +285,18 @@ class Meta:
}

SchemaClsDeep = create_schema(TestModel, name="TestSchemaDeep", depth=1)
# print(SchemaClsDeep.json_schema())
print(SchemaClsDeep.json_schema())
assert SchemaClsDeep.json_schema() == {
"type": "object",
"properties": {
"id": {"anyOf": [{"type": "integer"}, {"type": "null"}], "title": "ID"},
"onetoonefield": {
"title": "Onetoonefield",
"description": "",
"allOf": [{"$ref": "#/$defs/Related"}],
},
"onetoonefield": pydantic_ref_fix(
{
"title": "Onetoonefield",
"description": "",
"$ref": "#/$defs/Related",
}
),
"foreignkey": {
"title": "Foreignkey",
"allOf": [{"$ref": "#/$defs/Related"}],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@
"data": {
"title": "Data4 Title",
"description": "Data4 Desc",
"allOf": [
{
"$ref": "#/components/schemas/TestData4"
}
]
"$ref": "#/components/schemas/TestData4"
},
"nested-data": {
"$ref": "#/components/schemas/TestData"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@
"data": {
"title": "Data4 Title",
"description": "Data4 Desc",
"allOf": [
{
"$ref": "#/components/schemas/TestData4"
}
]
"$ref": "#/components/schemas/TestData4"
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@
"data": {
"title": "Data4 Title",
"description": "Data4 Desc",
"allOf": [
{
"$ref": "#/components/schemas/TestData4"
}
]
"$ref": "#/components/schemas/TestData4"
}
},
"required": [
Expand Down
6 changes: 1 addition & 5 deletions tests/test_with_django/schema_fixtures/test-multi-body.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@
"data": {
"title": "Data4 Title",
"description": "Data4 Desc",
"allOf": [
{
"$ref": "#/components/schemas/TestData4"
}
]
"$ref": "#/components/schemas/TestData4"
},
"nested-data": {
"$ref": "#/components/schemas/TestData"
Expand Down
2 changes: 2 additions & 0 deletions tests/test_with_django/test_multi_param_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def test_validate_test_data():
json.dump(schema["paths"][path], f, indent=2)
with Path(filename).open() as f:
data = json.load(f)
print("---" * 10, filename)
print(data)
assert json.loads(json.dumps(schema["paths"][path])) == data


Expand Down
13 changes: 13 additions & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pydantic


def pydantic_ref_fix(data: dict):
"In pydantic 1.7 $ref was changed to allOf: [{'$ref': ...}] but in 2.9 it was changed back"
v = tuple(map(int, pydantic.version.version_short().split(".")))
if v < (1, 7) or v >= (2, 9):
return data

result = data.copy()
if "$ref" in data:
result["allOf"] = [{"$ref": result.pop("$ref")}]
return result

0 comments on commit 7c39ecc

Please sign in to comment.