From 781077a7361085d820ffac1999ec7b1ff7770d54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Gorej?= Date: Thu, 2 Nov 2023 10:52:37 +0100 Subject: [PATCH] fix: create default OpenAPI.servers fields if omitted (#3219) This change is specific to OpenAPI 3.x.y definitions. Refs #3143 --- src/helpers/index.js | 1 + src/helpers/is-http-url.js | 5 +++++ src/index.js | 11 ++++++++--- 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 src/helpers/is-http-url.js diff --git a/src/helpers/index.js b/src/helpers/index.js index 51ee44d34..a3e71f894 100755 --- a/src/helpers/index.js +++ b/src/helpers/index.js @@ -4,3 +4,4 @@ export { default as getOperationRaw } from './get-operation-raw.js'; export { default as opId } from './op-id.js'; export { default as idFromPathMethod } from './id-from-path-method/index.js'; export { default as idFromPathMethodLegacy } from './id-from-path-method/legacy.js'; +export { default as isHttpUrl } from './is-http-url.js'; diff --git a/src/helpers/is-http-url.js b/src/helpers/is-http-url.js new file mode 100644 index 000000000..c6296ca5b --- /dev/null +++ b/src/helpers/is-http-url.js @@ -0,0 +1,5 @@ +import { url } from '@swagger-api/apidom-reference/configuration/empty'; + +const { isHttpUrl } = url; + +export default isHttpUrl; diff --git a/src/index.js b/src/index.js index 55c7b2179..28e0b175d 100644 --- a/src/index.js +++ b/src/index.js @@ -8,7 +8,8 @@ import openApi30ResolveStrategy from './resolver/strategies/openapi-3-0/index.js import openApi31ApiDOMResolveStrategy from './resolver/strategies/openapi-3-1-apidom/index.js'; import { makeApisTagOperation } from './interfaces.js'; import { execute, buildRequest, baseUrl } from './execute/index.js'; -import { opId } from './helpers/index.js'; +import { opId, isHttpUrl } from './helpers/index.js'; +import { isOpenAPI2, isOpenAPI3 } from './helpers/openapi-predicates.js'; import HttpResolverSwaggerClient from './resolver/apidom/reference/resolve/resolvers/http-swagger-client/index.js'; import JsonParser from './resolver/apidom/reference/parse/parsers/json/index.js'; import YamlParser from './resolver/apidom/reference/parse/parsers/yaml-1-2/index.js'; @@ -131,8 +132,8 @@ Swagger.prototype = { Swagger.prototype.applyDefaults = function applyDefaults() { const { spec } = this; const specUrl = this.url; - // TODO: OAS3: support servers here - if (specUrl && specUrl.startsWith('http')) { + + if (isOpenAPI2(spec) && isHttpUrl(specUrl)) { const parsed = new URL(specUrl); if (!spec.host) { spec.host = parsed.host; @@ -143,6 +144,10 @@ Swagger.prototype.applyDefaults = function applyDefaults() { if (!spec.basePath) { spec.basePath = '/'; } + } else if (isOpenAPI3(spec) && isHttpUrl(specUrl)) { + if (!spec.servers) { + spec.servers = [{ url: specUrl }]; + } } };