openapi-spec-tools 0.19.0__tar.gz → 0.20.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.19.0 → openapi_spec_tools-0.20.0}/.gitignore +4 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/PKG-INFO +2 -2
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/__init__.py +3 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/api_gen/api_generator.py +4 -4
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/base_gen/_requests.py +1 -1
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/base_gen/base_generator.py +8 -11
- openapi_spec_tools-0.20.0/openapi_spec_tools/cli/analyze.py +16 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli/api_gen.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli/arguments.py +19 -0
- openapi_spec_tools-0.20.0/openapi_spec_tools/cli/content.py +44 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli/history.py +1 -1
- openapi_spec_tools-0.20.0/openapi_spec_tools/cli/models.py +186 -0
- openapi_spec_tools-0.20.0/openapi_spec_tools/cli/oas.py +212 -0
- openapi_spec_tools-0.20.0/openapi_spec_tools/cli/operations.py +277 -0
- openapi_spec_tools-0.20.0/openapi_spec_tools/cli/paths.py +119 -0
- openapi_spec_tools-0.20.0/openapi_spec_tools/cli/tags.py +89 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli/utils.py +56 -4
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli_gen/_tree.py +0 -2
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli_gen/cli_generator.py +27 -16
- openapi_spec_tools-0.20.0/openapi_spec_tools/constants.py +5 -0
- openapi_spec_tools-0.20.0/openapi_spec_tools/expand.py +104 -0
- openapi_spec_tools-0.20.0/openapi_spec_tools/flatten.py +303 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/layout/layout_generator.py +6 -10
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/types.py +10 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/utils.py +17 -8
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/pyproject.toml +12 -3
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/tests/cli_gen/test_cli_generator.py +15 -6
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/tests/cli_gen/test_files.py +4 -2
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/tests/cli_gen/test_main.py +1 -1
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/tests/cli_gen/test_tree.py +1 -1
- openapi_spec_tools-0.19.0/openapi_spec_tools/cli/oas.py +0 -712
- openapi_spec_tools-0.19.0/tests/cli_gen/helpers.py +0 -11
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/LICENSE +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/README.md +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/api_gen/__init__.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/api_gen/_environment.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/api_gen/flat_generator.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/api_gen/opaque_generator.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/api_gen/property_generator.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/base_gen/__init__.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/base_gen/_logging.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/base_gen/config.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/base_gen/constants.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/base_gen/files.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/base_gen/utils.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli/__init__.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli/cli_gen.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli/layout.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli_gen/__init__.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli_gen/_arguments.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli_gen/_display.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli_gen/_exceptions.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/cli_gen/files.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/layout/__init__.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/layout/config.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/layout/merge.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/layout/types.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/layout/utils.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/tests/cli_gen/__init__.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/tests/cli_gen/test_display.py +0 -0
- {openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/tests/cli_gen/test_exceptions.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.20.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.
|
|
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,7 @@
|
|
|
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 flatten_schema
|
|
2
5
|
from openapi_spec_tools.utils import count_values
|
|
3
6
|
from openapi_spec_tools.utils import find_diffs
|
|
4
7
|
from openapi_spec_tools.utils import find_paths
|
{openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/api_gen/api_generator.py
RENAMED
|
@@ -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
|
|
105
|
-
from {self.package_name} import _logging as _l
|
|
106
|
-
from {self.package_name} import _requests as _r
|
|
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"
|
{openapi_spec_tools-0.19.0 → openapi_spec_tools-0.20.0}/openapi_spec_tools/base_gen/_requests.py
RENAMED
|
@@ -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
|
|
|
@@ -37,6 +37,7 @@ from openapi_spec_tools.types import OasField
|
|
|
37
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"
|
|
@@ -259,7 +256,7 @@ class BaseGenerator:
|
|
|
259
256
|
reference = item.get(OasField.REFS)
|
|
260
257
|
|
|
261
258
|
if reference:
|
|
262
|
-
prop[OasField.X_REF.value] =
|
|
259
|
+
prop[OasField.X_REF.value] = reference_classname(reference)
|
|
263
260
|
|
|
264
261
|
# resolve references, if they're enums
|
|
265
262
|
sub_model = self.get_model(reference)
|
|
@@ -390,7 +387,7 @@ class BaseGenerator:
|
|
|
390
387
|
|
|
391
388
|
full_ref = model.get(OasField.REFS)
|
|
392
389
|
if full_ref:
|
|
393
|
-
updated[OasField.X_REF.value] =
|
|
390
|
+
updated[OasField.X_REF.value] = reference_classname(full_ref)
|
|
394
391
|
submodel = self.get_model(full_ref)
|
|
395
392
|
if not submodel:
|
|
396
393
|
self.logger.warning(f"Unable to find model for {full_ref}")
|
|
@@ -426,7 +423,7 @@ class BaseGenerator:
|
|
|
426
423
|
for index, parent in enumerate(model.get(OasField.ALL_OF, [])):
|
|
427
424
|
required_sub = parent.get(OasField.REQUIRED, [])
|
|
428
425
|
reference = parent.get(OasField.REFS, "")
|
|
429
|
-
short_refname =
|
|
426
|
+
short_refname = reference_classname(reference)
|
|
430
427
|
sub_properties = self.expanded_settable_properties(f"{name}.anyOf[{index}]", parent)
|
|
431
428
|
for sub_name, sub_data in sub_properties.items():
|
|
432
429
|
if short_refname:
|
|
@@ -446,7 +443,7 @@ class BaseGenerator:
|
|
|
446
443
|
self.update_one_of(name, model)
|
|
447
444
|
|
|
448
445
|
reference = model.get(OasField.REFS, "")
|
|
449
|
-
short_refname =
|
|
446
|
+
short_refname = reference_classname(reference)
|
|
450
447
|
required_props = model.get(OasField.REQUIRED, [])
|
|
451
448
|
ref_model = self.get_model(reference)
|
|
452
449
|
if ref_model:
|
|
@@ -504,7 +501,7 @@ class BaseGenerator:
|
|
|
504
501
|
full_name = f"{prop_name}.{sub_name}"
|
|
505
502
|
sub_data[OasField.REQUIRED.value] = prop_name in required_props and sub_name in required_sub
|
|
506
503
|
if reference:
|
|
507
|
-
set_missing(sub_data, OasField.X_REF.value,
|
|
504
|
+
set_missing(sub_data, OasField.X_REF.value, reference_classname(reference))
|
|
508
505
|
set_missing(sub_data, OasField.X_FIELD.value, sub_name)
|
|
509
506
|
prepend(sub_data, OasField.X_PARENTS.value, prop_name)
|
|
510
507
|
if forced_single:
|
|
@@ -530,7 +527,7 @@ class BaseGenerator:
|
|
|
530
527
|
name = "body"
|
|
531
528
|
ref = schema.get(OasField.REFS)
|
|
532
529
|
if ref:
|
|
533
|
-
name =
|
|
530
|
+
name = reference_classname(ref)
|
|
534
531
|
schema = self.get_model(ref)
|
|
535
532
|
|
|
536
533
|
return (name, schema)
|
|
@@ -659,7 +656,7 @@ class BaseGenerator:
|
|
|
659
656
|
model = self.get_model(ref)
|
|
660
657
|
if model:
|
|
661
658
|
item = deepcopy(model)
|
|
662
|
-
item[OasField.X_REF] =
|
|
659
|
+
item[OasField.X_REF] = reference_classname(ref)
|
|
663
660
|
if item.get(OasField.IN) != location:
|
|
664
661
|
continue
|
|
665
662
|
|
|
@@ -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")
|
|
File without changes
|
|
@@ -19,6 +19,14 @@ class LogLevel(str, Enum):
|
|
|
19
19
|
DEBUG = "debug"
|
|
20
20
|
|
|
21
21
|
|
|
22
|
+
class ExpansionType(str, Enum):
|
|
23
|
+
"""Type of expansion to perform."""
|
|
24
|
+
|
|
25
|
+
NONE = "none"
|
|
26
|
+
EXPAND = "expand"
|
|
27
|
+
FLATTEN = "flatten"
|
|
28
|
+
|
|
29
|
+
|
|
22
30
|
CodeDirectoryOption = Annotated[
|
|
23
31
|
str | None,
|
|
24
32
|
typer.Option("--code-dir", metavar=DIRECTORY, show_default=False, help="Directory for code -- overrides default")
|
|
@@ -27,6 +35,17 @@ ConfigFileOption = Annotated[
|
|
|
27
35
|
str | None,
|
|
28
36
|
typer.Option("--config", metavar=FILENAME, show_default=False, help="Generator configuration file")
|
|
29
37
|
]
|
|
38
|
+
ExpansionOption = Annotated[
|
|
39
|
+
ExpansionType,
|
|
40
|
+
typer.Option(
|
|
41
|
+
"--expansion",
|
|
42
|
+
case_sensitive=False,
|
|
43
|
+
help=(
|
|
44
|
+
"Level of expansion to display: 'expand' resolve '$ref' and 'allOf' properties;"
|
|
45
|
+
" 'flatten' unnests sub-object properties."
|
|
46
|
+
),
|
|
47
|
+
),
|
|
48
|
+
]
|
|
30
49
|
IndentOption = Annotated[
|
|
31
50
|
int,
|
|
32
51
|
typer.Option(
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Implement the 'oas analyze content' CLI commands."""
|
|
3
|
+
from typing import Annotated
|
|
4
|
+
|
|
5
|
+
import typer
|
|
6
|
+
from rich_objects import console_factory
|
|
7
|
+
|
|
8
|
+
from openapi_spec_tools.cli.arguments import LogLevelOption
|
|
9
|
+
from openapi_spec_tools.cli.arguments import OpenApiFilenameArgument
|
|
10
|
+
from openapi_spec_tools.cli.utils import init_logging
|
|
11
|
+
from openapi_spec_tools.cli.utils import open_oas_with_error_handling
|
|
12
|
+
from openapi_spec_tools.utils import map_content_types
|
|
13
|
+
|
|
14
|
+
LOG_CLASS = "content"
|
|
15
|
+
|
|
16
|
+
app = typer.Typer(no_args_is_help=True, short_help="Inspect things related to content-types")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@app.command("list", short_help="List operations by response content-type")
|
|
20
|
+
def content_type_list(
|
|
21
|
+
filename: OpenApiFilenameArgument,
|
|
22
|
+
max_size: Annotated[int, typer.Option(help="Maximum number of operations to show")] = 10,
|
|
23
|
+
content_type: Annotated[str | None, typer.Option(help="Only display for specified content type")] = None,
|
|
24
|
+
log_level: LogLevelOption = "info",
|
|
25
|
+
) -> None:
|
|
26
|
+
logger = init_logging(log_level, LOG_CLASS)
|
|
27
|
+
spec = open_oas_with_error_handling(filename, logger)
|
|
28
|
+
content = map_content_types(spec)
|
|
29
|
+
|
|
30
|
+
if content_type:
|
|
31
|
+
content = {k: v for k, v in content.items() if k == content_type}
|
|
32
|
+
|
|
33
|
+
console = console_factory()
|
|
34
|
+
if not content:
|
|
35
|
+
console.print("No content-types found")
|
|
36
|
+
return
|
|
37
|
+
|
|
38
|
+
for name, operations in content.items():
|
|
39
|
+
console.print(name)
|
|
40
|
+
for op_id in sorted(operations)[:max_size]:
|
|
41
|
+
console.print(f" {op_id}")
|
|
42
|
+
if len(operations) > max_size:
|
|
43
|
+
console.print(" ...")
|
|
44
|
+
console.print(f" + {len(operations) - max_size} more")
|
|
@@ -156,7 +156,7 @@ def _assert_exists(file: str) -> None:
|
|
|
156
156
|
commit = repo.head.commit
|
|
157
157
|
relative_path = Path(file).absolute().relative_to(commit.tree.abspath).as_posix()
|
|
158
158
|
_ = commit.tree / relative_path
|
|
159
|
-
except Exception:
|
|
159
|
+
except Exception: # noqa: BLE001
|
|
160
160
|
error_out("Unable to find file")
|
|
161
161
|
|
|
162
162
|
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Implement the 'oas analyze models' CLI commands."""
|
|
3
|
+
from typing import Annotated
|
|
4
|
+
|
|
5
|
+
import typer
|
|
6
|
+
import yaml
|
|
7
|
+
from rich_objects import console_factory
|
|
8
|
+
|
|
9
|
+
from openapi_spec_tools.cli.arguments import ExpansionOption
|
|
10
|
+
from openapi_spec_tools.cli.arguments import LogLevelOption
|
|
11
|
+
from openapi_spec_tools.cli.arguments import OpenApiFilenameArgument
|
|
12
|
+
from openapi_spec_tools.cli.utils import error_out
|
|
13
|
+
from openapi_spec_tools.cli.utils import init_logging
|
|
14
|
+
from openapi_spec_tools.cli.utils import open_oas_with_error_handling
|
|
15
|
+
from openapi_spec_tools.cli.utils import perform_expansion
|
|
16
|
+
from openapi_spec_tools.cli.utils import remove_dict_prefix
|
|
17
|
+
from openapi_spec_tools.cli.utils import remove_list_prefix
|
|
18
|
+
from openapi_spec_tools.types import OasField
|
|
19
|
+
from openapi_spec_tools.utils import find_references
|
|
20
|
+
from openapi_spec_tools.utils import map_models
|
|
21
|
+
from openapi_spec_tools.utils import model_filter
|
|
22
|
+
from openapi_spec_tools.utils import model_full_name
|
|
23
|
+
from openapi_spec_tools.utils import model_references
|
|
24
|
+
from openapi_spec_tools.utils import models_referenced_by
|
|
25
|
+
from openapi_spec_tools.utils import unroll
|
|
26
|
+
|
|
27
|
+
INDENT = " "
|
|
28
|
+
LOG_CLASS = "models"
|
|
29
|
+
|
|
30
|
+
app = typer.Typer(no_args_is_help=True, short_help="Inspect things related to models")
|
|
31
|
+
|
|
32
|
+
ModelNameArgument = Annotated[str, typer.Argument(metavar="NAME", help="Name of the model")]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@app.command(name="list", short_help="List models in OpenAPI spec")
|
|
36
|
+
def models_list(
|
|
37
|
+
filename: OpenApiFilenameArgument,
|
|
38
|
+
search: Annotated[
|
|
39
|
+
str | None,
|
|
40
|
+
typer.Option("--contains", help="Search for this value in the model names"),
|
|
41
|
+
] = None,
|
|
42
|
+
log_level: LogLevelOption = "info",
|
|
43
|
+
) -> None:
|
|
44
|
+
logger = init_logging(log_level, LOG_CLASS)
|
|
45
|
+
spec = open_oas_with_error_handling(filename, logger)
|
|
46
|
+
|
|
47
|
+
models = map_models(spec.get(OasField.COMPONENTS, {}))
|
|
48
|
+
names = sorted(models.keys())
|
|
49
|
+
if search:
|
|
50
|
+
needle = search.lower()
|
|
51
|
+
names = [_ for _ in names if needle in _.lower()]
|
|
52
|
+
|
|
53
|
+
console = console_factory()
|
|
54
|
+
match_info = f" matching '{search}'" if search else ""
|
|
55
|
+
if not names:
|
|
56
|
+
console.print(f"No models found{match_info}")
|
|
57
|
+
else:
|
|
58
|
+
console.print(f"Found {len(names)} models{match_info}:")
|
|
59
|
+
names = remove_list_prefix(names)
|
|
60
|
+
for n in names:
|
|
61
|
+
console.print(f"{INDENT}{n}")
|
|
62
|
+
|
|
63
|
+
return
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@app.command(name="show", short_help="Show the model schema")
|
|
67
|
+
def models_show(
|
|
68
|
+
filename: OpenApiFilenameArgument,
|
|
69
|
+
model_name: ModelNameArgument,
|
|
70
|
+
include_referenced: Annotated[bool, typer.Option("--references", help="Include referenced models")] = False,
|
|
71
|
+
expansion: ExpansionOption = "none",
|
|
72
|
+
log_level: LogLevelOption = "info",
|
|
73
|
+
) -> None:
|
|
74
|
+
logger = init_logging(log_level, LOG_CLASS)
|
|
75
|
+
spec = open_oas_with_error_handling(filename, logger)
|
|
76
|
+
|
|
77
|
+
components = spec.get(OasField.COMPONENTS, {})
|
|
78
|
+
models = map_models(components)
|
|
79
|
+
full_name = model_full_name(models, model_name)
|
|
80
|
+
model = models.get(full_name)
|
|
81
|
+
if not model:
|
|
82
|
+
error_out(f"failed to find {model_name}")
|
|
83
|
+
|
|
84
|
+
if not include_referenced:
|
|
85
|
+
models = {full_name: model}
|
|
86
|
+
else:
|
|
87
|
+
models = model_filter(models, {full_name})
|
|
88
|
+
|
|
89
|
+
models = {name: perform_expansion(expansion, schema, components, name) for name, schema in models.items()}
|
|
90
|
+
|
|
91
|
+
models = remove_dict_prefix(models)
|
|
92
|
+
console = console_factory()
|
|
93
|
+
console.print(yaml.dump(models, indent=len(INDENT)))
|
|
94
|
+
return
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@app.command(name="uses", short_help="List sub-models used by the specified model")
|
|
98
|
+
def models_uses(
|
|
99
|
+
filename: OpenApiFilenameArgument,
|
|
100
|
+
model_name: ModelNameArgument,
|
|
101
|
+
log_level: LogLevelOption = "info",
|
|
102
|
+
) -> None:
|
|
103
|
+
logger = init_logging(log_level, LOG_CLASS)
|
|
104
|
+
spec = open_oas_with_error_handling(filename, logger)
|
|
105
|
+
|
|
106
|
+
models = map_models(spec.get(OasField.COMPONENTS, {}))
|
|
107
|
+
full_name = model_full_name(models, model_name)
|
|
108
|
+
if not full_name:
|
|
109
|
+
error_out(f"no model '{model_name}' found")
|
|
110
|
+
|
|
111
|
+
references = model_references(models)
|
|
112
|
+
|
|
113
|
+
console = console_factory()
|
|
114
|
+
matches = unroll(references, references.get(full_name))
|
|
115
|
+
if not matches:
|
|
116
|
+
console.print(f"{model_name} does not use any other models")
|
|
117
|
+
else:
|
|
118
|
+
matches = remove_list_prefix(list(matches))
|
|
119
|
+
console.print(f"Found {model_name} uses {len(matches)} models:")
|
|
120
|
+
for n in sorted(matches):
|
|
121
|
+
console.print(f"{INDENT}{n}")
|
|
122
|
+
|
|
123
|
+
return
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@app.command(name="used-by", short_help="List models which reference the specified model")
|
|
127
|
+
def models_used_by(
|
|
128
|
+
filename: OpenApiFilenameArgument,
|
|
129
|
+
model_name: ModelNameArgument,
|
|
130
|
+
log_level: LogLevelOption = "info",
|
|
131
|
+
) -> None:
|
|
132
|
+
logger = init_logging(log_level, LOG_CLASS)
|
|
133
|
+
spec = open_oas_with_error_handling(filename, logger)
|
|
134
|
+
|
|
135
|
+
models = map_models(spec.get(OasField.COMPONENTS, {}))
|
|
136
|
+
full_name = model_full_name(models, model_name)
|
|
137
|
+
if not full_name:
|
|
138
|
+
error_out(f"no model '{model_name}' found")
|
|
139
|
+
|
|
140
|
+
console = console_factory()
|
|
141
|
+
matches = models_referenced_by(models, full_name)
|
|
142
|
+
if not matches:
|
|
143
|
+
console.print(f"{model_name} is not used by any other models")
|
|
144
|
+
else:
|
|
145
|
+
matches = remove_list_prefix(list(matches))
|
|
146
|
+
console.print(f"Found {model_name} is used by {len(matches)} models:")
|
|
147
|
+
for n in sorted(matches):
|
|
148
|
+
console.print(f"{INDENT}{n}")
|
|
149
|
+
|
|
150
|
+
return
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@app.command(name="ops", short_help="List operations which reference the specified model")
|
|
154
|
+
def models_operations(
|
|
155
|
+
filename: OpenApiFilenameArgument,
|
|
156
|
+
model_name: ModelNameArgument,
|
|
157
|
+
log_level: LogLevelOption = "info",
|
|
158
|
+
) -> None:
|
|
159
|
+
logger = init_logging(log_level, LOG_CLASS)
|
|
160
|
+
spec = open_oas_with_error_handling(filename, logger)
|
|
161
|
+
|
|
162
|
+
models = map_models(spec.get(OasField.COMPONENTS, {}))
|
|
163
|
+
full_name = model_full_name(models, model_name)
|
|
164
|
+
if not full_name:
|
|
165
|
+
error_out(f"no model '{model_name}' found")
|
|
166
|
+
|
|
167
|
+
model_refs = models_referenced_by(models, full_name)
|
|
168
|
+
model_refs.add(full_name) # include the direct references, too
|
|
169
|
+
|
|
170
|
+
matches = []
|
|
171
|
+
for path_data in spec.get(OasField.PATHS, {}).values():
|
|
172
|
+
for method, op_data in path_data.items():
|
|
173
|
+
if method == OasField.PARAMS:
|
|
174
|
+
continue
|
|
175
|
+
references = find_references(op_data)
|
|
176
|
+
if references.intersection(model_refs):
|
|
177
|
+
op_id = op_data.get(OasField.OP_ID)
|
|
178
|
+
matches.append(op_id)
|
|
179
|
+
|
|
180
|
+
matches = remove_list_prefix(matches)
|
|
181
|
+
console = console_factory()
|
|
182
|
+
console.print(f"Found {model_name} is used by {len(matches)} operations:")
|
|
183
|
+
for n in sorted(matches):
|
|
184
|
+
console.print(f"{INDENT}{n}")
|
|
185
|
+
|
|
186
|
+
return
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Implement the 'oas' CLI with options for analyzing and modifying OpenAPI specs."""
|
|
3
|
+
from copy import deepcopy
|
|
4
|
+
from enum import Enum
|
|
5
|
+
from typing import Annotated
|
|
6
|
+
|
|
7
|
+
import typer
|
|
8
|
+
import yaml
|
|
9
|
+
from rich_objects import console_factory
|
|
10
|
+
|
|
11
|
+
from openapi_spec_tools.cli.analyze import app as analyze_app
|
|
12
|
+
from openapi_spec_tools.cli.arguments import LogLevelOption
|
|
13
|
+
from openapi_spec_tools.cli.arguments import OpenApiFilenameArgument
|
|
14
|
+
from openapi_spec_tools.cli.arguments import UpdatedOpenApiFilenameOption
|
|
15
|
+
from openapi_spec_tools.cli.history import app as history_app
|
|
16
|
+
from openapi_spec_tools.cli.utils import error_out
|
|
17
|
+
from openapi_spec_tools.cli.utils import init_logging
|
|
18
|
+
from openapi_spec_tools.cli.utils import open_oas_with_error_handling
|
|
19
|
+
from openapi_spec_tools.cli.utils import short_filename
|
|
20
|
+
from openapi_spec_tools.types import OasField
|
|
21
|
+
from openapi_spec_tools.utils import count_values
|
|
22
|
+
from openapi_spec_tools.utils import find_diffs
|
|
23
|
+
from openapi_spec_tools.utils import map_models
|
|
24
|
+
from openapi_spec_tools.utils import remove_property
|
|
25
|
+
from openapi_spec_tools.utils import remove_schema_tags
|
|
26
|
+
from openapi_spec_tools.utils import schema_operations_filter
|
|
27
|
+
from openapi_spec_tools.utils import set_nullable_not_required
|
|
28
|
+
|
|
29
|
+
INDENT = " "
|
|
30
|
+
LOG_CLASS = "oas"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
#################################################
|
|
34
|
+
# Top-level stuff
|
|
35
|
+
app = typer.Typer(
|
|
36
|
+
name="oas",
|
|
37
|
+
no_args_is_help=True,
|
|
38
|
+
short_help="OpenAPI specification tools",
|
|
39
|
+
help="Various utilities for inspecting, analyzing and modifying OpenAPI specifications.",
|
|
40
|
+
)
|
|
41
|
+
app.add_typer(history_app, name="history")
|
|
42
|
+
app.add_typer(analyze_app, name="analyze")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@app.command("info", short_help="Display the 'info' from the OpenAPI specification")
|
|
46
|
+
def info(
|
|
47
|
+
filename: OpenApiFilenameArgument,
|
|
48
|
+
log_level: LogLevelOption = "info",
|
|
49
|
+
) -> None:
|
|
50
|
+
logger = init_logging(log_level, LOG_CLASS)
|
|
51
|
+
spec = open_oas_with_error_handling(filename, logger)
|
|
52
|
+
|
|
53
|
+
info = spec.get("info", {})
|
|
54
|
+
console = console_factory()
|
|
55
|
+
console.print(yaml.dump({"info": info}, indent=len(INDENT)))
|
|
56
|
+
return
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@app.command("summary", short_help="Display summary of OAS data")
|
|
60
|
+
def summary(
|
|
61
|
+
filename: OpenApiFilenameArgument,
|
|
62
|
+
log_level: LogLevelOption = "info",
|
|
63
|
+
) -> None:
|
|
64
|
+
logger = init_logging(log_level, LOG_CLASS)
|
|
65
|
+
spec = open_oas_with_error_handling(filename, logger)
|
|
66
|
+
method_count = {
|
|
67
|
+
'get': 0,
|
|
68
|
+
'put': 0,
|
|
69
|
+
'patch': 0,
|
|
70
|
+
'delete': 0,
|
|
71
|
+
'post': 0,
|
|
72
|
+
}
|
|
73
|
+
path_count = 0
|
|
74
|
+
model_count = len(map_models(spec.get(OasField.COMPONENTS, {})))
|
|
75
|
+
tag_count = {}
|
|
76
|
+
|
|
77
|
+
for path_data in spec.get(OasField.PATHS, {}).values():
|
|
78
|
+
path_count += 1
|
|
79
|
+
for method, operation in path_data.items():
|
|
80
|
+
if method == OasField.PARAMS:
|
|
81
|
+
continue
|
|
82
|
+
|
|
83
|
+
method_count[method] += 1
|
|
84
|
+
for tag in operation.get(OasField.TAGS, []):
|
|
85
|
+
orig = tag_count.get(tag, 0)
|
|
86
|
+
tag_count[tag] = orig + 1
|
|
87
|
+
|
|
88
|
+
console = console_factory()
|
|
89
|
+
console.print(f"OpenAPI spec ({short_filename(filename)}):")
|
|
90
|
+
console.print(f"{INDENT}Models: {model_count}")
|
|
91
|
+
console.print(f"{INDENT}Paths: {path_count}")
|
|
92
|
+
console.print(f"{INDENT}Operation methods ({sum(method_count.values())}):")
|
|
93
|
+
for k, v in method_count.items():
|
|
94
|
+
console.print(f"{INDENT * 2}{k}: {v}")
|
|
95
|
+
console.print(f"{INDENT}Tags ({len(tag_count)}) with operation counts:")
|
|
96
|
+
for k, v in tag_count.items():
|
|
97
|
+
console.print(f"{INDENT * 2}{k}: {v}")
|
|
98
|
+
|
|
99
|
+
return
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@app.command("diff", short_help="Compare two OAS files")
|
|
103
|
+
def diff(
|
|
104
|
+
original: Annotated[
|
|
105
|
+
str,
|
|
106
|
+
typer.Argument(metavar="FILENAME", show_default=False, help="Original OpenAPI specification filename"),
|
|
107
|
+
],
|
|
108
|
+
updated: Annotated[
|
|
109
|
+
str,
|
|
110
|
+
typer.Argument(metavar="FILENAME", show_default=False, help="Updated OpenAPI specification filename"),
|
|
111
|
+
],
|
|
112
|
+
log_level: LogLevelOption = "info",
|
|
113
|
+
) -> None:
|
|
114
|
+
logger = init_logging(log_level, LOG_CLASS)
|
|
115
|
+
old_spec = open_oas_with_error_handling(original, logger)
|
|
116
|
+
new_spec = open_oas_with_error_handling(updated, logger)
|
|
117
|
+
|
|
118
|
+
console = console_factory()
|
|
119
|
+
diffs = find_diffs(old_spec, new_spec)
|
|
120
|
+
if not diffs:
|
|
121
|
+
console.print(f"No differences between {short_filename(original)} and {short_filename(updated)}")
|
|
122
|
+
else:
|
|
123
|
+
console.print(yaml.dump(diffs, indent=len(INDENT)))
|
|
124
|
+
return
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class DisplayOption(str, Enum):
|
|
128
|
+
"""Options for displaying the output."""
|
|
129
|
+
|
|
130
|
+
NONE = "none"
|
|
131
|
+
SUMMARY = "summary"
|
|
132
|
+
DIFF = "diff"
|
|
133
|
+
FINAL = "final"
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
@app.command("update", short_help="Update the OpenAPI spec")
|
|
137
|
+
def update(
|
|
138
|
+
original_filename: OpenApiFilenameArgument,
|
|
139
|
+
updated_filename: UpdatedOpenApiFilenameOption = None,
|
|
140
|
+
nullable_not_required: Annotated[
|
|
141
|
+
bool,
|
|
142
|
+
typer.Option(help="Remove 'nullable' properties from required list"),
|
|
143
|
+
] = False,
|
|
144
|
+
remove_all_tags: Annotated[bool, typer.Option(help="Remove all tags")] = False,
|
|
145
|
+
remove_operations: Annotated[
|
|
146
|
+
list[str] | None,
|
|
147
|
+
typer.Option("--remove-op", show_default=False, help="List of operations to remove"),
|
|
148
|
+
] = None,
|
|
149
|
+
allowed_operations: Annotated[
|
|
150
|
+
list[str] | None,
|
|
151
|
+
typer.Option("--allow-op", show_default=False, help="List of operations to keep"),
|
|
152
|
+
] = None,
|
|
153
|
+
remove_properties: Annotated[
|
|
154
|
+
list[str] | None,
|
|
155
|
+
typer.Option("--remove", show_default=False, help="List of properties to remove."),
|
|
156
|
+
] = None,
|
|
157
|
+
display_option: Annotated[
|
|
158
|
+
DisplayOption,
|
|
159
|
+
typer.Option("--display", help="Shown on console at conclusion", case_sensitive=False),
|
|
160
|
+
] = DisplayOption.DIFF,
|
|
161
|
+
indent: Annotated[
|
|
162
|
+
int,
|
|
163
|
+
typer.Option(min=1, max=10, help="Number of characters to indent on YAML display"),
|
|
164
|
+
] = len(INDENT),
|
|
165
|
+
log_level: LogLevelOption = "info",
|
|
166
|
+
) -> None:
|
|
167
|
+
logger = init_logging(log_level, LOG_CLASS)
|
|
168
|
+
old_spec = open_oas_with_error_handling(original_filename, logger)
|
|
169
|
+
updated = deepcopy(old_spec)
|
|
170
|
+
|
|
171
|
+
if allowed_operations and remove_operations:
|
|
172
|
+
error_out("cannot specify both --allow-op and --remove-op")
|
|
173
|
+
|
|
174
|
+
if remove_all_tags:
|
|
175
|
+
updated = remove_schema_tags(updated)
|
|
176
|
+
|
|
177
|
+
if remove_properties:
|
|
178
|
+
for prop_name in remove_properties:
|
|
179
|
+
updated = remove_property(updated, prop_name)
|
|
180
|
+
|
|
181
|
+
if nullable_not_required:
|
|
182
|
+
updated = set_nullable_not_required(updated)
|
|
183
|
+
|
|
184
|
+
if remove_operations:
|
|
185
|
+
updated = schema_operations_filter(updated, remove=set(remove_operations))
|
|
186
|
+
|
|
187
|
+
if allowed_operations:
|
|
188
|
+
updated = schema_operations_filter(updated, allow=set(allowed_operations))
|
|
189
|
+
|
|
190
|
+
if updated_filename:
|
|
191
|
+
with open(updated_filename, "w", encoding="utf-8", newline="\n") as fp:
|
|
192
|
+
yaml.dump(updated, fp, indent=indent)
|
|
193
|
+
|
|
194
|
+
console = console_factory()
|
|
195
|
+
diffs = find_diffs(old_spec, updated)
|
|
196
|
+
if display_option == DisplayOption.NONE:
|
|
197
|
+
pass
|
|
198
|
+
elif display_option == DisplayOption.FINAL:
|
|
199
|
+
console.print(yaml.dump(updated, indent=indent))
|
|
200
|
+
elif not diffs:
|
|
201
|
+
console.print(f"No differences between {short_filename(original_filename)} and updated")
|
|
202
|
+
elif display_option == DisplayOption.DIFF:
|
|
203
|
+
console.print(yaml.dump(diffs, indent=indent))
|
|
204
|
+
else: # must be DisplayOption.SUMMARY:
|
|
205
|
+
diff_count = count_values(diffs)
|
|
206
|
+
console.print(f"Found {diff_count} differences from {short_filename(original_filename)}")
|
|
207
|
+
|
|
208
|
+
return
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
if __name__ == "__main__":
|
|
212
|
+
app()
|