openapi-spec-tools 0.20.0__tar.gz → 0.21.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/PKG-INFO +1 -1
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/__init__.py +4 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/api_gen/api_generator.py +3 -3
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/api_gen/property_generator.py +24 -53
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/base_generator.py +36 -310
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/api_gen.py +1 -3
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/arguments.py +4 -2
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/cli_gen.py +3 -5
- openapi_spec_tools-0.21.0/openapi_spec_tools/cli/constants.py +6 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/content.py +1 -2
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/layout.py +2 -3
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/models.py +2 -3
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/oas.py +2 -4
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/operations.py +2 -3
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/paths.py +2 -3
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/tags.py +2 -3
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/utils.py +2 -8
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/constants.py +2 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/expand.py +28 -8
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/flatten.py +155 -23
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/layout/layout_generator.py +2 -1
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/types.py +2 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/utils.py +82 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/pyproject.toml +1 -1
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/test_cli_generator.py +11 -24
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/.gitignore +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/LICENSE +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/README.md +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/api_gen/__init__.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/api_gen/_environment.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/api_gen/flat_generator.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/api_gen/opaque_generator.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/__init__.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/_logging.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/_requests.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/config.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/constants.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/files.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/utils.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/__init__.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/analyze.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/history.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/__init__.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/_arguments.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/_display.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/_exceptions.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/_tree.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/cli_generator.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/files.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/layout/__init__.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/layout/config.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/layout/merge.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/layout/types.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/layout/utils.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/__init__.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/test_display.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/test_exceptions.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/test_files.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/test_main.py +0 -0
- {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/test_tree.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: openapi-spec-tools
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.21.0
|
|
4
4
|
Summary: OpenAPI specification tools for analyzing, updating, and generating a CLI.
|
|
5
5
|
Project-URL: Homepage, https://github.com/rickwporter/openapi-spec-tools
|
|
6
6
|
Project-URL: Repository, https://github.com/rickwporter/openapi-spec-tools
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"""Implementation for several utilities for analyzing and modifying OpenAPI specifications."""
|
|
2
2
|
from openapi_spec_tools.expand import expand_schema
|
|
3
3
|
from openapi_spec_tools.expand import merge_schema
|
|
4
|
+
from openapi_spec_tools.flatten import apply_any_of
|
|
5
|
+
from openapi_spec_tools.flatten import apply_one_of
|
|
6
|
+
from openapi_spec_tools.flatten import flatten_collection
|
|
4
7
|
from openapi_spec_tools.flatten import flatten_schema
|
|
8
|
+
from openapi_spec_tools.flatten import is_simple_type
|
|
5
9
|
from openapi_spec_tools.utils import count_values
|
|
6
10
|
from openapi_spec_tools.utils import find_diffs
|
|
7
11
|
from openapi_spec_tools.utils import find_paths
|
{openapi_spec_tools-0.20.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/api_gen/api_generator.py
RENAMED
|
@@ -62,17 +62,17 @@ class ApiGenerator(BaseGenerator, ABC):
|
|
|
62
62
|
def property_help(self, prop: dict[str, Any]) -> str:
|
|
63
63
|
"""Get the short help string for the specified property."""
|
|
64
64
|
help = prop.get(OasField.SUMMARY) or prop.get(OasField.DESCRIPTION) or ""
|
|
65
|
-
short_ref = prop.get(OasField.X_REF)
|
|
65
|
+
short_ref = prop.get(OasField.X_ENUM) or prop.get(OasField.X_REF)
|
|
66
66
|
choices = prop.get(OasField.ENUM)
|
|
67
67
|
required = prop.get(OasField.REQUIRED)
|
|
68
68
|
|
|
69
69
|
if help:
|
|
70
70
|
if len(help) > self.max_help_length:
|
|
71
71
|
help = help.split(". ")[0].strip()[:self.max_help_length] + '...'
|
|
72
|
-
elif short_ref:
|
|
73
|
-
help = f"see {short_ref} for info"
|
|
74
72
|
elif choices:
|
|
75
73
|
help = f"choices: {', '.join([str(_) for _ in choices])}"
|
|
74
|
+
elif short_ref:
|
|
75
|
+
help = f"see {short_ref} for info"
|
|
76
76
|
|
|
77
77
|
if required and "require" not in help.lower():
|
|
78
78
|
if not help.endswith(" "):
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
"""Declares the PropertyApiGenerator class."""
|
|
2
|
-
from copy import deepcopy
|
|
3
2
|
from typing import Any
|
|
4
3
|
|
|
5
4
|
from openapi_spec_tools.api_gen.api_generator import ApiGenerator
|
|
6
5
|
from openapi_spec_tools.base_gen import NL
|
|
7
6
|
from openapi_spec_tools.base_gen import SEP1
|
|
8
7
|
from openapi_spec_tools.base_gen import quoted
|
|
9
|
-
from openapi_spec_tools.
|
|
8
|
+
from openapi_spec_tools.expand import expand_schema
|
|
9
|
+
from openapi_spec_tools.flatten import apply_any_of
|
|
10
|
+
from openapi_spec_tools.flatten import apply_one_of
|
|
11
|
+
from openapi_spec_tools.flatten import flatten_collection
|
|
10
12
|
from openapi_spec_tools.layout import LayoutNode
|
|
11
13
|
from openapi_spec_tools.types import OasField
|
|
12
14
|
|
|
@@ -24,57 +26,26 @@ class PropertyApiGenerator(ApiGenerator):
|
|
|
24
26
|
|
|
25
27
|
def model_properties(self, name: str, schema: dict[str, Any]) -> dict[str, Any]: # noqa: PLR0912
|
|
26
28
|
"""Get a list of settable model properties."""
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
any_of = model.pop(OasField.ANY_OF, None)
|
|
49
|
-
if any_of:
|
|
50
|
-
if len(any_of) != 1:
|
|
51
|
-
self.logger.info(f"Grabbing anyOf[0] item from {name}")
|
|
52
|
-
self.logger.debug(f"{name} anyOf selected: {shallow(any_of[0])}")
|
|
53
|
-
# just grab the first one... not sure this is the best choice, but need to do something
|
|
54
|
-
updated = self.update_one_of(name, deepcopy(any_of[0]))
|
|
55
|
-
updated = self.update_collection(updated)
|
|
56
|
-
updated = self.update_reference(updated)
|
|
57
|
-
model.update(self.update_enum(updated))
|
|
58
|
-
|
|
59
|
-
model = self.update_one_of(name, model)
|
|
60
|
-
model = self.update_collection(model)
|
|
61
|
-
model = self.update_reference(model)
|
|
62
|
-
required_props = model.get(OasField.REQUIRED, [])
|
|
63
|
-
|
|
64
|
-
# copy the individual properties
|
|
65
|
-
for prop_name, _prop_data in model.get(OasField.PROPS, {}).items():
|
|
66
|
-
if _prop_data.get(OasField.READ_ONLY, False):
|
|
67
|
-
continue
|
|
68
|
-
|
|
69
|
-
prop_data = deepcopy(_prop_data)
|
|
70
|
-
prop_data[OasField.REQUIRED.value] = prop_name in required_props
|
|
71
|
-
prop_data = self.update_one_of(prop_name, prop_data)
|
|
72
|
-
prop_data = self.update_collection(prop_data)
|
|
73
|
-
prop_data = self.update_reference(prop_data)
|
|
74
|
-
|
|
75
|
-
body_props[prop_name] = self.update_enum(prop_data)
|
|
76
|
-
|
|
77
|
-
return body_props
|
|
29
|
+
expanded = expand_schema(schema, self.components, name)
|
|
30
|
+
while apply_one_of(expanded) or apply_any_of(expanded):
|
|
31
|
+
pass
|
|
32
|
+
flatten_collection(expanded, force_single=False)
|
|
33
|
+
settable = {
|
|
34
|
+
name: data
|
|
35
|
+
for name, data in expanded.get(OasField.PROPS).items()
|
|
36
|
+
if not data.get(OasField.READ_ONLY, False)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
# update settable fields for easier digestion
|
|
40
|
+
required = expanded.get(OasField.REQUIRED) or []
|
|
41
|
+
for prop_name, prop_data in settable.items():
|
|
42
|
+
while apply_one_of(prop_data) or apply_any_of(prop_data):
|
|
43
|
+
pass
|
|
44
|
+
flatten_collection(prop_data, force_single=False)
|
|
45
|
+
self.update_enum(prop_data)
|
|
46
|
+
prop_data[OasField.REQUIRED.value] = bool(prop_name in required)
|
|
47
|
+
|
|
48
|
+
return settable
|
|
78
49
|
|
|
79
50
|
def op_body_formation(self, properties: dict[str, Any]) -> str:
|
|
80
51
|
"""Create body parameter and poulates it when there are body paramters."""
|
|
@@ -24,17 +24,17 @@ from openapi_spec_tools.base_gen.constants import SEP1
|
|
|
24
24
|
from openapi_spec_tools.base_gen.constants import SEP2
|
|
25
25
|
from openapi_spec_tools.base_gen.files import copy_and_update
|
|
26
26
|
from openapi_spec_tools.base_gen.utils import maybe_quoted
|
|
27
|
-
from openapi_spec_tools.base_gen.utils import prepend
|
|
28
27
|
from openapi_spec_tools.base_gen.utils import quoted
|
|
29
28
|
from openapi_spec_tools.base_gen.utils import replace_special
|
|
30
|
-
from openapi_spec_tools.base_gen.utils import set_missing
|
|
31
|
-
from openapi_spec_tools.base_gen.utils import shallow
|
|
32
29
|
from openapi_spec_tools.base_gen.utils import simple_escape
|
|
33
30
|
from openapi_spec_tools.base_gen.utils import to_camel_case
|
|
34
31
|
from openapi_spec_tools.base_gen.utils import to_snake_case
|
|
32
|
+
from openapi_spec_tools.constants import NULL_TYPES
|
|
33
|
+
from openapi_spec_tools.expand import expand_schema
|
|
34
|
+
from openapi_spec_tools.flatten import flatten_schema
|
|
35
|
+
from openapi_spec_tools.flatten import is_simple_type
|
|
35
36
|
from openapi_spec_tools.types import ContentType
|
|
36
37
|
from openapi_spec_tools.types import OasField
|
|
37
|
-
from openapi_spec_tools.utils import NULL_TYPES
|
|
38
38
|
from openapi_spec_tools.utils import _or
|
|
39
39
|
from openapi_spec_tools.utils import map_operations
|
|
40
40
|
from openapi_spec_tools.utils import reference_classname
|
|
@@ -245,76 +245,6 @@ class BaseGenerator:
|
|
|
245
245
|
|
|
246
246
|
return value
|
|
247
247
|
|
|
248
|
-
def update_reference(self, prop: dict[str, Any]) -> dict[str, Any]:
|
|
249
|
-
"""Update a property's reference."""
|
|
250
|
-
reference = prop.get(OasField.REFS, "")
|
|
251
|
-
|
|
252
|
-
# this helps with CloudTruth where the role's are based on an enum reference inside an 'allOf'
|
|
253
|
-
all_of = prop.get(OasField.ALL_OF, [])
|
|
254
|
-
if not reference and len(all_of) == 1:
|
|
255
|
-
item = all_of[0]
|
|
256
|
-
reference = item.get(OasField.REFS)
|
|
257
|
-
|
|
258
|
-
if reference:
|
|
259
|
-
prop[OasField.X_REF.value] = reference_classname(reference)
|
|
260
|
-
|
|
261
|
-
# resolve references, if they're enums
|
|
262
|
-
sub_model = self.get_model(reference)
|
|
263
|
-
if sub_model:
|
|
264
|
-
prop.update(sub_model)
|
|
265
|
-
|
|
266
|
-
return prop
|
|
267
|
-
|
|
268
|
-
def update_collection(self, prop: dict[str, Any]) -> dict[str, Any]:
|
|
269
|
-
"""Update the collection information."""
|
|
270
|
-
collection_type = self.model_collection_type(prop)
|
|
271
|
-
if collection_type:
|
|
272
|
-
item_model = prop.pop(OasField.ITEMS, {})
|
|
273
|
-
prop[OasField.X_COLLECT.value] = collection_type
|
|
274
|
-
prop.update(item_model)
|
|
275
|
-
|
|
276
|
-
return prop
|
|
277
|
-
|
|
278
|
-
def model_is_complex(self, model: dict[str, Any]) -> bool:
|
|
279
|
-
"""Determine if the model is complex, such that it would not work well with a list.
|
|
280
|
-
|
|
281
|
-
Basically, anything with more than one property is considered complex. This logic is
|
|
282
|
-
not perfect -- it does not expand everything (or wait for "final" answers), but is
|
|
283
|
-
good enough in most cases.
|
|
284
|
-
"""
|
|
285
|
-
total_prop_count = 0
|
|
286
|
-
for prop_data in model.get(OasField.PROPS, {}).values():
|
|
287
|
-
if prop_data.get(OasField.READ_ONLY):
|
|
288
|
-
continue
|
|
289
|
-
|
|
290
|
-
reference = prop_data.get(OasField.REFS)
|
|
291
|
-
if not reference:
|
|
292
|
-
total_prop_count += 1
|
|
293
|
-
if reference:
|
|
294
|
-
submodel = self.get_model(reference)
|
|
295
|
-
if self.model_is_complex(submodel):
|
|
296
|
-
return True
|
|
297
|
-
sub_props = submodel.get(OasField.PROPS, {})
|
|
298
|
-
total_prop_count += len(sub_props)
|
|
299
|
-
|
|
300
|
-
if total_prop_count > 1:
|
|
301
|
-
return True
|
|
302
|
-
|
|
303
|
-
for inherited in model.get(OasField.ALL_OF, []):
|
|
304
|
-
properties = inherited.get(OasField.PROPS, {})
|
|
305
|
-
total_prop_count += len(properties)
|
|
306
|
-
if total_prop_count > 1:
|
|
307
|
-
return True
|
|
308
|
-
|
|
309
|
-
reference = inherited.get(OasField.REFS)
|
|
310
|
-
submodel = self.get_model(reference)
|
|
311
|
-
properties = submodel.get(OasField.PROPS, {})
|
|
312
|
-
total_prop_count += len(properties)
|
|
313
|
-
if total_prop_count > 1:
|
|
314
|
-
return True
|
|
315
|
-
|
|
316
|
-
return False
|
|
317
|
-
|
|
318
248
|
def op_url_params(self, path: str) -> str:
|
|
319
249
|
"""Parse the X-PATH to list the parameters that go into the URL formation."""
|
|
320
250
|
parts = path.split("/")
|
|
@@ -357,165 +287,19 @@ class BaseGenerator:
|
|
|
357
287
|
|
|
358
288
|
return None
|
|
359
289
|
|
|
360
|
-
def model_collection_type(self, model: str) -> str | None:
|
|
361
|
-
"""Determine the collection type (current just an array)."""
|
|
362
|
-
model_type = self.simplify_type(model.get(OasField.TYPE))
|
|
363
|
-
if model_type in COLLECTIONS.keys():
|
|
364
|
-
return model_type
|
|
365
|
-
|
|
366
|
-
for parent in model.get(OasField.ALL_OF) or model.get(OasField.ANY_OF) or []:
|
|
367
|
-
reference = parent.get(OasField.REFS, "")
|
|
368
|
-
if not reference:
|
|
369
|
-
submodel = parent
|
|
370
|
-
else:
|
|
371
|
-
submodel = self.get_model(reference)
|
|
372
|
-
# recursively search through submodels
|
|
373
|
-
sub_collection = self.model_collection_type(submodel)
|
|
374
|
-
if sub_collection:
|
|
375
|
-
return sub_collection
|
|
376
|
-
|
|
377
|
-
return None
|
|
378
|
-
|
|
379
|
-
def expand_references(self, model: dict[str, Any]) -> dict[str, Any]:
|
|
380
|
-
"""Expand all the references.
|
|
381
|
-
|
|
382
|
-
This is a brute force method to recursively look for any `$ref` keys, and update
|
|
383
|
-
those dictionaries in place.
|
|
384
|
-
"""
|
|
385
|
-
# start at this level
|
|
386
|
-
updated = deepcopy(model)
|
|
387
|
-
|
|
388
|
-
full_ref = model.get(OasField.REFS)
|
|
389
|
-
if full_ref:
|
|
390
|
-
updated[OasField.X_REF.value] = reference_classname(full_ref)
|
|
391
|
-
submodel = self.get_model(full_ref)
|
|
392
|
-
if not submodel:
|
|
393
|
-
self.logger.warning(f"Unable to find model for {full_ref}")
|
|
394
|
-
return {}
|
|
395
|
-
|
|
396
|
-
updated.update(submodel)
|
|
397
|
-
|
|
398
|
-
# then, loop thru all the sub-items
|
|
399
|
-
result = {}
|
|
400
|
-
for key, value in updated.items():
|
|
401
|
-
if isinstance(value, dict):
|
|
402
|
-
# recursively update
|
|
403
|
-
resolved = self.expand_references(value)
|
|
404
|
-
if resolved:
|
|
405
|
-
result[key] = resolved
|
|
406
|
-
elif isinstance(value, list):
|
|
407
|
-
items = [
|
|
408
|
-
self.expand_references(v) if isinstance(v, dict) else v
|
|
409
|
-
for v in value
|
|
410
|
-
]
|
|
411
|
-
if items:
|
|
412
|
-
result[key] = items
|
|
413
|
-
else:
|
|
414
|
-
result[key] = value
|
|
415
|
-
|
|
416
|
-
return result
|
|
417
|
-
|
|
418
|
-
def expanded_settable_properties(self, name: str, model: dict[str, Any]) -> dict[str, Any]:
|
|
419
|
-
"""Turn an expanded model (all references expanded) into a dictionary of properties."""
|
|
420
|
-
properties = {}
|
|
421
|
-
|
|
422
|
-
# start with the base-classes in allOf
|
|
423
|
-
for index, parent in enumerate(model.get(OasField.ALL_OF, [])):
|
|
424
|
-
required_sub = parent.get(OasField.REQUIRED, [])
|
|
425
|
-
reference = parent.get(OasField.REFS, "")
|
|
426
|
-
short_refname = reference_classname(reference)
|
|
427
|
-
sub_properties = self.expanded_settable_properties(f"{name}.anyOf[{index}]", parent)
|
|
428
|
-
for sub_name, sub_data in sub_properties.items():
|
|
429
|
-
if short_refname:
|
|
430
|
-
set_missing(sub_data, OasField.X_REF.value, short_refname)
|
|
431
|
-
set_missing(sub_data, OasField.X_FIELD.value, sub_name)
|
|
432
|
-
sub_data[OasField.REQUIRED.value] = sub_data.get(OasField.REQUIRED.value) and sub_name in required_sub
|
|
433
|
-
properties[sub_name] = sub_data
|
|
434
|
-
|
|
435
|
-
any_of = model.get(OasField.ANY_OF)
|
|
436
|
-
if any_of:
|
|
437
|
-
if len(any_of) != 1:
|
|
438
|
-
self.logger.info(f"Grabbing anyOf[0] item from {name}")
|
|
439
|
-
self.logger.debug(f"{name} anyOf selected: {shallow(any_of[0])}")
|
|
440
|
-
# just grab the first one... not sure this is the best choice, but need to do something
|
|
441
|
-
model.update(any_of[0])
|
|
442
|
-
|
|
443
|
-
self.update_one_of(name, model)
|
|
444
|
-
|
|
445
|
-
reference = model.get(OasField.REFS, "")
|
|
446
|
-
short_refname = reference_classname(reference)
|
|
447
|
-
required_props = model.get(OasField.REQUIRED, [])
|
|
448
|
-
ref_model = self.get_model(reference)
|
|
449
|
-
if ref_model:
|
|
450
|
-
# update the non-existing fields in the model
|
|
451
|
-
set_missing(model, OasField.X_REF.value, short_refname)
|
|
452
|
-
for k, v in ref_model.items():
|
|
453
|
-
set_missing(model, k, v)
|
|
454
|
-
|
|
455
|
-
# copy the individual properties
|
|
456
|
-
for prop_name, prop_data in model.get(OasField.PROPS, {}).items():
|
|
457
|
-
if prop_data.get(OasField.READ_ONLY, False):
|
|
458
|
-
continue
|
|
459
|
-
|
|
460
|
-
collection_type = self.model_collection_type(prop_data)
|
|
461
|
-
if collection_type:
|
|
462
|
-
collect_name = f"{short_refname}." if short_refname else "" + prop_name
|
|
463
|
-
item_model = prop_data.get(OasField.ITEMS, {})
|
|
464
|
-
if not item_model:
|
|
465
|
-
self.logger.error(f"Could not find {collect_name} item model")
|
|
466
|
-
continue
|
|
467
|
-
if self.model_is_complex(item_model):
|
|
468
|
-
self.logger.warning(f"Treating complex collection of {collect_name} as a single item")
|
|
469
|
-
prop_data.pop(OasField.ITEMS.value, None)
|
|
470
|
-
prop_data.update(item_model)
|
|
471
|
-
prop_data[OasField.X_SINGLE.value] = True
|
|
472
|
-
else:
|
|
473
|
-
prop_data.pop(OasField.ITEMS.value, None)
|
|
474
|
-
prop_data[OasField.X_COLLECT.value] = collection_type
|
|
475
|
-
prop_data.update(item_model)
|
|
476
|
-
|
|
477
|
-
required_sub = prop_data.get(OasField.REQUIRED, [])
|
|
478
|
-
sub_properties = self.expanded_settable_properties(f"{name}.{prop_name}", prop_data)
|
|
479
|
-
if not sub_properties:
|
|
480
|
-
# kind of a corner case where an enum has no properties
|
|
481
|
-
for key in (OasField.ALL_OF, OasField.ANY_OF, OasField.ONE_OF):
|
|
482
|
-
items = prop_data.pop(key, None)
|
|
483
|
-
if not items:
|
|
484
|
-
continue
|
|
485
|
-
prop_data.update(items[0])
|
|
486
|
-
|
|
487
|
-
pytype = self.get_property_pytype(prop_name, prop_data)
|
|
488
|
-
if not pytype:
|
|
489
|
-
self.logger.warning(f"Unable to determine pytype for {name}.{prop_name}")
|
|
490
|
-
continue
|
|
491
|
-
|
|
492
|
-
if short_refname:
|
|
493
|
-
set_missing(prop_data, OasField.X_REF.value, short_refname)
|
|
494
|
-
prop_data[OasField.REQUIRED.value] = prop_name in required_props
|
|
495
|
-
properties[prop_name] = prop_data
|
|
496
|
-
continue
|
|
497
|
-
|
|
498
|
-
forced_single = prop_data.get(OasField.X_SINGLE, False)
|
|
499
|
-
for sub_name, sub_data in sub_properties.items():
|
|
500
|
-
# these properties are "name mangled" to include the parent property name
|
|
501
|
-
full_name = f"{prop_name}.{sub_name}"
|
|
502
|
-
sub_data[OasField.REQUIRED.value] = prop_name in required_props and sub_name in required_sub
|
|
503
|
-
if reference:
|
|
504
|
-
set_missing(sub_data, OasField.X_REF.value, reference_classname(reference))
|
|
505
|
-
set_missing(sub_data, OasField.X_FIELD.value, sub_name)
|
|
506
|
-
prepend(sub_data, OasField.X_PARENTS.value, prop_name)
|
|
507
|
-
if forced_single:
|
|
508
|
-
# propagate to sub-fields, since there's nothing for the parent
|
|
509
|
-
sub_data[OasField.X_SINGLE.value] = True
|
|
510
|
-
properties[full_name] = sub_data
|
|
511
|
-
|
|
512
|
-
return properties
|
|
513
|
-
|
|
514
290
|
def model_settable_properties(self, name: str, model: dict[str, Any]) -> dict[str, Any]:
|
|
515
291
|
"""Expand the model into a dictionary of properties."""
|
|
516
|
-
expanded = self.
|
|
292
|
+
expanded = expand_schema(model, self.components, name)
|
|
293
|
+
flattened = flatten_schema(expanded, name)
|
|
517
294
|
|
|
518
|
-
|
|
295
|
+
# filter out read-only properties, and complex properties that CLI cannot deal with
|
|
296
|
+
settable = {
|
|
297
|
+
name: data
|
|
298
|
+
for name, data in flattened.get(OasField.PROPS, {}).items()
|
|
299
|
+
if not data.get(OasField.READ_ONLY, False) and is_simple_type(data)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return settable
|
|
519
303
|
|
|
520
304
|
def op_body_schema(self, operation: dict[str, Any]) -> tuple[str, dict[str, Any]]:
|
|
521
305
|
"""Get the name and schema dictionary."""
|
|
@@ -580,11 +364,12 @@ class BaseGenerator:
|
|
|
580
364
|
reduced = set(schema) - NULL_TYPES
|
|
581
365
|
if len(reduced) == 1:
|
|
582
366
|
return reduced.pop()
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
367
|
+
|
|
368
|
+
# find first item from schema this is a non-null type
|
|
369
|
+
item = next((item for item in schema if item in reduced), None)
|
|
370
|
+
if item is not None:
|
|
371
|
+
self.logger.debug(f"Choosing {item} type from {', '.join(schema)}")
|
|
372
|
+
return item
|
|
588
373
|
|
|
589
374
|
self.logger.warning(f"Unable to simplify type for {schema}")
|
|
590
375
|
return None
|
|
@@ -603,7 +388,7 @@ class BaseGenerator:
|
|
|
603
388
|
"""
|
|
604
389
|
values = param_data.get(OasField.ENUM)
|
|
605
390
|
if values:
|
|
606
|
-
name = param_data.get(OasField.
|
|
391
|
+
name = param_data.get(OasField.X_ENUM) or param_data.get(OasField.NAME)
|
|
607
392
|
return self.class_name(name)
|
|
608
393
|
|
|
609
394
|
py_type = self.schema_to_pytype(param_data)
|
|
@@ -666,82 +451,26 @@ class BaseGenerator:
|
|
|
666
451
|
params.append(item)
|
|
667
452
|
return params
|
|
668
453
|
|
|
669
|
-
def condense_one_of(self, one_of: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
670
|
-
"""Remove "duplicate" collection elements, and adds X_COLLECT to the schema."""
|
|
671
|
-
condensed = []
|
|
672
|
-
for outer in one_of:
|
|
673
|
-
item = deepcopy(outer)
|
|
674
|
-
found = False
|
|
675
|
-
for inner in one_of:
|
|
676
|
-
if item.get(OasField.ITEMS) == inner:
|
|
677
|
-
found = True
|
|
678
|
-
break
|
|
679
|
-
if inner.get(OasField.ITEMS) == item:
|
|
680
|
-
item[OasField.X_COLLECT.value] = inner.get(OasField.TYPE)
|
|
681
|
-
if not found:
|
|
682
|
-
condensed.append(item)
|
|
683
|
-
|
|
684
|
-
return condensed
|
|
685
|
-
|
|
686
|
-
def update_one_of(self, name: str, prop: dict[str, Any]) -> dict[str, Any]:
|
|
687
|
-
"""Update the property when oneOf is present."""
|
|
688
|
-
one_of = prop.pop(OasField.ONE_OF, None)
|
|
689
|
-
if one_of:
|
|
690
|
-
updated = self.condense_one_of(one_of)
|
|
691
|
-
if len(updated) != 1:
|
|
692
|
-
self.logger.info(f"Grabbing oneOf[0] item from {name}")
|
|
693
|
-
self.logger.debug(f"{name} oneOf selected: {shallow(updated[0])}")
|
|
694
|
-
|
|
695
|
-
# just grab the first one... not sure this is the best choice, but need to do something
|
|
696
|
-
updated = self.update_collection(deepcopy(updated[0]))
|
|
697
|
-
updated = self.update_reference(updated)
|
|
698
|
-
prop.update(self.update_enum(updated))
|
|
699
|
-
|
|
700
|
-
return prop
|
|
701
|
-
|
|
702
454
|
def param_to_property(self, param: dict[str, Any]) -> dict[str, Any]:
|
|
703
455
|
"""Convert parameter data to property data.
|
|
704
456
|
|
|
705
457
|
Resolves parameter data to make it easier to digest (e.g. choosing any oneOf,
|
|
706
458
|
collection information, required).
|
|
707
459
|
"""
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
460
|
+
name = param.get(OasField.NAME) or ""
|
|
461
|
+
expanded = expand_schema(param, self.components, name)
|
|
462
|
+
flattened = flatten_schema(expanded, name)
|
|
463
|
+
self.update_enum(flattened)
|
|
711
464
|
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
465
|
+
# flatten the type, too
|
|
466
|
+
types = flattened.get(OasField.TYPE)
|
|
467
|
+
if types and isinstance(types, list) and not NULL_TYPES.isdisjoint(types):
|
|
468
|
+
# if the item is nullable, then remove the null type(s) and set required=False
|
|
469
|
+
types = [t for t in types if t not in NULL_TYPES]
|
|
470
|
+
flattened[OasField.TYPE.value] = types[0] if len(types) == 1 else types
|
|
471
|
+
flattened[OasField.REQUIRED.value] = False
|
|
717
472
|
|
|
718
|
-
|
|
719
|
-
schema_type = prop.get(OasField.TYPE)
|
|
720
|
-
if isinstance(schema_type, list):
|
|
721
|
-
nullable = any(nt in schema_type for nt in NULL_TYPES)
|
|
722
|
-
schema_list = [v for v in schema_type if v not in NULL_TYPES]
|
|
723
|
-
nullable = schema_type != schema_list
|
|
724
|
-
if len(schema_list) == 1:
|
|
725
|
-
schema_type = schema_list[0]
|
|
726
|
-
else:
|
|
727
|
-
schema_type = schema_list
|
|
728
|
-
prop[OasField.TYPE.value] = schema_type
|
|
729
|
-
|
|
730
|
-
if isinstance(schema_type, str) and schema_type in COLLECTIONS.keys():
|
|
731
|
-
items = prop.pop(OasField.ITEMS, {})
|
|
732
|
-
prop.update(items)
|
|
733
|
-
prop[OasField.X_COLLECT.value] = schema_type
|
|
734
|
-
schema_type = items.get(OasField.TYPE)
|
|
735
|
-
|
|
736
|
-
self.update_enum(prop)
|
|
737
|
-
|
|
738
|
-
schema = self.simplify_type(prop)
|
|
739
|
-
if schema:
|
|
740
|
-
prop.update(schema)
|
|
741
|
-
if nullable:
|
|
742
|
-
prop[OasField.REQUIRED.value] = False
|
|
743
|
-
|
|
744
|
-
return prop
|
|
473
|
+
return flattened
|
|
745
474
|
|
|
746
475
|
def params_to_settable_properties(self, parameters: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
747
476
|
"""Get a dictionary of settable parameter properties.
|
|
@@ -835,13 +564,10 @@ class BaseGenerator:
|
|
|
835
564
|
dep_msg = f"{option} is deprecated and should not be used"
|
|
836
565
|
|
|
837
566
|
obj_name = "body"
|
|
838
|
-
field = prop_name
|
|
567
|
+
field = prop_data.get(OasField.X_FIELD) or prop_name
|
|
839
568
|
parents = prop_data.get(OasField.X_PARENTS)
|
|
840
569
|
if parents:
|
|
841
570
|
obj_name = self.variable_name(parents[-1])
|
|
842
|
-
x_field = prop_data.get(OasField.X_FIELD)
|
|
843
|
-
if x_field:
|
|
844
|
-
field = x_field
|
|
845
571
|
if prop_data.get(OasField.REQUIRED):
|
|
846
572
|
lines.append(f'{obj_name}["{field}"] = {var_name}')
|
|
847
573
|
else:
|
|
@@ -989,7 +715,7 @@ class BaseGenerator:
|
|
|
989
715
|
if not values:
|
|
990
716
|
continue
|
|
991
717
|
|
|
992
|
-
e_name = param_data.get(OasField.
|
|
718
|
+
e_name = param_data.get(OasField.X_ENUM) or param_data.get(OasField.NAME)
|
|
993
719
|
e_type = self.schema_to_pytype(param_data) or 'str'
|
|
994
720
|
enums[self.class_name(e_name)] = (e_type, values)
|
|
995
721
|
|
|
@@ -997,7 +723,7 @@ class BaseGenerator:
|
|
|
997
723
|
values = prop.get(OasField.ENUM)
|
|
998
724
|
if not values:
|
|
999
725
|
continue
|
|
1000
|
-
e_name = prop.get(OasField.
|
|
726
|
+
e_name = prop.get(OasField.X_ENUM) or name
|
|
1001
727
|
e_type = self.schema_to_pytype(prop) or 'str'
|
|
1002
728
|
enums[self.class_name(e_name)] = (e_type, values)
|
|
1003
729
|
|
|
@@ -21,12 +21,10 @@ from openapi_spec_tools.cli.utils import gen_config_from_file
|
|
|
21
21
|
from openapi_spec_tools.cli.utils import init_logging
|
|
22
22
|
from openapi_spec_tools.cli.utils import layout_tree_with_error_handling
|
|
23
23
|
from openapi_spec_tools.cli.utils import open_oas_with_error_handling
|
|
24
|
+
from openapi_spec_tools.constants import LOG_CLASS
|
|
24
25
|
from openapi_spec_tools.layout import DEFAULT_START
|
|
25
26
|
from openapi_spec_tools.layout import LayoutGenerator
|
|
26
27
|
|
|
27
|
-
SEP = "\n "
|
|
28
|
-
|
|
29
|
-
LOG_CLASS = "api-gen"
|
|
30
28
|
|
|
31
29
|
class BodyType(str, Enum):
|
|
32
30
|
"""Different body types for API generated code."""
|
|
@@ -6,8 +6,9 @@ import typer
|
|
|
6
6
|
from rich_objects import OutputFormat
|
|
7
7
|
from rich_objects import OutputStyle
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
from openapi_spec_tools.cli.constants import DIRECTORY
|
|
10
|
+
from openapi_spec_tools.cli.constants import FILENAME
|
|
11
|
+
|
|
11
12
|
|
|
12
13
|
class LogLevel(str, Enum):
|
|
13
14
|
"""Log levels."""
|
|
@@ -73,6 +74,7 @@ LogLevelOption = Annotated[
|
|
|
73
74
|
typer.Option(
|
|
74
75
|
"--log",
|
|
75
76
|
case_sensitive=False,
|
|
77
|
+
envvar="LOG_LEVEL",
|
|
76
78
|
help="Log level",
|
|
77
79
|
),
|
|
78
80
|
]
|
|
@@ -20,6 +20,8 @@ from openapi_spec_tools.cli.arguments import PackageNameOption
|
|
|
20
20
|
from openapi_spec_tools.cli.arguments import PathPrefixOption
|
|
21
21
|
from openapi_spec_tools.cli.arguments import StartPointOption
|
|
22
22
|
from openapi_spec_tools.cli.arguments import UpdatedOpenApiFilenameOption
|
|
23
|
+
from openapi_spec_tools.cli.constants import DIRECTORY
|
|
24
|
+
from openapi_spec_tools.cli.constants import SEP
|
|
23
25
|
from openapi_spec_tools.cli.utils import gen_config_from_file
|
|
24
26
|
from openapi_spec_tools.cli.utils import init_logging
|
|
25
27
|
from openapi_spec_tools.cli.utils import layout_tree_with_error_handling
|
|
@@ -31,6 +33,7 @@ from openapi_spec_tools.cli_gen import check_for_missing
|
|
|
31
33
|
from openapi_spec_tools.cli_gen import create_tree_table
|
|
32
34
|
from openapi_spec_tools.cli_gen import find_unreferenced
|
|
33
35
|
from openapi_spec_tools.cli_gen import generate_tree_node
|
|
36
|
+
from openapi_spec_tools.constants import LOG_CLASS
|
|
34
37
|
from openapi_spec_tools.layout import DEFAULT_START
|
|
35
38
|
from openapi_spec_tools.layout import LayoutGenerator
|
|
36
39
|
from openapi_spec_tools.layout import LayoutNode
|
|
@@ -42,11 +45,6 @@ from openapi_spec_tools.utils import remove_schema_tags
|
|
|
42
45
|
from openapi_spec_tools.utils import schema_operations_filter
|
|
43
46
|
from openapi_spec_tools.utils import set_nullable_not_required
|
|
44
47
|
|
|
45
|
-
SEP = "\n "
|
|
46
|
-
LOG_CLASS = "cli-gen"
|
|
47
|
-
FILENAME = "FILENAME"
|
|
48
|
-
DIRECTORY = "DIRECTORY"
|
|
49
|
-
|
|
50
48
|
#################################################
|
|
51
49
|
# Utilities
|
|
52
50
|
#################################################
|
|
@@ -9,10 +9,9 @@ from openapi_spec_tools.cli.arguments import LogLevelOption
|
|
|
9
9
|
from openapi_spec_tools.cli.arguments import OpenApiFilenameArgument
|
|
10
10
|
from openapi_spec_tools.cli.utils import init_logging
|
|
11
11
|
from openapi_spec_tools.cli.utils import open_oas_with_error_handling
|
|
12
|
+
from openapi_spec_tools.constants import LOG_CLASS
|
|
12
13
|
from openapi_spec_tools.utils import map_content_types
|
|
13
14
|
|
|
14
|
-
LOG_CLASS = "content"
|
|
15
|
-
|
|
16
15
|
app = typer.Typer(no_args_is_help=True, short_help="Inspect things related to content-types")
|
|
17
16
|
|
|
18
17
|
|