Skip to content

Commit 9637dfd

Browse files
authored
Corrects change from #13 which failed to return results to be written to file. (#15)
Signed-off-by: Caroline Russell <caroline@appthreat.dev>
1 parent 433e63b commit 9637dfd

4 files changed

Lines changed: 51 additions & 19 deletions

File tree

‎atom_tools/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""
22
A cli, classes and functions for converting an atom slice to a different format
33
"""
4-
__version__ = '0.1.0'
4+
__version__ = '0.1.1'

‎atom_tools/cli/commands/convert.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Convert Command for the atom-tools CLI.
33
"""
44
import json
5+
import logging
56

67
from cleo.commands.command import Command
78
from cleo.helpers import option
@@ -93,13 +94,19 @@ def handle(self):
9394
self.option('format'),
9495
self.option('type'),
9596
self.option('usages-slice'),
96-
self.option('server'),
97+
# self.option('server'),
9798
# self.option('reachables-slice'),
9899
)
99-
if result := converter.endpoints_to_openapi():
100-
with open(self.option('output-file'), 'w',
101-
encoding='utf-8') as f:
102-
json.dump(result, f, indent=4, sort_keys=True)
100+
101+
if not (result := converter.endpoints_to_openapi(
102+
self.option('server'))):
103+
logging.error('No results produced!')
104+
return 1
105+
with open(self.option('output-file'), 'w',
106+
encoding='utf-8') as f:
107+
json.dump(result, f, indent=4, sort_keys=True)
108+
logging.info(f'OpenAPI document written to '
109+
f'{self.option("output-file")}.')
103110
case _:
104111
raise ValueError(
105112
f'Unknown destination format: {self.option("format")}'

‎atom_tools/lib/converter.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,14 @@ def __init__(
107107
dest_format: str,
108108
origin_type: str,
109109
usages: str,
110-
server: str
111110
) -> None:
112111
self.usages = AtomSlice(usages, origin_type)
113112
self.origin_type = origin_type
114113
self.openapi_version = dest_format.replace('openapi', '')
115114
self.title = f'OpenAPI Specification for {Path(usages).parent.stem}'
116115
self.regex = RegexCollection()
117-
self.server = server
118-
self.output: Dict[str, Any] = {}
119116

120-
def endpoints_to_openapi(self) -> None:
117+
def endpoints_to_openapi(self, server: str = '') -> Any:
121118
"""
122119
Generates an OpenAPI document with paths from usages.
123120
"""
@@ -127,9 +124,10 @@ def endpoints_to_openapi(self) -> None:
127124
'info': {'title': self.title, 'version': '1.0.0'},
128125
'paths': paths_obj
129126
}
130-
if self.server:
131-
output['servers'] = list({'url': self.server})
132-
self.output = output
127+
if server:
128+
output['servers'] = [{'url': server}] # type: ignore[list-item]
129+
130+
return output
133131

134132
def convert_usages(self) -> Dict[str, Any]:
135133
"""

‎test/test_converter.py‎

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,34 @@
66
@pytest.fixture
77
def java_usages_1():
88
return OpenAPI('openapi3.1.0', 'java',
9-
'test/data/java-piggymetrics-usages.json', 'https://piggymetrics.com')
9+
'test/data/java-piggymetrics-usages.json')
1010

1111

1212
@pytest.fixture
1313
def java_usages_2():
1414
return OpenAPI('openapi3.0.1', 'java',
15-
'test/data/java-sec-code-usages.json', '')
15+
'test/data/java-sec-code-usages.json')
1616

1717

1818
@pytest.fixture
1919
def js_usages_1():
2020
return OpenAPI('openapi3.0.1', 'javascript',
21-
'test/data/js-juiceshop-usages.json', '')
21+
'test/data/js-juiceshop-usages.json')
2222

2323

2424
@pytest.fixture
2525
def js_usages_2():
26-
return OpenAPI('openapi3.0.1', 'js', 'test/data/js-nodegoat-usages.json', '')
26+
return OpenAPI('openapi3.0.1', 'js', 'test/data/js-nodegoat-usages.json')
2727

2828

2929
@pytest.fixture
3030
def py_usages_1():
31-
return OpenAPI('openapi3.0.1', 'python', 'test/data/py-airflow-usages.json', '')
31+
return OpenAPI('openapi3.0.1', 'python', 'test/data/py-airflow-usages.json')
3232

3333

3434
@pytest.fixture
3535
def py_usages_2():
36-
return OpenAPI('openapi3.0.1', 'py', 'test/data/py-tornado-usages.json', '')
36+
return OpenAPI('openapi3.0.1', 'py', 'test/data/py-tornado-usages.json')
3737

3838

3939
def test_populate_endpoints(js_usages_1, js_usages_2):
@@ -919,3 +919,30 @@ def test_convert_usages(java_usages_1, java_usages_2, js_usages_1, js_usages_2,
919919
# }
920920
assert py_usages_2.convert_usages() == {'/': {}, '/auth/google': {},
921921
'/logout': {}}
922+
923+
924+
def test_endpoints_to_openapi(java_usages_1):
925+
926+
assert java_usages_1.endpoints_to_openapi() == {'info': {'title': 'OpenAPI Specification for data', 'version': '1.0.0'},
927+
'openapi': '3.1.0',
928+
'paths': {'/': {'post': {'responses': {}}},
929+
'/accounts/{accountName}': {'get': {'responses': {}},
930+
'parameters': [{'in': 'path',
931+
'name': 'accountName',
932+
'required': True}]},
933+
'/current': {'get': {'responses': {}}, 'put': {'responses': {}}},
934+
'/latest': {'get': {'responses': {}}},
935+
'/statistics/{accountName}': {'parameters': [{'in': 'path',
936+
'name': 'accountName',
937+
'required': True}],
938+
'put': {'responses': {}}},
939+
'/uaa/users': {'post': {'responses': {}}},
940+
'/{accountName}': {'get': {'responses': {}},
941+
'parameters': [{'in': 'path',
942+
'name': 'accountName',
943+
'required': True}],
944+
'put': {'responses': {}}},
945+
'/{name}': {'get': {'responses': {}},
946+
'parameters': [{'in': 'path',
947+
'name': 'name',
948+
'required': True}]}}}

0 commit comments

Comments
 (0)