From 639eaca0818cfddcbda734274f19ac0e64291d15 Mon Sep 17 00:00:00 2001 From: AdrianP <38519157+panaaj@users.noreply.github.com> Date: Wed, 17 May 2023 03:54:38 +0930 Subject: [PATCH] fix: Use servers array in plugin openApi.json. (#1554) * Use servers array in plugin openApi.json. * Remove reference to demo.signalk.org * doc: add servers note * refactor: rename for clarity --------- Co-authored-by: Teppo Kurki --- SERVERPLUGINS.md | 2 +- src/api/swagger.ts | 13 ++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/SERVERPLUGINS.md b/SERVERPLUGINS.md index 0d0bc89f6..3ee4ddd72 100644 --- a/SERVERPLUGINS.md +++ b/SERVERPLUGINS.md @@ -918,7 +918,7 @@ Express does not have a public API for deregistering subrouters, so `stop` does **Consider seriously providing an OpenApi description** for your plugin's API. This promotes further cooperation with other plugin/webapp authors. One way we can work together is by fleshing out new APIs within a plugin and then merge them in the Signal K specification for more cooperation. -Implement `getOpenApi` in your plugin to expose your OpenApi to be included in the server's OpenApi UI tooling. See [testplugin](https://github.com/SignalK/signalk-server/tree/b82477e63ebdc14878164ce1ed3aedd80c5a8b0c/test/plugin-test-config/node_modules/testplugin) for an example. +Implement `getOpenApi` in your plugin to expose your OpenApi to be included in the server's OpenApi UI tooling. The plugin's OpenApi description either not include `servers` property at all or specify only the path in the url. The server will provide servers if it is missing and relative, path only server urls work best in different environments. See [testplugin](https://github.com/SignalK/signalk-server/tree/b82477e63ebdc14878164ce1ed3aedd80c5a8b0c/test/plugin-test-config/node_modules/testplugin) for an example. ## Plugin configuration HTTP API diff --git a/src/api/swagger.ts b/src/api/swagger.ts index 7b3eeeb9b..028b8719e 100644 --- a/src/api/swagger.ts +++ b/src/api/swagger.ts @@ -79,18 +79,13 @@ export function mountSwaggerUi(app: IRouter & PluginManager, path: string) { apiRecord = app.getPluginOpenApi(req.params.pluginId as PluginId) } const apiDoc = apiRecord?.apiDoc - const apiDocPath = apiRecord?.path + const apiPath = apiRecord?.path - if (apiDoc && apiDocPath !== undefined) { + if (apiDoc && apiPath !== undefined) { // eslint-disable-next-line @typescript-eslint/no-explicit-any - ;(apiDoc as any).servers = [ + ;(apiDoc as any).servers = (apiDoc as any).servers ?? [ { - url: `${process.env.PROTOCOL ? 'https' : req.protocol}://${req.get( - 'Host' - )}${apiDocPath}` - }, - { - url: `https://demo.signalk.org${apiDocPath}` + url: `${apiPath}` } ] res.json(apiDoc)