|
| 1 | +# Copyright (C) 2023 IKUS Software. All rights reserved. |
| 2 | +# IKUS Software inc. PROPRIETARY/CONFIDENTIAL. |
| 3 | +# Use is subject to license terms. |
| 4 | +# |
| 5 | +# This script is used to generate openapi.json file offline. |
| 6 | +# |
| 7 | +# Did not find an alternative to generate markdown or rst from my spec file. |
| 8 | +# Either the solution is obsolete or failing |
| 9 | +# So let use this quick and simple solution. |
| 10 | +import json |
| 11 | +import os |
| 12 | + |
| 13 | +import cherrypy |
| 14 | +from minarca_server.app import MinarcaApplication |
| 15 | +from minarca_server.config import parse_args |
| 16 | +from rdiffweb.controller.api_openapi import OpenAPI |
| 17 | + |
| 18 | +# Change location of cwd for script location. |
| 19 | +os.chdir(os.path.dirname(os.path.abspath(__file__))) |
| 20 | + |
| 21 | +# Generate spec file. |
| 22 | +cfg = parse_args( |
| 23 | + args=['--database-uri', 'sqlite://', '--minarca-user-dir-owner', 'nobody', '--minarca-user-dir-group', 'nogroup'] |
| 24 | +) |
| 25 | +app = cherrypy.request.app = MinarcaApplication(cfg) |
| 26 | +api_spec = OpenAPI()._generate_spec(app.root) |
| 27 | +with open("_static/openapi.json", 'w') as fp: |
| 28 | + json.dump(api_spec, fp) |
| 29 | + |
| 30 | + |
| 31 | +def generate_markdown_for_path(path, methods): |
| 32 | + md_content = [] |
| 33 | + for method, details in methods.items(): |
| 34 | + md_content.append(f"\n### {method.upper()} {path}") |
| 35 | + |
| 36 | + if "description" in details: |
| 37 | + md_content.append(f"**Description:**\n{details['description']}\n") |
| 38 | + |
| 39 | + # Parameters |
| 40 | + parameters = details.get("parameters", []) |
| 41 | + if parameters: |
| 42 | + md_content.append("**Parameters:**") |
| 43 | + for param in parameters: |
| 44 | + param_name = param.get('name', 'Unnamed') |
| 45 | + param_in = param.get('in', 'unknown') |
| 46 | + param_required = " Required" if param.get('required', False) else "" |
| 47 | + param_default = f" Default: {param.get('default')}" if "default" in param else "" |
| 48 | + md_content.append(f"- **{param_name}** (in {param_in}){param_required}{param_default}") |
| 49 | + md_content.append("") |
| 50 | + |
| 51 | + # Responses |
| 52 | + md_content.append("**Responses:**") |
| 53 | + for status, response in details.get("responses", {}).items(): |
| 54 | + md_content.append(f"- **{status}**: {response.get('description', 'No description')}") |
| 55 | + content = response.get("content", {}) |
| 56 | + if content: |
| 57 | + md_content.append(" - **Content:** " + ", ".join(content.keys())) |
| 58 | + |
| 59 | + return md_content |
| 60 | + |
| 61 | + |
| 62 | +def generate_markdown_from_openapi(openapi_json, output_file): |
| 63 | + """ |
| 64 | + Generate a Markdown file from an OpenAPI JSON specification. |
| 65 | +
|
| 66 | + :param openapi_json: Path to the OpenAPI JSON file. |
| 67 | + :param output_file: Path to the output Markdown file. |
| 68 | + """ |
| 69 | + with open(openapi_json, "r", encoding="utf-8") as f: |
| 70 | + spec = json.load(f) |
| 71 | + |
| 72 | + md_content = [] |
| 73 | + |
| 74 | + # Separate API and non-API endpoints |
| 75 | + md_content.append("# All Endpoints") |
| 76 | + |
| 77 | + md_content.append("## REST API Endpoints") |
| 78 | + |
| 79 | + for path in sorted(spec.get("paths", [])): |
| 80 | + if path.startswith('/api'): |
| 81 | + md_content.extend(generate_markdown_for_path(path, spec["paths"][path])) |
| 82 | + |
| 83 | + md_content.append("## Other Endpoints") |
| 84 | + |
| 85 | + for path in sorted(spec.get("paths", [])): |
| 86 | + if not path.startswith('/api'): |
| 87 | + md_content.extend(generate_markdown_for_path(path, spec["paths"][path])) |
| 88 | + |
| 89 | + # Write to file |
| 90 | + with open(output_file, "w", encoding="utf-8") as f: |
| 91 | + f.write("\n".join(md_content)) |
| 92 | + |
| 93 | + print(f"Markdown documentation generated: {output_file}") |
| 94 | + |
| 95 | + |
| 96 | +# Example usage |
| 97 | +generate_markdown_from_openapi("_static/openapi.json", "endpoints.md") |
0 commit comments