Skip to content

Commit 7d02914

Browse files
committed
Add test cases
1 parent 3950b2b commit 7d02914

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# project dependencies
2-
pydantic==1.8.2
2+
pydantic==1.9.0
33

44
# PyPi
5-
twine==3.4.2
5+
twine==3.8.0
66

77
# dev dependencies
88
# black

tests/util/__init__.py

Whitespace-only changes.

tests/util/test_pydantic_field.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from typing import Literal, Union
2+
3+
from pydantic import BaseModel, Field
4+
from pydantic.schema import schema
5+
6+
from openapi_schema_pydantic import OpenAPI, Info, PathItem, Operation, RequestBody, MediaType, Response, Schema, \
7+
Reference, Discriminator
8+
from openapi_schema_pydantic.util import PydanticSchema, construct_open_api_with_schema_class
9+
10+
11+
def test_pydantic_discriminator_schema_generation():
12+
"""https://github.com/kuimono/openapi-schema-pydantic/issues/8"""
13+
14+
json_schema = schema([RequestModel])
15+
assert json_schema == {
16+
"definitions": {
17+
"DataAModel": {
18+
"properties": {"kind": {"enum": ["a"], "title": "Kind", "type": "string"}},
19+
"required": ["kind"],
20+
"title": "DataAModel",
21+
"type": "object",
22+
},
23+
"DataBModel": {
24+
"properties": {"kind": {"enum": ["b"], "title": "Kind", "type": "string"}},
25+
"required": ["kind"],
26+
"title": "DataBModel",
27+
"type": "object",
28+
},
29+
"RequestModel": {
30+
"properties": {
31+
"data": {
32+
"anyOf": [{"$ref": "#/definitions/DataAModel"}, {"$ref": "#/definitions/DataBModel"}],
33+
"discriminator": {
34+
"mapping": {"a": "#/definitions/DataAModel", "b": "#/definitions/DataBModel"},
35+
"propertyName": "kind",
36+
},
37+
"title": "Data",
38+
}
39+
},
40+
"required": ["data"],
41+
"title": "RequestModel",
42+
"type": "object",
43+
},
44+
}
45+
}
46+
47+
48+
def test_pydantic_discriminator_openapi_generation():
49+
"""https://github.com/kuimono/openapi-schema-pydantic/issues/8"""
50+
51+
open_api = construct_open_api_with_schema_class(construct_base_open_api())
52+
json_schema = open_api.components.schemas["RequestModel"]
53+
assert json_schema.properties == {
54+
"data": Schema(
55+
anyOf=[
56+
Reference(ref="#/components/schemas/DataAModel", summary=None, description=None),
57+
Reference(ref="#/components/schemas/DataBModel", summary=None, description=None),
58+
],
59+
title="Data",
60+
discriminator=Discriminator(
61+
propertyName="kind",
62+
mapping={"a": "#/components/schemas/DataAModel", "b": "#/components/schemas/DataBModel"},
63+
),
64+
)
65+
}
66+
67+
68+
def construct_base_open_api() -> OpenAPI:
69+
return OpenAPI(
70+
info=Info(
71+
title="My own API",
72+
version="v0.0.1",
73+
),
74+
paths={
75+
"/ping": PathItem(
76+
post=Operation(
77+
requestBody=RequestBody(
78+
content={
79+
"application/json": MediaType(media_type_schema=PydanticSchema(schema_class=RequestModel))
80+
}
81+
),
82+
responses={"200": Response(description="pong")},
83+
)
84+
)
85+
},
86+
)
87+
88+
89+
class DataAModel(BaseModel):
90+
kind: Literal["a"]
91+
92+
93+
class DataBModel(BaseModel):
94+
kind: Literal["b"]
95+
96+
97+
class RequestModel(BaseModel):
98+
data: Union[DataAModel, DataBModel] = Field(discriminator="kind")
File renamed without changes.

0 commit comments

Comments
 (0)