in modular_api/swagger/generate_open_api_spec.py [0:0]
def generate_definition(commands_def, prefix) -> dict:
"""
Generate config file(openapi_spec.yaml) for OpenAPI 3.0
according to M3admin commands(commands_base.json)
"""
result_paths = {}
result_groups = []
for command_name, command_meta in commands_def.items():
description = command_meta.get('description')
command_route = command_meta.get('route', {})
method = command_route.get('method', '').lower()
path = command_route.get('path')
parameters = command_meta.get('parameters')
get_param = []
post_param = []
# generate the parameters for "get", "delete"
# requests from the command parameters
if method in ["get", "delete"]:
get_param = [{
"name": parameter.get('name'),
"required": parameter.get('required'),
"description": parameter.get('description'),
"in": "path",
"schema": {"type": resolve_parameter_type(
parameter.get('type')
)
}} for parameter in parameters]
# generate the schema and the example of body-requests
# for "post", "put", "patch"
# requests from command parameters
elif method in ["post", "put", "patch"]:
properties = {
parameter.get("name"): {
'type': resolve_parameter_type(parameter.get('type')),
'description': parameter.get("description"),
'required': parameter.get("required")
} for parameter in parameters
}
example = {parameter.get("name"): resolve_parameter_type(
parameter.get('type')
) for parameter in parameters}
post_param = {
"description": "description of requestBody",
"required": True,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": properties},
"example": example}
}
}
# form path for config file
group_name = resolve_group_name(command_path=command_name)
command_definition = {
path: {
method: {
"summary": description,
"description": description,
"parameters": get_param,
"tags": [group_name],
"security": [{'BasicAuth': []}, {'BearerAuth': []}],
"requestBody": post_param,
"responses": RESPONSES
}
}
}
result_paths.update(command_definition)
result_groups.append({'name': group_name})
open_api_spec_template = get_open_api_spec_template(prefix=prefix)
open_api_spec_template["tags"] = result_groups
open_api_spec_template["paths"] = result_paths
return open_api_spec_template