Swagger Editor import vs Swagger UI hosting for restoring an OpenAPI backup with relative $refs
0 reputation · 13 Dec 2023, 22:20 UTC
0 reputation · 13 Dec 2023, 22:20 UTC
I keep backups of an OpenAPI 3.0 specification split across multiple files: a root openapi.yaml plus sibling schema files referenced through relative $ref paths. When I restore a backup to a new folder, the directory layout may not match the original, and I need the restored spec to be complete and verifiable.
There seem to be two documented ways to work with the restored files: importing the root file into Swagger Editor, or serving the folder over HTTP and loading it in Swagger UI. My understanding is that Editor resolves references in the browser with more relaxed handling, while UI enforces stricter same-origin rules and resolves relative paths against the served base URL. That suggests the same restored backup could render fully in one tool and show missing operations in the other, without an obvious error message.
Assume current stable releases of both tools, with the bundled Swagger Parser version unverified.
Which approach is more reliable for confirming that a restored multi-file spec is intact? Should I trust the rendered operation list in either tool, or is independent validation (for example swagger-cli validate) required in both cases? And when relative $ref targets move outside the spec's directory after restore, is there a documented way to make resolution deterministic across both tools?
33275 reputation · 14 Dec 2023, 04:57 UTC
When you restore a split OpenAPI 3.0 backup into a new folder, the safest way to confirm that every $ref is resolvable is to run a deterministic validation tool first. Swagger‑CLI validate (or a programmatic call to swagger‑parser) guarantees that the spec can be fully resolved without browser quirks. After that, you can use either Swagger Editor or Swagger UI to view the rendered UI, but you should treat the operation list in those tools as a visual aid rather than a definitive proof of correctness.
file:// scheme. It resolves $refs against the local file system and will continue to render operations even if some refs cannot be found, often only logging a warning in the console.$refs relative to the base URL and enforces same‑origin rules. A missing file will surface as a 404 or a CORS error, making the failure obvious in the UI console.npx swagger-cli validate openapi.yaml
# or
npx swagger-cli validate --strict openapi.yaml
Any unresolved refs will be listed in the console. The command also produces a fully resolved JSON if you add --output json, which you can compare to the UI’s internal JSON.
const Parser = require('swagger-parser');
Parser.validate('openapi.yaml').then(resolved => {
console.log(JSON.stringify(resolved, null, 2));
});
npx serve ..
http://localhost:5000/openapi.yaml.
swagger-cli validate.openapi.yaml into the Editor.
swagger-cli.
openapi.yaml to point to the correct location, or
--cwd flag with swagger-cli to set the base directory, or
There is no built‑in feature in either tool to automatically redirect external refs; you must adjust the file structure or provide the external files in the same HTTP context.
Both tools rely on the same swagger-parser library, but different embedded versions can change resolution behaviour. If you notice inconsistent results, check the parser version in each tool’s package.json or run npm ls swagger-parser in the tool’s install folder.
One detail that would change the recommendation is whether any $ref points to a file outside the restored directory. If that is the case, the above workflow still applies, but you’ll need to either include those external files in the same HTTP server or adjust the paths to use absolute URLs that are reachable from the client.
Does any $ref in your restored backup point to a file outside the restored folder?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.