Skip to content

Commit faba196

Browse files
committed
Rename Schema class to Model to disambiguate with OpenAPI term
1 parent c4bc824 commit faba196

File tree

6 files changed

+55
-53
lines changed

6 files changed

+55
-53
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
78
## 0.5.0 - Unreleased
89
### Internal Changes
910
- Switched OpenAPI document parsing to use
1011
[openapi-schema-pydantic](https://github.com/kuimono/openapi-schema-pydantic/pull/1) (#103)
1112

13+
1214
## 0.4.2 - 2020-06-13
1315
### Additions
1416
- Support for responses with no content (#63 & #66). Thanks @acgray!

openapi_python_client/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ def _build_models(self) -> None:
170170
types_path.write_text(types_template.render())
171171

172172
model_template = self.env.get_template("model.pyi")
173-
for schema in self.openapi.schemas.values():
174-
module_path = models_dir / f"{schema.reference.module_name}.py"
175-
module_path.write_text(model_template.render(schema=schema))
176-
imports.append(import_string_from_reference(schema.reference))
173+
for model in self.openapi.models.values():
174+
module_path = models_dir / f"{model.reference.module_name}.py"
175+
module_path.write_text(model_template.render(model=model))
176+
imports.append(import_string_from_reference(model.reference))
177177

178178
# Generate enums
179179
enum_template = self.env.get_template("enum.pyi")

openapi_python_client/openapi_parser/openapi.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ def from_data(*, data: oai.Operation, path: str, method: str, tag: str) -> Endpo
167167

168168

169169
@dataclass
170-
class Schema:
170+
class Model:
171171
"""
172-
Describes a schema, AKA data model used in requests.
172+
A data model used by the API- usually a Schema with type "object".
173173
174174
These will all be converted to dataclasses in the client
175175
"""
@@ -181,8 +181,8 @@ class Schema:
181181
relative_imports: Set[str]
182182

183183
@staticmethod
184-
def from_data(*, data: Union[oai.Reference, oai.Schema], name: str) -> Schema:
185-
""" A single Schema from its OAI data
184+
def from_data(*, data: Union[oai.Reference, oai.Schema], name: str) -> Model:
185+
""" A single Model from its OAI data
186186
187187
Args:
188188
data: Data of a single Schema
@@ -207,21 +207,21 @@ def from_data(*, data: Union[oai.Reference, oai.Schema], name: str) -> Schema:
207207
optional_properties.append(p)
208208
relative_imports.update(p.get_imports(prefix=""))
209209

210-
schema = Schema(
210+
model = Model(
211211
reference=ref,
212212
required_properties=required_properties,
213213
optional_properties=optional_properties,
214214
relative_imports=relative_imports,
215215
description=data.description or "",
216216
)
217-
return schema
217+
return model
218218

219219
@staticmethod
220-
def build(*, schemas: Dict[str, Union[oai.Reference, oai.Schema]]) -> Dict[str, Schema]:
220+
def build(*, schemas: Dict[str, Union[oai.Reference, oai.Schema]]) -> Dict[str, Model]:
221221
""" Get a list of Schemas from an OpenAPI dict """
222222
result = {}
223223
for name, data in schemas.items():
224-
s = Schema.from_data(data=data, name=name)
224+
s = Model.from_data(data=data, name=name)
225225
result[s.reference.class_name] = s
226226
return result
227227

@@ -233,7 +233,7 @@ class GeneratorData:
233233
title: str
234234
description: Optional[str]
235235
version: str
236-
schemas: Dict[str, Schema]
236+
models: Dict[str, Model]
237237
endpoint_collections_by_tag: Dict[str, EndpointCollection]
238238
enums: Dict[str, EnumProperty]
239239

@@ -242,9 +242,9 @@ def from_dict(d: Dict[str, Dict[str, Any]]) -> GeneratorData:
242242
""" Create an OpenAPI from dict """
243243
openapi = oai.OpenAPI.parse_obj(d)
244244
if openapi.components is None or openapi.components.schemas is None:
245-
schemas = {}
245+
models = {}
246246
else:
247-
schemas = Schema.build(schemas=openapi.components.schemas)
247+
models = Model.build(schemas=openapi.components.schemas)
248248
endpoint_collections_by_tag = EndpointCollection.from_data(data=openapi.paths)
249249
enums = EnumProperty.get_all_enums()
250250

@@ -253,6 +253,6 @@ def from_dict(d: Dict[str, Dict[str, Any]]) -> GeneratorData:
253253
description=openapi.info.description,
254254
version=openapi.info.version,
255255
endpoint_collections_by_tag=endpoint_collections_by_tag,
256-
schemas=schemas,
256+
models=models,
257257
enums=enums,
258258
)

openapi_python_client/templates/model.pyi

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ from __future__ import annotations
33
from dataclasses import dataclass
44
from typing import Any, Dict
55

6-
{% for relative in schema.relative_imports %}
6+
{% for relative in model.relative_imports %}
77
{{ relative }}
88
{% endfor %}
99

1010

1111
@dataclass
12-
class {{ schema.reference.class_name }}:
13-
""" {{ schema.description }} """
14-
{% for property in schema.required_properties + schema.optional_properties %}
12+
class {{ model.reference.class_name }}:
13+
""" {{ model.description }} """
14+
{% for property in model.required_properties + model.optional_properties %}
1515
{{ property.to_string() }}
1616
{% endfor %}
1717

1818
def to_dict(self) -> Dict[str, Any]:
19-
{% for property in schema.required_properties + schema.optional_properties %}
19+
{% for property in model.required_properties + model.optional_properties %}
2020
{% if property.template %}
2121
{% from "property_templates/" + property.template import transform %}
2222
{{ transform(property, "self." + property.python_name, property.python_name) | indent(8) }}
@@ -26,14 +26,14 @@ class {{ schema.reference.class_name }}:
2626
{% endfor %}
2727

2828
return {
29-
{% for property in schema.required_properties + schema.optional_properties %}
29+
{% for property in model.required_properties + model.optional_properties %}
3030
"{{ property.name }}": {{ property.python_name }},
3131
{% endfor %}
3232
}
3333

3434
@staticmethod
35-
def from_dict(d: Dict[str, Any]) -> {{ schema.reference.class_name }}:
36-
{% for property in schema.required_properties + schema.optional_properties %}
35+
def from_dict(d: Dict[str, Any]) -> {{ model.reference.class_name }}:
36+
{% for property in model.required_properties + model.optional_properties %}
3737
{% if property.required %}
3838
{% set property_source = 'd["' + property.name + '"]' %}
3939
{% else %}
@@ -47,8 +47,8 @@ class {{ schema.reference.class_name }}:
4747
{% endif %}
4848

4949
{% endfor %}
50-
return {{ schema.reference.class_name }}(
51-
{% for property in schema.required_properties + schema.optional_properties %}
50+
return {{ model.reference.class_name }}(
51+
{% for property in model.required_properties + model.optional_properties %}
5252
{{ property.python_name }}={{ property.python_name }},
5353
{% endfor %}
5454
)

tests/test___init__.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ def test__build_models(self, mocker):
260260
from openapi_python_client import GeneratorData, _Project
261261

262262
openapi = mocker.MagicMock(autospec=GeneratorData, title="My Test API")
263-
schema_1 = mocker.MagicMock()
264-
schema_2 = mocker.MagicMock()
265-
openapi.schemas = {"1": schema_1, "2": schema_2}
263+
model_1 = mocker.MagicMock()
264+
model_2 = mocker.MagicMock()
265+
openapi.models = {"1": model_1, "2": model_2}
266266
enum_1 = mocker.MagicMock()
267267
enum_2 = mocker.MagicMock()
268268
openapi.enums = {"1": enum_1, "2": enum_2}
@@ -271,15 +271,15 @@ def test__build_models(self, mocker):
271271
models_init = mocker.MagicMock()
272272
types_py = mocker.MagicMock()
273273
models_dir = mocker.MagicMock()
274-
schema_1_module_path = mocker.MagicMock()
275-
schema_2_module_path = mocker.MagicMock()
274+
model_1_module_path = mocker.MagicMock()
275+
model_2_module_path = mocker.MagicMock()
276276
enum_1_module_path = mocker.MagicMock()
277277
enum_2_module_path = mocker.MagicMock()
278278
module_paths = {
279279
"__init__.py": models_init,
280280
"types.py": types_py,
281-
f"{schema_1.reference.module_name}.py": schema_1_module_path,
282-
f"{schema_2.reference.module_name}.py": schema_2_module_path,
281+
f"{model_1.reference.module_name}.py": model_1_module_path,
282+
f"{model_2.reference.module_name}.py": model_2_module_path,
283283
f"{enum_1.reference.module_name}.py": enum_1_module_path,
284284
f"{enum_2.reference.module_name}.py": enum_2_module_path,
285285
}
@@ -327,13 +327,13 @@ def models_dir_get(x):
327327
models_dir.mkdir.assert_called_once()
328328
models_dir.__truediv__.assert_has_calls([mocker.call(key) for key in module_paths])
329329
project.env.get_template.assert_has_calls([mocker.call(key) for key in templates])
330-
model_template.render.assert_has_calls([mocker.call(schema=schema_1), mocker.call(schema=schema_2)])
331-
schema_1_module_path.write_text.assert_called_once_with(model_render_1)
332-
schema_2_module_path.write_text.assert_called_once_with(model_render_2)
330+
model_template.render.assert_has_calls([mocker.call(model=model_1), mocker.call(model=model_2)])
331+
model_1_module_path.write_text.assert_called_once_with(model_render_1)
332+
model_2_module_path.write_text.assert_called_once_with(model_render_2)
333333
import_string_from_reference.assert_has_calls(
334334
[
335-
mocker.call(schema_1.reference),
336-
mocker.call(schema_2.reference),
335+
mocker.call(model_1.reference),
336+
mocker.call(model_2.reference),
337337
mocker.call(enum_1.reference),
338338
mocker.call(enum_2.reference),
339339
]

tests/test_openapi_parser/test_openapi.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class TestGeneratorData:
1010
def test_from_dict(self, mocker):
11-
Schema = mocker.patch(f"{MODULE_NAME}.Schema")
11+
Model = mocker.patch(f"{MODULE_NAME}.Model")
1212
EndpointCollection = mocker.patch(f"{MODULE_NAME}.EndpointCollection")
1313
OpenAPI = mocker.patch(f"{MODULE_NAME}.oai.OpenAPI")
1414
openapi = OpenAPI.parse_obj.return_value
@@ -21,39 +21,39 @@ def test_from_dict(self, mocker):
2121
generator_data = GeneratorData.from_dict(in_dict)
2222

2323
OpenAPI.parse_obj.assert_called_once_with(in_dict)
24-
Schema.build.assert_called_once_with(schemas=openapi.components.schemas)
24+
Model.build.assert_called_once_with(schemas=openapi.components.schemas)
2525
EndpointCollection.from_data.assert_called_once_with(data=openapi.paths)
2626
get_all_enums.assert_called_once_with()
2727
assert generator_data == GeneratorData(
2828
title=openapi.info.title,
2929
description=openapi.info.description,
3030
version=openapi.info.version,
3131
endpoint_collections_by_tag=EndpointCollection.from_data.return_value,
32-
schemas=Schema.build.return_value,
32+
models=Model.build.return_value,
3333
enums=get_all_enums.return_value,
3434
)
3535

3636
# Test no components
3737
openapi.components = None
38-
Schema.build.reset_mock()
38+
Model.build.reset_mock()
3939

4040
generator_data = GeneratorData.from_dict(in_dict)
4141

42-
Schema.build.assert_not_called()
43-
assert generator_data.schemas == {}
42+
Model.build.assert_not_called()
43+
assert generator_data.models == {}
4444

4545

46-
class TestSchema:
46+
class TestModel:
4747
def test_build(self, mocker):
48-
from_data = mocker.patch(f"{MODULE_NAME}.Schema.from_data")
48+
from_data = mocker.patch(f"{MODULE_NAME}.Model.from_data")
4949
in_data = {1: mocker.MagicMock(), 2: mocker.MagicMock()}
5050
schema_1 = mocker.MagicMock()
5151
schema_2 = mocker.MagicMock()
5252
from_data.side_effect = [schema_1, schema_2]
5353

54-
from openapi_python_client.openapi_parser.openapi import Schema
54+
from openapi_python_client.openapi_parser.openapi import Model
5555

56-
result = Schema.build(schemas=in_data)
56+
result = Model.build(schemas=in_data)
5757

5858
from_data.assert_has_calls([mocker.call(data=value, name=name) for (name, value) in in_data.items()])
5959
assert result == {
@@ -81,9 +81,9 @@ def test_from_data(self, mocker):
8181
)
8282
from_ref = mocker.patch(f"{MODULE_NAME}.Reference.from_ref")
8383

84-
from openapi_python_client.openapi_parser.openapi import Schema
84+
from openapi_python_client.openapi_parser.openapi import Model
8585

86-
result = Schema.from_data(data=in_data, name=mocker.MagicMock())
86+
result = Model.from_data(data=in_data, name=mocker.MagicMock())
8787

8888
from_ref.assert_called_once_with(in_data.title)
8989
property_from_data.assert_has_calls(
@@ -94,7 +94,7 @@ def test_from_data(self, mocker):
9494
)
9595
required_property.get_imports.assert_called_once_with(prefix="")
9696
optional_property.get_imports.assert_called_once_with(prefix="")
97-
assert result == Schema(
97+
assert result == Model(
9898
reference=from_ref(),
9999
required_properties=[required_property],
100100
optional_properties=[optional_property],
@@ -103,10 +103,10 @@ def test_from_data(self, mocker):
103103
)
104104

105105
def test_from_data_parse_error_on_reference(self):
106-
from openapi_python_client.openapi_parser.openapi import Schema
106+
from openapi_python_client.openapi_parser.openapi import Model
107107

108108
with pytest.raises(ParseError):
109-
Schema.from_data(data=oai.Reference.construct(), name="")
109+
Model.from_data(data=oai.Reference.construct(), name="")
110110

111111

112112
class TestEndpoint:

0 commit comments

Comments
 (0)