Add ability to trace API validator script (off by default)

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2025-05-31 10:08:54 +02:00
parent 49aa55e9c2
commit 5d3972a54d
2 changed files with 35 additions and 5 deletions

View File

@@ -9,13 +9,17 @@
# This file is copyright under the latest version of the EUPL. # This file is copyright under the latest version of the EUPL.
# Please see LICENSE file for your rights under this license. # Please see LICENSE file for your rights under this license.
import sys
import trace
from libs.openAPI import openApi from libs.openAPI import openApi
from libs.FTLAPI import FTLAPI from libs.FTLAPI import FTLAPI
from libs.responseVerifyer import ResponseVerifyer from libs.responseVerifyer import ResponseVerifyer
if __name__ == "__main__": TRACE = False
def main():
# OpenAPI specs are split into multiple files, this script extracts the endpoints from them # OpenAPI specs are split into multiple files, this script extracts the endpoints from them
openapi = openApi(base_path = "src/api/docs/content/specs/", api_root = "/api") openapi = openApi(base_path = "src/api/docs/content/specs/", api_root = "/api", trace = TRACE)
if not openapi.parse("main.yaml"): if not openapi.parse("main.yaml"):
exit(1) exit(1)
@@ -110,4 +114,22 @@ if __name__ == "__main__":
# If there are no errors, exit with success # If there are no errors, exit with success
print("Everything okay!") print("Everything okay!")
exit(0) #exit(0)
if __name__ == "__main__":
if TRACE:
tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix],
trace=1, count=1)
tracer.run('main()')
# make a report, placing output in the current directory
r = tracer.results()
print(r.write_results(show_missing=True, coverdir="."))
# Exit with success
exit(0)
else:
main()
# Exit with success
exit(0)

View File

@@ -17,10 +17,11 @@ class openApi():
# List of methods we want to extract # List of methods we want to extract
METHODS = ["get", "post", "put", "patch", "delete"] METHODS = ["get", "post", "put", "patch", "delete"]
def __init__(self, base_path: str, api_root: str = "/api") -> None: def __init__(self, base_path: str, api_root: str = "/api", trace: bool = False) -> None:
# Store arguments # Store arguments
self.base_path = base_path self.base_path = base_path
self.api_root = api_root self.api_root = api_root
self.TRACE = trace
# Prepare list of YAML endpoints # Prepare list of YAML endpoints
self.endpoints = {} self.endpoints = {}
@@ -83,6 +84,8 @@ class openApi():
for a in dict_in.keys(): for a in dict_in.keys():
# Create the next dict key # Create the next dict key
next_dict_key = dict_key + "/" + a if len(dict_key) > 0 else a next_dict_key = dict_key + "/" + a if len(dict_key) > 0 else a
if self.TRACE:
print("Resolving " + next_dict_key)
# If the item is a dict, we check if it is a reference # If the item is a dict, we check if it is a reference
if isinstance(dict_in[a], dict): if isinstance(dict_in[a], dict):
# Check if this is a reference # Check if this is a reference
@@ -108,6 +111,11 @@ class openApi():
else: else:
# No reference, just recurse into the next level # No reference, just recurse into the next level
self.recurseRef(dict_in[a][i], next_dict_key) self.recurseRef(dict_in[a][i], next_dict_key)
else:
# If it is not a dict or list, we do not need to do anything
if self.TRACE:
print(f"Not recursing into {next_dict_key} as it is not a dict or list: {type(dict_in[a])}")
pass
def parse(self, filename: str): def parse(self, filename: str):
@@ -116,7 +124,7 @@ class openApi():
# Get the paths # Get the paths
self.paths = self.read_yaml_maybe_cache(self.base_path + filename)["paths"] self.paths = self.read_yaml_maybe_cache(self.base_path + filename)["paths"]
except Exception as e: except Exception as e:
print("Exception when trying to read " + e) print("Exception when trying to read " + str(e))
return False return False
# Recursively resolve references in the paths # Recursively resolve references in the paths