|
| 1 | +def generate_markdown(api_spec, title): |
| 2 | + md = [] |
| 3 | + |
| 4 | + # Info |
| 5 | + info = api_spec.get("info", {}) |
| 6 | + if title: |
| 7 | + words = [word.capitalize() for word in title.split(" ")] |
| 8 | + title = " ".join(words) |
| 9 | + md.append(f"# {title}") |
| 10 | + else: |
| 11 | + md.append(f"# {info.get('title', 'API Documentation')}") |
| 12 | + md.append(f"\n## Version: {info.get('version', 'N/A')}\n") |
| 13 | + |
| 14 | + # Paths |
| 15 | + md.append("### Paths\n") |
| 16 | + paths = api_spec.get("paths", {}) |
| 17 | + for path, methods in paths.items(): |
| 18 | + for method, details in methods.items(): |
| 19 | + md.append(f"#### {path}\n") |
| 20 | + md.append(f"**{method.upper()}**\n") |
| 21 | + md.append(f"**Summary:** {details.get('summary', 'No summary')}\n") |
| 22 | + md.append(f"**Operation ID:** {details.get('operationId', 'N/A')}\n") |
| 23 | + |
| 24 | + # Parameters |
| 25 | + parameters = details.get("parameters", []) |
| 26 | + if parameters: |
| 27 | + md.append("**Parameters:**\n") |
| 28 | + md.append("| Name | In | Required | Schema | Description | Example |") |
| 29 | + md.append("|------|----|----------|--------|-------------|---------|") |
| 30 | + for param in parameters: |
| 31 | + name = param.get("name", "N/A") |
| 32 | + param_in = param.get("in", "N/A") |
| 33 | + required = param.get("required", False) |
| 34 | + schema = param.get("schema", {}) |
| 35 | + schema_str = f"`type: {schema.get('type', 'N/A')}`<br>`title: {schema.get('title', 'N/A')}`" |
| 36 | + if "description" in schema: |
| 37 | + schema_str += f"<br>`description: {schema.get('description', 'N/A')}`" |
| 38 | + if "enum" in schema: |
| 39 | + schema_str += f"<br>`enum: {schema.get('enum')}`" |
| 40 | + if "default" in schema: |
| 41 | + schema_str += f"<br>`default: {schema.get('default')}`" |
| 42 | + description = param.get("description", "N/A") |
| 43 | + example = param.get("example", "N/A") |
| 44 | + md.append( |
| 45 | + f"| {name} | {param_in} | {required} | {schema_str} | {description} | {example} |" |
| 46 | + ) |
| 47 | + |
| 48 | + # Request Body |
| 49 | + request_body = details.get("requestBody", {}) |
| 50 | + if request_body: |
| 51 | + md.append("**Request Body:**") |
| 52 | + md.append(f"- **Required:** {request_body.get('required', False)}") |
| 53 | + md.append(f"- **Content:**") |
| 54 | + content = request_body.get("content", {}) |
| 55 | + for content_type, content_schema in content.items(): |
| 56 | + md.append(f" - **{content_type}:**") |
| 57 | + schema_ref = content_schema.get("schema", {}).get("$ref", "N/A") |
| 58 | + md.append( |
| 59 | + f" - **Schema:** [{schema_ref.split('/')[-1]}](#{schema_ref.split('/')[-1].lower()})\n" |
| 60 | + ) |
| 61 | + |
| 62 | + # Responses |
| 63 | + responses = details.get("responses", {}) |
| 64 | + if responses: |
| 65 | + md.append("**Responses:**\n") |
| 66 | + md.append("| Status Code | Description | Content |") |
| 67 | + md.append("|-------------|-------------|---------|") |
| 68 | + for status, response in responses.items(): |
| 69 | + description = response.get("description", "N/A") |
| 70 | + content = response.get("content", {}) |
| 71 | + content_str = "" |
| 72 | + for content_type, content_schema in content.items(): |
| 73 | + schema_ref = content_schema.get("schema", {}).get("$ref", "N/A") |
| 74 | + content_str += f"{content_type}: `schema: [{schema_ref.split('/')[-1]}](#{schema_ref.split('/')[-1].lower()})`<br>" |
| 75 | + md.append(f"| {status} | {description} | {content_str.strip('<br>')} |") |
| 76 | + |
| 77 | + # Components |
| 78 | + components = api_spec.get("components", {}).get("schemas", {}) |
| 79 | + if components: |
| 80 | + md.append("### Components") |
| 81 | + md.append("#### Schemas") |
| 82 | + for schema_name, schema_details in components.items(): |
| 83 | + md.append(f"##### {schema_name}") |
| 84 | + md.append(f"- **Type:** {schema_details.get('type', 'N/A')}") |
| 85 | + if "required" in schema_details: |
| 86 | + md.append(f"- **Required:** {', '.join(schema_details.get('required', []))}") |
| 87 | + md.append(f"- **Title:** {schema_details.get('title', 'N/A')}") |
| 88 | + md.append("- **Properties:**") |
| 89 | + properties = schema_details.get("properties", {}) |
| 90 | + for prop_name, prop_details in properties.items(): |
| 91 | + prop_type = prop_details.get("type", "N/A") |
| 92 | + prop_format = prop_details.get("format", "N/A") |
| 93 | + prop_title = prop_details.get("title", "N/A") |
| 94 | + prop_desc = prop_details.get("description", "N/A") |
| 95 | + md.append(f" - **{prop_name}:**") |
| 96 | + md.append(f" - **Type:** {prop_type}") |
| 97 | + if prop_format != "N/A": |
| 98 | + md.append(f" - **Format:** {prop_format}") |
| 99 | + md.append(f" - **Title:** {prop_title}") |
| 100 | + md.append(f" - **Description:** {prop_desc}") |
| 101 | + |
| 102 | + return "\n".join(md) |
0 commit comments