diff --git a/generate_schema_config.py b/generate_schema_config.py index c0ffe04e955f9329a7eba324a74aa519a518afeb..32f4165bbca2db27776f38b2c1aebb78f5ff8c0b 100644 --- a/generate_schema_config.py +++ b/generate_schema_config.py @@ -23,8 +23,8 @@ import os URL_PREFIX = os.getenv("URL_PREFIX", "") # Allow env override of default host FLASK_HOST = os.getenv("FLASK_HOST", "localhost") -# Allow env override of default port -FLASK_PORT = os.getenv("FLASK_PORT", 5000) +# Allow env override of default port +FLASK_PORT = os.getenv("FLASK_PORT", 5000) swagger_config = { @@ -114,17 +114,18 @@ swagger_config = { def configure_flask(swagger_config): app = Flask(__name__) Swagger(app, config=swagger_config, merge=True) - + @app.after_request def after_request_decorator(response): - if type(response).__name__ == 'Response': - if response.content_type == 'application/json': + if type(response).__name__ == "Response": + if response.content_type == "application/json": data = response.json - if 'definitions' in data: - del data['definitions'] + if "definitions" in data: + del data["definitions"] response.data = json.dumps(data) return response + return app @@ -132,32 +133,32 @@ def serve(swagger_config): """ Run as local flask app on port 5000 """ - # Replace schema route to remove invalid + # Replace schema route to remove invalid # definitions: {} # Should be fixed if Flassger 0.9.7 is released - # - # The last release of flasgger was Aug 2020 - # This bug was fixed in Nov 2021 + # + # The last release of flasgger was Aug 2020 + # This bug was fixed in Nov 2021 # There is a pre-release from May 2023 - # Until the fix gets released we have to - # remove the invalid definitions object + # Until the fix gets released we have to + # remove the invalid definitions object # from the spec - app = configure_flask(swagger_config) + app = configure_flask(swagger_config) app.run(debug=True, host=FLASK_HOST, port=FLASK_PORT) def compile_schema(swagger_config): """Extract the output schema from flasgger - - The only way I have found to do this is to - use a test client to make the GET request + + The only way I have found to do this is to + use a test client to make the GET request for the page - The function that returns the definition + The function that returns the definition can't be called outside the flask app context """ - app = configure_flask(swagger_config) - route = swagger_config['specs'][0]['route'] + app = configure_flask(swagger_config) + route = swagger_config["specs"][0]["route"] client = app.test_client() response = client.get(route) spec = response.json @@ -171,7 +172,7 @@ def write_schema(swagger_config, file_path): spec = compile_schema(swagger_config) json_schema = json.dumps(spec, indent=2) - with open(file_path, "w") as f: + with open(file_path, "w") as f: f.write(json_schema) @@ -179,22 +180,38 @@ def get_options(): """ Parse script arguments """ - parser = argparse.ArgumentParser(description="Generate the schema", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - parser.add_argument("-s", "--serve", dest="run_flask", action="store_true", help="Run flask app", default=False) - parser.add_argument("-f", "--file", dest="output_file", action="store_true", help="Save output to schema file", default=False) + parser = argparse.ArgumentParser( + description="Generate the schema", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + ) + parser.add_argument( + "-s", + "--serve", + dest="run_flask", + action="store_true", + help="Run flask app", + default=False, + ) + parser.add_argument( + "-f", + "--file", + dest="output_file", + action="store_true", + help="Save output to schema file", + default=False, + ) args = parser.parse_args() config = vars(args) # If no flag is specified default to running the flask server - if (all(v is False for v in config.values())): + if all(v is False for v in config.values()): config["run_flask"] = True - return config + return config + +if __name__ == "__main__": + # Parse script args + config = get_options() -if __name__ == "__main__": - # Parse script args - config = get_options() - # Output compiled schema if config.get("output_file"): write_schema(swagger_config, "project/soar/swagger.json")