@@ -23,6 +23,7 @@ def test_get_type_string(no_optional, nullable, required, expected):
2323 nullable = nullable ,
2424 default = None ,
2525 reference = Reference (class_name = "MyClass" , module_name = "my_module" ),
26+ references = [],
2627 description = "" ,
2728 optional_properties = [],
2829 required_properties = [],
@@ -42,6 +43,7 @@ def test_get_imports():
4243 nullable = True ,
4344 default = None ,
4445 reference = Reference (class_name = "MyClass" , module_name = "my_module" ),
46+ references = [],
4547 description = "" ,
4648 optional_properties = [],
4749 required_properties = [],
@@ -57,3 +59,143 @@ def test_get_imports():
5759 "from typing import Dict" ,
5860 "from typing import cast" ,
5961 }
62+
63+
64+ def test_resolve_references (mocker ):
65+ import openapi_python_client .schema as oai
66+ from openapi_python_client .parser .properties import build_model_property
67+
68+ schemas = {
69+ "RefA" : oai .Schema .construct (
70+ title = mocker .MagicMock (),
71+ description = mocker .MagicMock (),
72+ required = ["String" ],
73+ properties = {
74+ "String" : oai .Schema .construct (type = "string" ),
75+ "Enum" : oai .Schema .construct (type = "string" , enum = ["aValue" ]),
76+ "DateTime" : oai .Schema .construct (type = "string" , format = "date-time" ),
77+ },
78+ ),
79+ "RefB" : oai .Schema .construct (
80+ title = mocker .MagicMock (),
81+ description = mocker .MagicMock (),
82+ required = ["DateTime" ],
83+ properties = {
84+ "Int" : oai .Schema .construct (type = "integer" ),
85+ "DateTime" : oai .Schema .construct (type = "string" , format = "date-time" ),
86+ "Float" : oai .Schema .construct (type = "number" , format = "float" ),
87+ },
88+ ),
89+ # Intentionally no properties defined
90+ "RefC" : oai .Schema .construct (
91+ title = mocker .MagicMock (),
92+ description = mocker .MagicMock (),
93+ ),
94+ }
95+
96+ model_schema = oai .Schema .construct (
97+ allOf = [
98+ oai .Reference .construct (ref = "#/components/schemas/RefA" ),
99+ oai .Reference .construct (ref = "#/components/schemas/RefB" ),
100+ oai .Reference .construct (ref = "#/components/schemas/RefC" ),
101+ oai .Schema .construct (
102+ title = mocker .MagicMock (),
103+ description = mocker .MagicMock (),
104+ required = ["Float" ],
105+ properties = {
106+ "String" : oai .Schema .construct (type = "string" ),
107+ "Float" : oai .Schema .construct (type = "number" , format = "float" ),
108+ },
109+ ),
110+ ]
111+ )
112+
113+ components = {** schemas , "Model" : model_schema }
114+
115+ from openapi_python_client .parser .properties import Schemas
116+
117+ schemas_holder = Schemas ()
118+ model , schemas_holder = build_model_property (
119+ data = model_schema , name = "Model" , required = True , schemas = schemas_holder , parent_name = None
120+ )
121+ model .resolve_references (components , schemas_holder )
122+ assert sorted (p .name for p in model .required_properties ) == ["DateTime" , "Float" , "String" ]
123+ assert all (p .required for p in model .required_properties )
124+ assert sorted (p .name for p in model .optional_properties ) == ["Enum" , "Int" ]
125+ assert all (not p .required for p in model .optional_properties )
126+
127+
128+ def test_resolve_references_nested_allof (mocker ):
129+ import openapi_python_client .schema as oai
130+ from openapi_python_client .parser .properties import build_model_property
131+
132+ schemas = {
133+ "RefA" : oai .Schema .construct (
134+ title = mocker .MagicMock (),
135+ description = mocker .MagicMock (),
136+ required = ["String" ],
137+ properties = {
138+ "String" : oai .Schema .construct (type = "string" ),
139+ "Enum" : oai .Schema .construct (type = "string" , enum = ["aValue" ]),
140+ "DateTime" : oai .Schema .construct (type = "string" , format = "date-time" ),
141+ },
142+ ),
143+ "RefB" : oai .Schema .construct (
144+ title = mocker .MagicMock (),
145+ description = mocker .MagicMock (),
146+ required = ["DateTime" ],
147+ properties = {
148+ "Int" : oai .Schema .construct (type = "integer" ),
149+ "DateTime" : oai .Schema .construct (type = "string" , format = "date-time" ),
150+ "Float" : oai .Schema .construct (type = "number" , format = "float" ),
151+ },
152+ ),
153+ # Intentionally no properties defined
154+ "RefC" : oai .Schema .construct (
155+ title = mocker .MagicMock (),
156+ description = mocker .MagicMock (),
157+ ),
158+ }
159+
160+ model_schema = oai .Schema .construct (
161+ type = "object" ,
162+ properties = {
163+ "Key" : oai .Schema .construct (
164+ allOf = [
165+ oai .Reference .construct (ref = "#/components/schemas/RefA" ),
166+ oai .Reference .construct (ref = "#/components/schemas/RefB" ),
167+ oai .Reference .construct (ref = "#/components/schemas/RefC" ),
168+ oai .Schema .construct (
169+ title = mocker .MagicMock (),
170+ description = mocker .MagicMock (),
171+ required = ["Float" ],
172+ properties = {
173+ "String" : oai .Schema .construct (type = "string" ),
174+ "Float" : oai .Schema .construct (type = "number" , format = "float" ),
175+ },
176+ ),
177+ ]
178+ ),
179+ },
180+ )
181+
182+ components = {** schemas , "Model" : model_schema }
183+
184+ from openapi_python_client .parser .properties import ModelProperty , Schemas
185+
186+ schemas_holder = Schemas ()
187+ model , schemas_holder = build_model_property (
188+ data = model_schema , name = "Model" , required = True , schemas = schemas_holder , parent_name = None
189+ )
190+ model .resolve_references (components , schemas_holder )
191+ assert sorted (p .name for p in model .required_properties ) == []
192+ assert sorted (p .name for p in model .optional_properties ) == ["Key" ]
193+ assert all (not p .required for p in model .optional_properties )
194+
195+ key_property = model .optional_properties [0 ]
196+ assert isinstance (key_property , ModelProperty )
197+ key_property .resolve_references (components , schemas_holder )
198+ assert sorted (p .name for p in key_property .required_properties ) == ["DateTime" , "Float" , "String" ]
199+ assert all (p .required for p in key_property .required_properties )
200+ assert sorted (p .name for p in key_property .optional_properties ) == ["Enum" , "Int" ]
201+ assert all (not p .required for p in key_property .optional_properties )
0 commit comments