Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(swagger): target the serving origin from Try-it
Yudhi Armyndharisb6bdfc0b2bf3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4a012c80a..8f5645e24 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]+### Fixed++- **Swagger "Try it out" called `http://localhost:2785` instead of the host that served the docs**, failing with `Failed to fetch` anywhere else — a relative server is now listed first, so it resolves to the serving origin.+## [0.14.0] - 2026-08-05### Addeddiff --git a/openapi.json b/openapi.jsonindex c3de8e216..c725017ed 100644--- a/openapi.json+++ b/openapi.json@@ -7205,9 +7205,13 @@}],"servers": [+ {+ "url": "/",+ "description": "This instance (the origin serving these docs)"+ },{"url": "http://{host}:{port}",- "description": "OpenWA instance",+ "description": "Another instance (set host and port)","variables": {"host": {"default": "localhost"diff --git a/src/config/swagger.config.spec.ts b/src/config/swagger.config.spec.tsindex 2641d6963..48ccea4c6 100644--- a/src/config/swagger.config.spec.ts+++ b/src/config/swagger.config.spec.ts@@ -25,14 +25,26 @@ describe('createSwaggerConfig', () => {expect(config.security).not.toContainEqual({ [METRICS_BEARER_SCHEME]: [] });});- it('declares a templated default server so published specs carry a base URL', () => {+ // Swagger UI aims "Try it" at servers[0]. A relative URL resolves against whatever origin served+ // the docs, so it works on localhost, a LAN address and behind a TLS proxy alike; anything+ // absolute here would send the reader's browser somewhere else entirely (#1068).+ it('lists a relative server FIRST so Try-it targets the origin serving the docs', () => {const config = createSwaggerConfig();- expect(config.servers).toHaveLength(1);- const [server] = config.servers ?? [];- expect(server.url).toBe('http://{host}:{port}');- expect(server.variables?.host.default).toBe('localhost');- expect(server.variables?.port.default).toBe('2785');+ const [first] = config.servers ?? [];+ expect(first.url).toBe('/');+ expect(first.url.startsWith('http')).toBe(false);+ });++ it('keeps the templated absolute server so published specs still carry a concrete base URL', () => {+ const config = createSwaggerConfig();++ expect(config.servers).toHaveLength(2);+ const templated = (config.servers ?? []).find(s => s.url.includes('{host}'));+ expect(templated).toBeDefined();+ expect(templated?.url).toBe('http://{host}:{port}');+ expect(templated?.variables?.host.default).toBe('localhost');+ expect(templated?.variables?.port.default).toBe('2785');});});diff --git a/src/config/swagger.config.ts b/src/config/swagger.config.tsindex b308d3e3e..39a6be114 100644--- a/src/config/swagger.config.ts+++ b/src/config/swagger.config.ts@@ -96,11 +96,20 @@ export function createSwaggerConfig(): Omit<OpenAPIObject, 'paths'> {.addTag('audit', 'Audit log').addTag('metrics', 'Prometheus metrics').addTag('health', 'Health check endpoints')- // Templated server: defaults to the out-of-box local instance, and the {host}/{port}- // variables keep Swagger UI "Try it" usable on real deployments (a hardcoded- // localhost URL would break Try-it for anyone serving elsewhere). Static consumers- // of the spec (the docs site) also get a concrete base URL to display.- .addServer('http://{host}:{port}', 'OpenWA instance', {+ // ORDER MATTERS. Swagger UI resolves "Try it" against servers[0], substituting the variable+ // defaults — it does not consider the origin the page was served from. A templated server+ // alone therefore aimed every request at `http://localhost:2785`, so on any deployment that+ // is not exactly that (a LAN address, a different PORT, a TLS proxy) Try-it called the+ // reader's own machine and failed with "Failed to fetch" — the browser's CSP `connect-src+ // 'self'` rejects the cross-origin call before it is even sent (#1068). A relative URL is+ // resolved against the document's own location, which is what OpenAPI 3 specifies and what+ // the spec did implicitly before it declared any server at all.+ //+ // So: relative first, and keep the templated absolute one second. Static consumers of+ // openapi.json still get a concrete base URL to display (#975), and the host/port editor+ // stays in the Servers dropdown for anyone pointing the docs at a different instance.+ .addServer('/', 'This instance (the origin serving these docs)')+ .addServer('http://{host}:{port}', 'Another instance (set host and port)', {host: { default: 'localhost' },port: { default: '2785', description: 'PORT env var' },})