Skip to content

Commit

Permalink
Merge pull request #2 from amalmurali47/feature/include-params
Browse files Browse the repository at this point in the history
Add support for including query parameters in route extraction
  • Loading branch information
amalmurali47 committed Apr 23, 2023
2 parents f20fa28 + 0f89efa commit 310f90f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 14 deletions.
54 changes: 52 additions & 2 deletions README.md
Expand Up @@ -15,6 +15,22 @@ pip install swagroutes
pip install -U swagroutes
```

## Help
```
usage: swagroutes [-h] [-o OUTPUT] [-p] input [input ...]
Extract routes from Swagger files.
positional arguments:
input Input file(s) or directory containing Swagger files.
options:
-h, --help show this help message and exit
-o OUTPUT, --output OUTPUT
Output file to store the results.
-p, --include-params Include query parameters in the extracted routes.
```

## Usage
To use swagroutes, simply provide input files or directories containing Swagger files as arguments. The tool will process the files and print the extracted routes.

Expand Down Expand Up @@ -43,6 +59,11 @@ swagroutes directory/
swagroutes file1.yaml directory1/ file2.json directory2/
```

#### Extract parameters
```bash
swagroutes file1.yaml --include-params
```

#### Output to a file
Save the extracted routes to an output file using the `-o` or `--output` option:

Expand All @@ -52,8 +73,8 @@ swagroutes file.yaml -o output.txt


## Examples
Given a Swagger file with the following content:

**Input:**
```yaml
basePath: /api
paths:
Expand All @@ -64,10 +85,39 @@ paths:
put: {}
```

swagroutes will output:
**Output:**

```
GET /api/users
POST /api/users
PUT /api/profile/{profile_id}
```

---

**Input:**
```yaml
basePath: /api/v1
paths:
/users:
get:
parameters:
- name: limit
in: query
- name: offset
in: query

/users/{userId}:
get:
parameters:
- name: userId
in: path
required: true
type: string
```

**Output:**
```
GET /api/v1/users/{userId}
GET /api/v1/users?limit&offset
```
2 changes: 1 addition & 1 deletion swagroutes/__init__.py
@@ -1,2 +1,2 @@
__version__ = "1.0.1"
__version__ = "1.0.2"

3 changes: 2 additions & 1 deletion swagroutes/cli.py
Expand Up @@ -7,6 +7,7 @@ def main():
parser = argparse.ArgumentParser(description='Extract routes from Swagger files.')
parser.add_argument('input', metavar='input', type=str, nargs='+', help='Input file(s) or directory containing Swagger files.')
parser.add_argument('-o', '--output', type=str, help='Output file to store the results.')
parser.add_argument('-p', '--include-params', action='store_true', help='Include query parameters in the extracted routes.')

args = parser.parse_args()

Expand All @@ -20,7 +21,7 @@ def main():
input_files.extend(path.glob('**/*.yaml'))
input_files.extend(path.glob('**/*.json'))

all_routes = process_swagger_files(input_files)
all_routes = process_swagger_files(input_files, args.include_params)

if args.output:
with open(args.output, 'w') as outfile:
Expand Down
13 changes: 9 additions & 4 deletions swagroutes/extractor.py
Expand Up @@ -13,23 +13,28 @@ def load_json_file(file_path):
return json.load(file)


def extract_routes(swagger_data):
def extract_routes(swagger_data, include_params=False):
routes = set()
base_path = swagger_data.get('basePath', '')
paths = swagger_data.get('paths', {})

http_methods = {'get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'}

for path, methods in paths.items():
for method in methods.keys():
for method, details in methods.items():
if method.lower() in http_methods:
route = f'{method.upper()} {base_path}{path}'
if include_params:
parameters = details.get('parameters', [])
query_params = [param['name'] for param in parameters if param['in'] == 'query']
if query_params:
route += '?' + '&'.join(query_params)
routes.add(route)

return routes


def process_swagger_files(files):
def process_swagger_files(files, include_params=False):
all_routes = set()

for file in files:
Expand All @@ -40,7 +45,7 @@ def process_swagger_files(files):
swagger_data = load_json_file(file)
else:
continue
routes = extract_routes(swagger_data)
routes = extract_routes(swagger_data, include_params)
all_routes.update(routes)

return all_routes
Expand Down
31 changes: 25 additions & 6 deletions tests/test_extractor.py
Expand Up @@ -29,16 +29,35 @@ def test_extract_routes_with_parameters(self):
"basePath": "/api",
"paths": {
"/users": {
"post": {},
"parameters": {}
"get": {
"parameters": [
{
"name": "limit",
"in": "query",
"type": "integer"
},
{
"name": "offset",
"in": "query",
"type": "integer"
}
]
}
}
}
}
expected_output = {
"POST /api/users",
expected_output_without_params = {
"GET /api/users"
}
routes = extract_routes(swagger_data)
self.assertEqual(expected_output, routes)
routes_without_params = extract_routes(swagger_data, include_params=False)
self.assertEqual(expected_output_without_params, routes_without_params)

expected_output_with_params = {
"GET /api/users?limit&offset"
}
routes_with_params = extract_routes(swagger_data, include_params=True)
self.assertEqual(expected_output_with_params, routes_with_params)


if __name__ == "__main__":
unittest.main()
Expand Down

0 comments on commit 310f90f

Please sign in to comment.