diff --git a/generate_schema_config.py b/generate_schema_config.py index 03888de520f71fc8a4ea107989a97129e9318847..a7bc202702e8bb6944ed165c3fe2912a773c0f61 100644 --- a/generate_schema_config.py +++ b/generate_schema_config.py @@ -120,11 +120,39 @@ def serve(): app.run(debug=False, 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 + for the page + + The function that returns the definition + can't be called outside the flask app context + """ + app = Flask(__name__) + Swagger(app, config=swagger_config, merge=True) + route = swagger_config['specs'][0]['route'] + client = app.test_client() + response = client.get(route) + spec = response.json + # 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 + # from the spec + del spec['definitions'] + return spec + + def write_schema(swagger_config, file_path): """ Dump schema to specified file """ - json_schema = json.dumps(swagger_config, indent=2) + spec = compile_schema(swagger_config) + json_schema = json.dumps(spec, indent=2) + with open(file_path, "w") as f: f.write(json_schema)