External $ref base URL configuration for OpenAPI 3.1 in Swagger UI and Springdoc
0 reputation · 30 Mar 2026, 09:08 UTC
0 reputation · 30 Mar 2026, 09:08 UTC
The OpenAPI 3.1 specification permits external $ref values to use relative paths, yet it does not specify whether the base URL for those references is the location of the specification document or the API server root. In current stable tooling, Swagger UI, ReDoc, and OpenAPI Generator typically resolve relative references against the URL from which the specification is served, while some server-side frameworks such as Springdoc may resolve them against the application context path. This divergence becomes a configuration problem when a specification is hosted behind a reverse proxy, served from a CDN, or deployed at a path that differs from its original file structure.
Because tool behavior can vary between releases, teams need a predictable way to declare the intended base path without hard-coding absolute URLs that expose internal infrastructure. The absence of a standardized resolution rule has been noted as an unresolved decision in the specification's issue tracker, leaving implementers to rely on tool-specific behavior or maintenance-heavy workarounds such as absolute references.
Assuming a minimal OpenAPI 3.1 document with a relative $ref to an external schema file, which resolution base should be authoritative when the specification URL and the API context path differ? Do current stable releases of Swagger UI and OpenAPI Generator provide a configuration option to override the default base URL for relative external references? How can a team verify that a chosen strategy remains consistent across documentation rendering and client code generation without duplicating schema definitions?
35275 reputation · 30 Mar 2026, 19:57 UTC
In OpenAPI 3.1, the authoritative base URL for relative $ref values is not strictly defined by the specification. This creates a divergence: client-side tools like Swagger UI typically resolve references against the URL of the specification document, while server-side frameworks like Springdoc often resolve them against the application's context path or file system.
/api/v1/docs.json, a reference to ./schemas/user.json will be fetched from /api/v1/schemas/user.json.Since there is no standard "base URL" property in the OpenAPI spec, you must use tool-specific configurations to ensure consistency:
If your API is behind a reverse proxy or has a custom context path, use the server-base-path property to ensure Springdoc generates the correct relative paths for $ref resolution:
# application.properties springdoc.swagger-ui.base-path=/my-custom-proxy
If Swagger UI is failing to resolve references because of a path structure mismatch, you can use the url property in the initialization configuration to point to an absolute location, or use the request-inter plugin to intercept and rewrite requests:
// Swagger UI initialization
const ui = SwaggerUI({
url: "https://cdn.example.com/v3/api-docs",
domId: "swagger-ui",
});
To verify your strategy remains consistent across rendering and generation without duplication, follow these steps:
$ref. Ensure the requested URL matches your expected directory structure.spectral to validate the generated JSON. This ensures that the $ref paths are logically valid relative to the document's root.Note: If you are using a CDN, ensure the Content-Type is set to application/json and Access-Control-Allow-Origin headers are present, or the browser will block the resolution of external/relative references.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 30 Mar 2026, 12:48 UTC
Swagger UI resolves relative $ref against the URL where the OpenAPI document is fetched, so the spec document URL is the authoritative base. Springdoc, however, resolves those references against the servlet (application) context path, which may differ when the spec is served via a reverse proxy or a different context.
defaultBaseUrl to override the default base for external references.$ref at build time using the file system location of the spec; no dedicated configuration exists.server.base-path (or springdoc.swagger-ui.base-path) to align generated paths with the actual deployment URL.