如何使用Redocly为每个API端点生成独立API规范文档
I have an API with 73 endpoints. I used Redocly to split the
openapi.jsonfile into separatePathsandComponentsfolders. Now I want to generate a corresponding API specification document based on each individual Path file. Thebundlecommand doesn't meet my needs because it relies on the originalopenapi.jsonfile to build a new document from the split files. Thejoincommand merges multipleopenapi.jsondocuments into one, which also doesn't fit. I don't want to manually parse the Path files and build the documents myself—are there any overlooked commands that can achieve this?
Redocly's built-in bundle and join commands don't directly support generating standalone API specs from individual split Path files, and there's no hidden command for this exact use case. But you can avoid manual parsing with these two workarounds:
1. Custom Script + Redocly bundle
Put together a simple script (Node.js works well here) to automate the process:
- Grab core global fields (openapi version, info, servers, etc.) from your original
openapi.json. - Loop through each file in your
pathsfolder. For each one:- Create a temporary JSON file that combines those global fields with just the single Path entry.
- Ensure the temp file references your existing
componentsfolder—Redocly'sbundlewill automatically resolve any component references needed for that Path.
- Run
redocly bundle temp-file.json -o [path-name]-spec.jsonfor each temp file to get a complete, standalone spec for that endpoint.
2. Path Filtering via Redocly Config
Use a temporary config file to narrow down the bundle output to a single Path:
- Create a
temp-redocly.yamlfile with these settings (disables unused component warnings and targets your specific Path):lint: rules: no-unused-components: off paths: /your-target-endpoint-path: {} - Run
redocly bundle openapi.json --config temp-redocly.yaml -o [path-name]-spec.json. The result will be a spec containing only the chosen Path and its required components.
内容的提问来源于stack exchange,提问作者USMC6072

