1
- from typing_extensions import Literal
2
1
from typing import Union
3
2
4
3
from pydantic import BaseModel , Field
5
4
from pydantic .schema import schema
5
+ from typing_extensions import Literal
6
6
7
- from openapi_schema_pydantic import (
8
- OpenAPI ,
7
+ from openapi_pydantic import (
8
+ Discriminator ,
9
9
Info ,
10
- PathItem ,
10
+ MediaType ,
11
+ OpenAPI ,
11
12
Operation ,
13
+ PathItem ,
14
+ Reference ,
12
15
RequestBody ,
13
- MediaType ,
14
16
Response ,
15
17
Schema ,
16
- Reference ,
17
- Discriminator ,
18
18
)
19
- from openapi_schema_pydantic .util import PydanticSchema , construct_open_api_with_schema_class
19
+ from openapi_pydantic .util import PydanticSchema , construct_open_api_with_schema_class
20
+
21
+
22
+ class DataAModel (BaseModel ):
23
+ kind : Literal ["a" ]
24
+
25
+
26
+ class DataBModel (BaseModel ):
27
+ kind : Literal ["b" ]
28
+
29
+
30
+ class RequestModel (BaseModel ):
31
+ data : Union [DataAModel , DataBModel ] = Field (discriminator = "kind" )
32
+
33
+
34
+ def construct_base_open_api () -> OpenAPI :
35
+ return OpenAPI (
36
+ info = Info (
37
+ title = "My own API" ,
38
+ version = "v0.0.1" ,
39
+ ),
40
+ paths = {
41
+ "/ping" : PathItem (
42
+ post = Operation (
43
+ requestBody = RequestBody (
44
+ content = {
45
+ "application/json" : MediaType (
46
+ media_type_schema = PydanticSchema (
47
+ schema_class = RequestModel
48
+ )
49
+ )
50
+ }
51
+ ),
52
+ responses = {"200" : Response (description = "pong" )},
53
+ )
54
+ )
55
+ },
56
+ )
20
57
21
58
22
59
def test_pydantic_discriminator_schema_generation () -> None :
@@ -26,23 +63,33 @@ def test_pydantic_discriminator_schema_generation() -> None:
26
63
assert json_schema == {
27
64
"definitions" : {
28
65
"DataAModel" : {
29
- "properties" : {"kind" : {"enum" : ["a" ], "title" : "Kind" , "type" : "string" }},
66
+ "properties" : {
67
+ "kind" : {"enum" : ["a" ], "title" : "Kind" , "type" : "string" }
68
+ },
30
69
"required" : ["kind" ],
31
70
"title" : "DataAModel" ,
32
71
"type" : "object" ,
33
72
},
34
73
"DataBModel" : {
35
- "properties" : {"kind" : {"enum" : ["b" ], "title" : "Kind" , "type" : "string" }},
74
+ "properties" : {
75
+ "kind" : {"enum" : ["b" ], "title" : "Kind" , "type" : "string" }
76
+ },
36
77
"required" : ["kind" ],
37
78
"title" : "DataBModel" ,
38
79
"type" : "object" ,
39
80
},
40
81
"RequestModel" : {
41
82
"properties" : {
42
83
"data" : {
43
- "anyOf" : [{"$ref" : "#/definitions/DataAModel" }, {"$ref" : "#/definitions/DataBModel" }],
84
+ "oneOf" : [
85
+ {"$ref" : "#/definitions/DataAModel" },
86
+ {"$ref" : "#/definitions/DataBModel" },
87
+ ],
44
88
"discriminator" : {
45
- "mapping" : {"a" : "#/definitions/DataAModel" , "b" : "#/definitions/DataBModel" },
89
+ "mapping" : {
90
+ "a" : "#/definitions/DataAModel" ,
91
+ "b" : "#/definitions/DataBModel" ,
92
+ },
46
93
"propertyName" : "kind" ,
47
94
},
48
95
"title" : "Data" ,
@@ -65,47 +112,25 @@ def test_pydantic_discriminator_openapi_generation() -> None:
65
112
json_schema = open_api .components .schemas ["RequestModel" ]
66
113
assert json_schema .properties == {
67
114
"data" : Schema (
68
- anyOf = [
69
- Reference (ref = "#/components/schemas/DataAModel" , summary = None , description = None ),
70
- Reference (ref = "#/components/schemas/DataBModel" , summary = None , description = None ),
115
+ oneOf = [
116
+ Reference (
117
+ ref = "#/components/schemas/DataAModel" ,
118
+ summary = None ,
119
+ description = None ,
120
+ ),
121
+ Reference (
122
+ ref = "#/components/schemas/DataBModel" ,
123
+ summary = None ,
124
+ description = None ,
125
+ ),
71
126
],
72
127
title = "Data" ,
73
128
discriminator = Discriminator (
74
129
propertyName = "kind" ,
75
- mapping = {"a" : "#/components/schemas/DataAModel" , "b" : "#/components/schemas/DataBModel" },
130
+ mapping = {
131
+ "a" : "#/components/schemas/DataAModel" ,
132
+ "b" : "#/components/schemas/DataBModel" ,
133
+ },
76
134
),
77
135
)
78
136
}
79
-
80
-
81
- def construct_base_open_api () -> OpenAPI :
82
- return OpenAPI (
83
- info = Info (
84
- title = "My own API" ,
85
- version = "v0.0.1" ,
86
- ),
87
- paths = {
88
- "/ping" : PathItem (
89
- post = Operation (
90
- requestBody = RequestBody (
91
- content = {
92
- "application/json" : MediaType (media_type_schema = PydanticSchema (schema_class = RequestModel ))
93
- }
94
- ),
95
- responses = {"200" : Response (description = "pong" )},
96
- )
97
- )
98
- },
99
- )
100
-
101
-
102
- class DataAModel (BaseModel ):
103
- kind : Literal ["a" ]
104
-
105
-
106
- class DataBModel (BaseModel ):
107
- kind : Literal ["b" ]
108
-
109
-
110
- class RequestModel (BaseModel ):
111
- data : Union [DataAModel , DataBModel ] = Field (discriminator = "kind" )
0 commit comments