openapi-spec-tools 0.19.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.
Files changed (62) hide show
  1. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/.gitignore +4 -0
  2. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/PKG-INFO +2 -2
  3. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/__init__.py +7 -0
  4. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/api_gen/api_generator.py +7 -7
  5. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/api_gen/property_generator.py +24 -53
  6. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/_requests.py +1 -1
  7. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/base_generator.py +39 -316
  8. openapi_spec_tools-0.21.0/openapi_spec_tools/cli/analyze.py +16 -0
  9. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/api_gen.py +1 -3
  10. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/arguments.py +23 -2
  11. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/cli_gen.py +3 -5
  12. openapi_spec_tools-0.21.0/openapi_spec_tools/cli/constants.py +6 -0
  13. openapi_spec_tools-0.21.0/openapi_spec_tools/cli/content.py +43 -0
  14. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/history.py +1 -1
  15. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/layout.py +2 -3
  16. openapi_spec_tools-0.21.0/openapi_spec_tools/cli/models.py +185 -0
  17. openapi_spec_tools-0.21.0/openapi_spec_tools/cli/oas.py +210 -0
  18. openapi_spec_tools-0.21.0/openapi_spec_tools/cli/operations.py +276 -0
  19. openapi_spec_tools-0.21.0/openapi_spec_tools/cli/paths.py +118 -0
  20. openapi_spec_tools-0.21.0/openapi_spec_tools/cli/tags.py +88 -0
  21. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/utils.py +57 -11
  22. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/_tree.py +0 -2
  23. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/cli_generator.py +27 -16
  24. openapi_spec_tools-0.21.0/openapi_spec_tools/constants.py +7 -0
  25. openapi_spec_tools-0.21.0/openapi_spec_tools/expand.py +124 -0
  26. openapi_spec_tools-0.21.0/openapi_spec_tools/flatten.py +435 -0
  27. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/layout/layout_generator.py +7 -10
  28. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/types.py +12 -0
  29. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/utils.py +99 -8
  30. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/pyproject.toml +12 -3
  31. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/test_cli_generator.py +26 -30
  32. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/test_files.py +4 -2
  33. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/test_main.py +1 -1
  34. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/test_tree.py +1 -1
  35. openapi_spec_tools-0.19.0/openapi_spec_tools/cli/oas.py +0 -712
  36. openapi_spec_tools-0.19.0/tests/cli_gen/helpers.py +0 -11
  37. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/LICENSE +0 -0
  38. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/README.md +0 -0
  39. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/api_gen/__init__.py +0 -0
  40. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/api_gen/_environment.py +0 -0
  41. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/api_gen/flat_generator.py +0 -0
  42. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/api_gen/opaque_generator.py +0 -0
  43. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/__init__.py +0 -0
  44. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/_logging.py +0 -0
  45. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/config.py +0 -0
  46. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/constants.py +0 -0
  47. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/files.py +0 -0
  48. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/base_gen/utils.py +0 -0
  49. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli/__init__.py +0 -0
  50. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/__init__.py +0 -0
  51. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/_arguments.py +0 -0
  52. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/_display.py +0 -0
  53. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/_exceptions.py +0 -0
  54. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/cli_gen/files.py +0 -0
  55. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/layout/__init__.py +0 -0
  56. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/layout/config.py +0 -0
  57. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/layout/merge.py +0 -0
  58. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/layout/types.py +0 -0
  59. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/openapi_spec_tools/layout/utils.py +0 -0
  60. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/__init__.py +0 -0
  61. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/test_display.py +0 -0
  62. {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.21.0}/tests/cli_gen/test_exceptions.py +0 -0
@@ -169,3 +169,7 @@ cython_debug/
169
169
 
170
170
  # PyPI configuration file
171
171
  .pypirc
172
+
173
+ # Pets test server output from the OpenAPI generator.
174
+ # Rebuilt with `make gen` in tools/pets_server; do not commit.
175
+ tools/pets_server/generated/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: openapi-spec-tools
3
- Version: 0.19.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
@@ -29,7 +29,7 @@ Requires-Dist: gitpython>=3.2.0
29
29
  Requires-Dist: pydantic<3,>=2.13.3
30
30
  Requires-Dist: pyyaml>=6.0.3
31
31
  Requires-Dist: requests>=2.34.2
32
- Requires-Dist: rich-objects>=0.3.0
32
+ Requires-Dist: rich-objects>=0.3.1
33
33
  Requires-Dist: rich>=15.0.0
34
34
  Requires-Dist: typer>=0.27.2
35
35
  Description-Content-Type: text/markdown
@@ -1,4 +1,11 @@
1
1
  """Implementation for several utilities for analyzing and modifying OpenAPI specifications."""
2
+ from openapi_spec_tools.expand import expand_schema
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
7
+ from openapi_spec_tools.flatten import flatten_schema
8
+ from openapi_spec_tools.flatten import is_simple_type
2
9
  from openapi_spec_tools.utils import count_values
3
10
  from openapi_spec_tools.utils import find_diffs
4
11
  from openapi_spec_tools.utils import find_paths
@@ -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(" "):
@@ -101,9 +101,9 @@ from datetime import datetime # noqa: F401
101
101
  from enum import Enum # noqa: F401
102
102
  from typing import Any
103
103
 
104
- from {self.package_name} import _environment as _e # noqa: F401
105
- from {self.package_name} import _logging as _l # noqa: F401
106
- from {self.package_name} import _requests as _r # noqa: F401
104
+ from {self.package_name} import _environment as _e
105
+ from {self.package_name} import _logging as _l
106
+ from {self.package_name} import _requests as _r
107
107
  """
108
108
 
109
109
  def init_infra_args(self, operation: dict[str, Any]) -> str:
@@ -161,7 +161,7 @@ from {self.package_name} import _requests as _r # noqa: F401
161
161
  if allow_required and required and schema_default is None:
162
162
  arg_default = ""
163
163
  else:
164
- if not required:
164
+ if not required or schema_default is None:
165
165
  py_type = f"{py_type} | None"
166
166
  if schema_default is None:
167
167
  arg_default = " = None"
@@ -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.base_gen import shallow
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
- model = deepcopy(schema)
28
- body_props = {}
29
-
30
- # start with the base-classes in allOf
31
- for parent in model.get(OasField.ALL_OF, []):
32
- reference = parent.get(OasField.REFS, "")
33
- if reference:
34
- sub_model = self.get_model(reference)
35
- else:
36
- sub_model = deepcopy(parent)
37
- required_sub = sub_model.get(OasField.REQUIRED, [])
38
- for sub_name, _sub_data in sub_model.get(OasField.PROPS, {}).items():
39
- if _sub_data.get(OasField.READ_ONLY):
40
- continue
41
-
42
- sub_data = self.update_one_of(sub_name, deepcopy(_sub_data))
43
- sub_data = self.update_collection(sub_data)
44
- sub_data = self.update_reference(sub_data)
45
- sub_data[OasField.REQUIRED.value] = sub_data.get(OasField.REQUIRED) or sub_name in required_sub
46
- body_props[sub_name] = self.update_enum(sub_data)
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."""
@@ -192,7 +192,7 @@ def request(
192
192
  try:
193
193
  content = response.content.decode(encoding=encoding, errors="ignore")
194
194
  return yaml.safe_load(content)
195
- except Exception as ex:
195
+ except Exception as ex: # noqa: BLE001
196
196
  logger.error(f"Failed to decode {method} {pretty_url} response: {ex}")
197
197
  return None
198
198
 
@@ -24,19 +24,20 @@ 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
+ from openapi_spec_tools.utils import reference_classname
40
41
 
41
42
  LOG_CLASS = "base-gen"
42
43
 
@@ -184,10 +185,6 @@ class BaseGenerator:
184
185
  value = to_snake_case(replace_special(s))
185
186
  return "--" + value.rstrip("_").replace("_", "-")
186
187
 
187
- def short_reference_name(self, full_name: str) -> str:
188
- """Transform the '#/components/schemas/Xxx' to 'Xxx'."""
189
- return full_name.rsplit('/', maxsplit=1)[-1]
190
-
191
188
  def shebang(self) -> str:
192
189
  """Get the shebang line that goes at the top of each file."""
193
190
  return "#!/usr/bin/env python3\n"
@@ -248,76 +245,6 @@ class BaseGenerator:
248
245
 
249
246
  return value
250
247
 
251
- def update_reference(self, prop: dict[str, Any]) -> dict[str, Any]:
252
- """Update a property's reference."""
253
- reference = prop.get(OasField.REFS, "")
254
-
255
- # this helps with CloudTruth where the role's are based on an enum reference inside an 'allOf'
256
- all_of = prop.get(OasField.ALL_OF, [])
257
- if not reference and len(all_of) == 1:
258
- item = all_of[0]
259
- reference = item.get(OasField.REFS)
260
-
261
- if reference:
262
- prop[OasField.X_REF.value] = self.short_reference_name(reference)
263
-
264
- # resolve references, if they're enums
265
- sub_model = self.get_model(reference)
266
- if sub_model:
267
- prop.update(sub_model)
268
-
269
- return prop
270
-
271
- def update_collection(self, prop: dict[str, Any]) -> dict[str, Any]:
272
- """Update the collection information."""
273
- collection_type = self.model_collection_type(prop)
274
- if collection_type:
275
- item_model = prop.pop(OasField.ITEMS, {})
276
- prop[OasField.X_COLLECT.value] = collection_type
277
- prop.update(item_model)
278
-
279
- return prop
280
-
281
- def model_is_complex(self, model: dict[str, Any]) -> bool:
282
- """Determine if the model is complex, such that it would not work well with a list.
283
-
284
- Basically, anything with more than one property is considered complex. This logic is
285
- not perfect -- it does not expand everything (or wait for "final" answers), but is
286
- good enough in most cases.
287
- """
288
- total_prop_count = 0
289
- for prop_data in model.get(OasField.PROPS, {}).values():
290
- if prop_data.get(OasField.READ_ONLY):
291
- continue
292
-
293
- reference = prop_data.get(OasField.REFS)
294
- if not reference:
295
- total_prop_count += 1
296
- if reference:
297
- submodel = self.get_model(reference)
298
- if self.model_is_complex(submodel):
299
- return True
300
- sub_props = submodel.get(OasField.PROPS, {})
301
- total_prop_count += len(sub_props)
302
-
303
- if total_prop_count > 1:
304
- return True
305
-
306
- for inherited in model.get(OasField.ALL_OF, []):
307
- properties = inherited.get(OasField.PROPS, {})
308
- total_prop_count += len(properties)
309
- if total_prop_count > 1:
310
- return True
311
-
312
- reference = inherited.get(OasField.REFS)
313
- submodel = self.get_model(reference)
314
- properties = submodel.get(OasField.PROPS, {})
315
- total_prop_count += len(properties)
316
- if total_prop_count > 1:
317
- return True
318
-
319
- return False
320
-
321
248
  def op_url_params(self, path: str) -> str:
322
249
  """Parse the X-PATH to list the parameters that go into the URL formation."""
323
250
  parts = path.split("/")
@@ -360,165 +287,19 @@ class BaseGenerator:
360
287
 
361
288
  return None
362
289
 
363
- def model_collection_type(self, model: str) -> str | None:
364
- """Determine the collection type (current just an array)."""
365
- model_type = self.simplify_type(model.get(OasField.TYPE))
366
- if model_type in COLLECTIONS.keys():
367
- return model_type
368
-
369
- for parent in model.get(OasField.ALL_OF) or model.get(OasField.ANY_OF) or []:
370
- reference = parent.get(OasField.REFS, "")
371
- if not reference:
372
- submodel = parent
373
- else:
374
- submodel = self.get_model(reference)
375
- # recursively search through submodels
376
- sub_collection = self.model_collection_type(submodel)
377
- if sub_collection:
378
- return sub_collection
379
-
380
- return None
381
-
382
- def expand_references(self, model: dict[str, Any]) -> dict[str, Any]:
383
- """Expand all the references.
384
-
385
- This is a brute force method to recursively look for any `$ref` keys, and update
386
- those dictionaries in place.
387
- """
388
- # start at this level
389
- updated = deepcopy(model)
390
-
391
- full_ref = model.get(OasField.REFS)
392
- if full_ref:
393
- updated[OasField.X_REF.value] = self.short_reference_name(full_ref)
394
- submodel = self.get_model(full_ref)
395
- if not submodel:
396
- self.logger.warning(f"Unable to find model for {full_ref}")
397
- return {}
398
-
399
- updated.update(submodel)
400
-
401
- # then, loop thru all the sub-items
402
- result = {}
403
- for key, value in updated.items():
404
- if isinstance(value, dict):
405
- # recursively update
406
- resolved = self.expand_references(value)
407
- if resolved:
408
- result[key] = resolved
409
- elif isinstance(value, list):
410
- items = [
411
- self.expand_references(v) if isinstance(v, dict) else v
412
- for v in value
413
- ]
414
- if items:
415
- result[key] = items
416
- else:
417
- result[key] = value
418
-
419
- return result
420
-
421
- def expanded_settable_properties(self, name: str, model: dict[str, Any]) -> dict[str, Any]:
422
- """Turn an expanded model (all references expanded) into a dictionary of properties."""
423
- properties = {}
424
-
425
- # start with the base-classes in allOf
426
- for index, parent in enumerate(model.get(OasField.ALL_OF, [])):
427
- required_sub = parent.get(OasField.REQUIRED, [])
428
- reference = parent.get(OasField.REFS, "")
429
- short_refname = self.short_reference_name(reference)
430
- sub_properties = self.expanded_settable_properties(f"{name}.anyOf[{index}]", parent)
431
- for sub_name, sub_data in sub_properties.items():
432
- if short_refname:
433
- set_missing(sub_data, OasField.X_REF.value, short_refname)
434
- set_missing(sub_data, OasField.X_FIELD.value, sub_name)
435
- sub_data[OasField.REQUIRED.value] = sub_data.get(OasField.REQUIRED.value) and sub_name in required_sub
436
- properties[sub_name] = sub_data
437
-
438
- any_of = model.get(OasField.ANY_OF)
439
- if any_of:
440
- if len(any_of) != 1:
441
- self.logger.info(f"Grabbing anyOf[0] item from {name}")
442
- self.logger.debug(f"{name} anyOf selected: {shallow(any_of[0])}")
443
- # just grab the first one... not sure this is the best choice, but need to do something
444
- model.update(any_of[0])
445
-
446
- self.update_one_of(name, model)
447
-
448
- reference = model.get(OasField.REFS, "")
449
- short_refname = self.short_reference_name(reference)
450
- required_props = model.get(OasField.REQUIRED, [])
451
- ref_model = self.get_model(reference)
452
- if ref_model:
453
- # update the non-existing fields in the model
454
- set_missing(model, OasField.X_REF.value, short_refname)
455
- for k, v in ref_model.items():
456
- set_missing(model, k, v)
457
-
458
- # copy the individual properties
459
- for prop_name, prop_data in model.get(OasField.PROPS, {}).items():
460
- if prop_data.get(OasField.READ_ONLY, False):
461
- continue
462
-
463
- collection_type = self.model_collection_type(prop_data)
464
- if collection_type:
465
- collect_name = f"{short_refname}." if short_refname else "" + prop_name
466
- item_model = prop_data.get(OasField.ITEMS, {})
467
- if not item_model:
468
- self.logger.error(f"Could not find {collect_name} item model")
469
- continue
470
- if self.model_is_complex(item_model):
471
- self.logger.warning(f"Treating complex collection of {collect_name} as a single item")
472
- prop_data.pop(OasField.ITEMS.value, None)
473
- prop_data.update(item_model)
474
- prop_data[OasField.X_SINGLE.value] = True
475
- else:
476
- prop_data.pop(OasField.ITEMS.value, None)
477
- prop_data[OasField.X_COLLECT.value] = collection_type
478
- prop_data.update(item_model)
479
-
480
- required_sub = prop_data.get(OasField.REQUIRED, [])
481
- sub_properties = self.expanded_settable_properties(f"{name}.{prop_name}", prop_data)
482
- if not sub_properties:
483
- # kind of a corner case where an enum has no properties
484
- for key in (OasField.ALL_OF, OasField.ANY_OF, OasField.ONE_OF):
485
- items = prop_data.pop(key, None)
486
- if not items:
487
- continue
488
- prop_data.update(items[0])
489
-
490
- pytype = self.get_property_pytype(prop_name, prop_data)
491
- if not pytype:
492
- self.logger.warning(f"Unable to determine pytype for {name}.{prop_name}")
493
- continue
494
-
495
- if short_refname:
496
- set_missing(prop_data, OasField.X_REF.value, short_refname)
497
- prop_data[OasField.REQUIRED.value] = prop_name in required_props
498
- properties[prop_name] = prop_data
499
- continue
500
-
501
- forced_single = prop_data.get(OasField.X_SINGLE, False)
502
- for sub_name, sub_data in sub_properties.items():
503
- # these properties are "name mangled" to include the parent property name
504
- full_name = f"{prop_name}.{sub_name}"
505
- sub_data[OasField.REQUIRED.value] = prop_name in required_props and sub_name in required_sub
506
- if reference:
507
- set_missing(sub_data, OasField.X_REF.value, self.short_reference_name(reference))
508
- set_missing(sub_data, OasField.X_FIELD.value, sub_name)
509
- prepend(sub_data, OasField.X_PARENTS.value, prop_name)
510
- if forced_single:
511
- # propagate to sub-fields, since there's nothing for the parent
512
- sub_data[OasField.X_SINGLE.value] = True
513
- properties[full_name] = sub_data
514
-
515
- return properties
516
-
517
290
  def model_settable_properties(self, name: str, model: dict[str, Any]) -> dict[str, Any]:
518
291
  """Expand the model into a dictionary of properties."""
519
- expanded = self.expand_references(model)
292
+ expanded = expand_schema(model, self.components, name)
293
+ flattened = flatten_schema(expanded, name)
294
+
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
+ }
520
301
 
521
- return self.expanded_settable_properties(name, expanded)
302
+ return settable
522
303
 
523
304
  def op_body_schema(self, operation: dict[str, Any]) -> tuple[str, dict[str, Any]]:
524
305
  """Get the name and schema dictionary."""
@@ -530,7 +311,7 @@ class BaseGenerator:
530
311
  name = "body"
531
312
  ref = schema.get(OasField.REFS)
532
313
  if ref:
533
- name = self.short_reference_name(ref)
314
+ name = reference_classname(ref)
534
315
  schema = self.get_model(ref)
535
316
 
536
317
  return (name, schema)
@@ -583,11 +364,12 @@ class BaseGenerator:
583
364
  reduced = set(schema) - NULL_TYPES
584
365
  if len(reduced) == 1:
585
366
  return reduced.pop()
586
- # loop through to find the items from the ordered schema
587
- for item in schema:
588
- if item in reduced:
589
- self.logger.debug(f"Choosing {item} type from {', '.join(schema)}")
590
- return item
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
591
373
 
592
374
  self.logger.warning(f"Unable to simplify type for {schema}")
593
375
  return None
@@ -606,7 +388,7 @@ class BaseGenerator:
606
388
  """
607
389
  values = param_data.get(OasField.ENUM)
608
390
  if values:
609
- name = param_data.get(OasField.X_REF) or param_data.get(OasField.NAME)
391
+ name = param_data.get(OasField.X_ENUM) or param_data.get(OasField.NAME)
610
392
  return self.class_name(name)
611
393
 
612
394
  py_type = self.schema_to_pytype(param_data)
@@ -659,7 +441,7 @@ class BaseGenerator:
659
441
  model = self.get_model(ref)
660
442
  if model:
661
443
  item = deepcopy(model)
662
- item[OasField.X_REF] = self.short_reference_name(ref)
444
+ item[OasField.X_REF] = reference_classname(ref)
663
445
  if item.get(OasField.IN) != location:
664
446
  continue
665
447
 
@@ -669,82 +451,26 @@ class BaseGenerator:
669
451
  params.append(item)
670
452
  return params
671
453
 
672
- def condense_one_of(self, one_of: list[dict[str, Any]]) -> list[dict[str, Any]]:
673
- """Remove "duplicate" collection elements, and adds X_COLLECT to the schema."""
674
- condensed = []
675
- for outer in one_of:
676
- item = deepcopy(outer)
677
- found = False
678
- for inner in one_of:
679
- if item.get(OasField.ITEMS) == inner:
680
- found = True
681
- break
682
- if inner.get(OasField.ITEMS) == item:
683
- item[OasField.X_COLLECT.value] = inner.get(OasField.TYPE)
684
- if not found:
685
- condensed.append(item)
686
-
687
- return condensed
688
-
689
- def update_one_of(self, name: str, prop: dict[str, Any]) -> dict[str, Any]:
690
- """Update the property when oneOf is present."""
691
- one_of = prop.pop(OasField.ONE_OF, None)
692
- if one_of:
693
- updated = self.condense_one_of(one_of)
694
- if len(updated) != 1:
695
- self.logger.info(f"Grabbing oneOf[0] item from {name}")
696
- self.logger.debug(f"{name} oneOf selected: {shallow(updated[0])}")
697
-
698
- # just grab the first one... not sure this is the best choice, but need to do something
699
- updated = self.update_collection(deepcopy(updated[0]))
700
- updated = self.update_reference(updated)
701
- prop.update(self.update_enum(updated))
702
-
703
- return prop
704
-
705
454
  def param_to_property(self, param: dict[str, Any]) -> dict[str, Any]:
706
455
  """Convert parameter data to property data.
707
456
 
708
457
  Resolves parameter data to make it easier to digest (e.g. choosing any oneOf,
709
458
  collection information, required).
710
459
  """
711
- prop = deepcopy(param)
712
- name = prop.get(OasField.NAME)
713
- prop = self.update_one_of(name, prop)
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)
714
464
 
715
- any_of = prop.pop(OasField.ANY_OF, [])
716
- if any_of:
717
- # just grab the first one...
718
- self.logger.warning(f"Grabbing anyOf[0] item from {shallow(any_of[0])}")
719
- prop.update(any_of[0])
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
720
472
 
721
- nullable = False
722
- schema_type = prop.get(OasField.TYPE)
723
- if isinstance(schema_type, list):
724
- nullable = any(nt in schema_type for nt in NULL_TYPES)
725
- schema_list = [v for v in schema_type if v not in NULL_TYPES]
726
- nullable = schema_type != schema_list
727
- if len(schema_list) == 1:
728
- schema_type = schema_list[0]
729
- else:
730
- schema_type = schema_list
731
- prop[OasField.TYPE.value] = schema_type
732
-
733
- if isinstance(schema_type, str) and schema_type in COLLECTIONS.keys():
734
- items = prop.pop(OasField.ITEMS, {})
735
- prop.update(items)
736
- prop[OasField.X_COLLECT.value] = schema_type
737
- schema_type = items.get(OasField.TYPE)
738
-
739
- self.update_enum(prop)
740
-
741
- schema = self.simplify_type(prop)
742
- if schema:
743
- prop.update(schema)
744
- if nullable:
745
- prop[OasField.REQUIRED.value] = False
746
-
747
- return prop
473
+ return flattened
748
474
 
749
475
  def params_to_settable_properties(self, parameters: list[dict[str, Any]]) -> list[dict[str, Any]]:
750
476
  """Get a dictionary of settable parameter properties.
@@ -838,13 +564,10 @@ class BaseGenerator:
838
564
  dep_msg = f"{option} is deprecated and should not be used"
839
565
 
840
566
  obj_name = "body"
841
- field = prop_name
567
+ field = prop_data.get(OasField.X_FIELD) or prop_name
842
568
  parents = prop_data.get(OasField.X_PARENTS)
843
569
  if parents:
844
570
  obj_name = self.variable_name(parents[-1])
845
- x_field = prop_data.get(OasField.X_FIELD)
846
- if x_field:
847
- field = x_field
848
571
  if prop_data.get(OasField.REQUIRED):
849
572
  lines.append(f'{obj_name}["{field}"] = {var_name}')
850
573
  else:
@@ -992,7 +715,7 @@ class BaseGenerator:
992
715
  if not values:
993
716
  continue
994
717
 
995
- e_name = param_data.get(OasField.X_REF) or param_data.get(OasField.NAME)
718
+ e_name = param_data.get(OasField.X_ENUM) or param_data.get(OasField.NAME)
996
719
  e_type = self.schema_to_pytype(param_data) or 'str'
997
720
  enums[self.class_name(e_name)] = (e_type, values)
998
721
 
@@ -1000,7 +723,7 @@ class BaseGenerator:
1000
723
  values = prop.get(OasField.ENUM)
1001
724
  if not values:
1002
725
  continue
1003
- e_name = prop.get(OasField.X_REF) or name
726
+ e_name = prop.get(OasField.X_ENUM) or name
1004
727
  e_type = self.schema_to_pytype(prop) or 'str'
1005
728
  enums[self.class_name(e_name)] = (e_type, values)
1006
729
 
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env python3
2
+ """Implement the 'oas analyze' CLI commands."""
3
+ import typer
4
+
5
+ from openapi_spec_tools.cli.content import app as content_app
6
+ from openapi_spec_tools.cli.models import app as models_app
7
+ from openapi_spec_tools.cli.operations import app as op_app
8
+ from openapi_spec_tools.cli.paths import app as path_app
9
+ from openapi_spec_tools.cli.tags import app as tag_app
10
+
11
+ app = typer.Typer(no_args_is_help=True, short_help="Tools for analyzing an OAS file")
12
+ app.add_typer(op_app, name="ops")
13
+ app.add_typer(path_app, name="paths")
14
+ app.add_typer(models_app, name="models")
15
+ app.add_typer(tag_app, name="tags")
16
+ app.add_typer(content_app, name="content")