Server variable resolution diverges between local and production builds
0 reputation · 09 Nov 2020, 20:46 UTC
0 reputation · 09 Nov 2020, 20:46 UTC
Goal: achieve identical base‑URL resolution for OpenAPI server variables whether the spec is consumed locally or in production.
Constraints: local development typically targets http://localhost:{port} while production uses a fixed domain; the servers object may contain variables such as {basePath} or {environment}. Generators treat missing variables differently — some fall back to the first server entry, others emit validation errors. Security schemes can be bound to a specific server, so environment‑specific bindings are not automatically transferred.
Uncertainty: it is unclear whether the divergence stems from default‑server selection, variable substitution rules, or generator version differences. The spec does not prescribe a universal fallback strategy.
Which resolution strategy does the generator apply when a variable is omitted in the production spec? How can the spec declare a fallback that works for both environments without hard‑coding hosts? What validation steps confirm that generated clients request the correct base URL in each deployment?
When a server variable is omitted in the spec, most OpenAPI generators (e.g., OpenAPI Generator v6.x, Swagger Codegen v3) fallback to the first server object in the servers array and substitute any undefined variables with that server’s default values. If the first server does not provide a default for a missing variable, the placeholder literal (e.g., {basePath}) is left unchanged, which can produce malformed URLs.
The difference between local and production builds arises because the generator picks the first server entry as the fallback. If the first entry is the production server, local builds will inherit its host (or placeholder) unless a localhost‑specific server appears earlier in the array.
servers:
- url: https://{environment}.example.com/{basePath}
variables:
environment:
default: "prod"
basePath:
default: "api"
- url: http://localhost:{port}/{basePath}
variables:
port:
default: "8080"
basePath:
default: "api"
http://localhost:{port} by fallback.security block on both server objects so the fallback retains authentication.Configuration.basePath or apiClient.getBasePath()).http://localhost:{port} when the localhost server is selected and equals the production domain when the production server is selected.To confirm whether the fallback‑to‑first‑server rule applies in your case, please specify: What version of the OpenAPI generator are you using? (Versions <5.x may require an upgrade or explicit defaults to avoid generation errors.)
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.