From a4bc5711181f04e9d6592185e2875b8d988d9a8e Mon Sep 17 00:00:00 2001 From: Damir Date: Thu, 4 Mar 2021 00:55:54 +0300 Subject: [PATCH 01/11] pathname vs server url (fix) --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 24d12ba..4c2617a 100644 --- a/src/index.js +++ b/src/index.js @@ -37,7 +37,7 @@ function buildValidations(referenced, dereferenced, receivedOptions) { const schemas = {}; const basePaths = dereferenced.servers && dereferenced.servers.length - ? dereferenced.servers.map(({ url }) => new URL(url).pathname) + ? dereferenced.servers.map(({ url }) => url.slice(0, 1) === "/" ? url : new URL(url).pathname) : [dereferenced.basePath || '/']; Object.keys(dereferenced.paths).forEach(currentPath => { From 148ef4b7704bb55bf651fd98b2cc1b0b57f989ac Mon Sep 17 00:00:00 2001 From: Damir Date: Thu, 4 Mar 2021 12:01:16 +0300 Subject: [PATCH 02/11] Update index.js Fix quotes --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 4c2617a..52ec148 100644 --- a/src/index.js +++ b/src/index.js @@ -37,7 +37,7 @@ function buildValidations(referenced, dereferenced, receivedOptions) { const schemas = {}; const basePaths = dereferenced.servers && dereferenced.servers.length - ? dereferenced.servers.map(({ url }) => url.slice(0, 1) === "/" ? url : new URL(url).pathname) + ? dereferenced.servers.map(({ url }) => url.slice(0, 1) === '/' ? url : new URL(url).pathname) : [dereferenced.basePath || '/']; Object.keys(dereferenced.paths).forEach(currentPath => { From b42edb8014ce4de1af75677423ba99a4c1f4a2e2 Mon Sep 17 00:00:00 2001 From: Damir Date: Thu, 4 Mar 2021 12:48:02 +0300 Subject: [PATCH 03/11] test for relative path support --- test/openapi3/general/pets-general.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/openapi3/general/pets-general.yaml b/test/openapi3/general/pets-general.yaml index 3337e82..c77566d 100644 --- a/test/openapi3/general/pets-general.yaml +++ b/test/openapi3/general/pets-general.yaml @@ -84,6 +84,7 @@ paths: servers: - url: http://petstore.swagger.io - url: http://petstore.swagger.io/staging/ +- url: /staging components: schemas: Error: From 99bd90c84cf46e498ef19f459a6cebce224a0907 Mon Sep 17 00:00:00 2001 From: Damir Date: Thu, 4 Mar 2021 13:23:41 +0300 Subject: [PATCH 04/11] Added support for basic relative URLs and variables/variable substitutions in URLs in servers --- src/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 52ec148..e2204be 100644 --- a/src/index.js +++ b/src/index.js @@ -37,7 +37,12 @@ function buildValidations(referenced, dereferenced, receivedOptions) { const schemas = {}; const basePaths = dereferenced.servers && dereferenced.servers.length - ? dereferenced.servers.map(({ url }) => url.slice(0, 1) === '/' ? url : new URL(url).pathname) + ? dereferenced.servers.map(({ url, variables = {} }) => { + Object.keys(variables).forEach((key) => { + url = url.replace(new RegExp(`{${key}}`), variables[key].default || '') + }) + return url.slice(0,1) === '/' ? url : new URL(url).pathname + }) : [dereferenced.basePath || '/']; Object.keys(dereferenced.paths).forEach(currentPath => { From 620b4dff03747c7891042f685eacb76f620a99cb Mon Sep 17 00:00:00 2001 From: Damir Date: Thu, 4 Mar 2021 13:26:42 +0300 Subject: [PATCH 05/11] eslint fix --- src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index e2204be..8047526 100644 --- a/src/index.js +++ b/src/index.js @@ -39,9 +39,9 @@ function buildValidations(referenced, dereferenced, receivedOptions) { const basePaths = dereferenced.servers && dereferenced.servers.length ? dereferenced.servers.map(({ url, variables = {} }) => { Object.keys(variables).forEach((key) => { - url = url.replace(new RegExp(`{${key}}`), variables[key].default || '') - }) - return url.slice(0,1) === '/' ? url : new URL(url).pathname + url = url.replace(new RegExp(`{${key}}`), variables[key].default || ''); + }); + return url.slice(0, 1) === '/' ? url : new URL(url).pathname; }) : [dereferenced.basePath || '/']; From 6b89a536b9aec558483984dc050cf6a1c3098c54 Mon Sep 17 00:00:00 2001 From: Damir Date: Thu, 4 Mar 2021 13:34:39 +0300 Subject: [PATCH 06/11] Added test for variables in servers url --- test/openapi3/general/pets-general.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/openapi3/general/pets-general.yaml b/test/openapi3/general/pets-general.yaml index c77566d..e1634a6 100644 --- a/test/openapi3/general/pets-general.yaml +++ b/test/openapi3/general/pets-general.yaml @@ -85,6 +85,18 @@ servers: - url: http://petstore.swagger.io - url: http://petstore.swagger.io/staging/ - url: /staging +- url: '{protocol}://petstore.swagger.io/{env}' + variables: + protocol: + enum: + - http + - https + default: http + env: + enum: + - staging + - '' + default: '' components: schemas: Error: From 9579c030383ddcff9bc3526a3a61957076f6b038 Mon Sep 17 00:00:00 2001 From: Damir Date: Thu, 4 Mar 2021 17:30:23 +0300 Subject: [PATCH 07/11] Minor: Updated CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c99b77..8a2d294 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Master +# 2.0.5 - 4 March, 2021 +### Improvements +- Added support for relative URLs and variables in servers #59 + # 2.0.4 - 26 Nov, 2020 ### Improvements - Nullable is now fully supported From 2dba574363c4d8b79b8b050b9f3f116a3473a4b6 Mon Sep 17 00:00:00 2001 From: Damir Date: Sun, 7 Mar 2021 23:18:52 +0300 Subject: [PATCH 08/11] refactoring --- src/index.js | 26 +++++++++++++++++-------- test/openapi3/general/pets-general.yaml | 3 ++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index 8047526..323c41e 100644 --- a/src/index.js +++ b/src/index.js @@ -31,19 +31,29 @@ function buildSchemaSync(pathOrSchema, options) { return buildValidations(jsonSchema, dereferencedSchema, options); } +function getBasePaths(dereferenced) { + return dereferenced.servers && dereferenced.servers.length + ? dereferenced.servers.map(({ url, variables = {} }) => { + // replace variables with deafault values + Object.keys(variables).forEach((key) => { + url = url.replace(new RegExp(`{${key}}`), variables[key].default); + }); + + // replace leading '//' with 'http://' (in cases such as //api.example.com) + url = url.replace(/^\/\//, 'http://'); + + // return base path + return (/\/\//.test(url)) ? new URL(url).pathname : url; + }) + : [dereferenced.basePath || '/']; +} + function buildValidations(referenced, dereferenced, receivedOptions) { const options = getOptions(receivedOptions); const schemas = {}; - const basePaths = dereferenced.servers && dereferenced.servers.length - ? dereferenced.servers.map(({ url, variables = {} }) => { - Object.keys(variables).forEach((key) => { - url = url.replace(new RegExp(`{${key}}`), variables[key].default || ''); - }); - return url.slice(0, 1) === '/' ? url : new URL(url).pathname; - }) - : [dereferenced.basePath || '/']; + const basePaths = getBasePaths(dereferenced); Object.keys(dereferenced.paths).forEach(currentPath => { const operationSchemas = {}; diff --git a/test/openapi3/general/pets-general.yaml b/test/openapi3/general/pets-general.yaml index e1634a6..75545e9 100644 --- a/test/openapi3/general/pets-general.yaml +++ b/test/openapi3/general/pets-general.yaml @@ -96,7 +96,8 @@ servers: enum: - staging - '' - default: '' + default: staging +- url: //petstore.swagger.io components: schemas: Error: From c210ce8c28de1672604e7e209574a269ec4c5b69 Mon Sep 17 00:00:00 2001 From: Damir Date: Sun, 7 Mar 2021 23:24:06 +0300 Subject: [PATCH 09/11] indentation fix --- src/index.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 323c41e..db60a6b 100644 --- a/src/index.js +++ b/src/index.js @@ -33,19 +33,19 @@ function buildSchemaSync(pathOrSchema, options) { function getBasePaths(dereferenced) { return dereferenced.servers && dereferenced.servers.length - ? dereferenced.servers.map(({ url, variables = {} }) => { + ? dereferenced.servers.map(({ url, variables = {} }) => { // replace variables with deafault values - Object.keys(variables).forEach((key) => { - url = url.replace(new RegExp(`{${key}}`), variables[key].default); - }); + Object.keys(variables).forEach((key) => { + url = url.replace(new RegExp(`{${key}}`), variables[key].default); + }); - // replace leading '//' with 'http://' (in cases such as //api.example.com) - url = url.replace(/^\/\//, 'http://'); + // replace leading '//' with 'http://' (in cases such as //api.example.com) + url = url.replace(/^\/\//, 'http://'); - // return base path - return (/\/\//.test(url)) ? new URL(url).pathname : url; - }) - : [dereferenced.basePath || '/']; + // return base path + return (/\/\//.test(url)) ? new URL(url).pathname : url; + }) + : [dereferenced.basePath || '/']; } function buildValidations(referenced, dereferenced, receivedOptions) { From 3d8a5066adc9ef4ee95b24e4f8d431e5b35d5589 Mon Sep 17 00:00:00 2001 From: Damir Date: Wed, 10 Mar 2021 11:34:13 +0300 Subject: [PATCH 10/11] Update CHANGELOG.md Move this improvement to next version --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 760fb7d..31a3804 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,12 @@ # Master +# 2.0.6 - 10 Mar, 2021 +### Improvements +- Added support for relative URLs and variables in servers #59 + # 2.0.5 - 08 Mar, 2021 ### Improvements - Update dependencies to fix security vulnerabilities -- Added support for relative URLs and variables in servers #59 # 2.0.4 - 26 Nov, 2020 ### Improvements From 6e71068db773467b3c880e6a97a0bf098024a031 Mon Sep 17 00:00:00 2001 From: Damir Date: Sat, 27 Mar 2021 14:07:26 +0300 Subject: [PATCH 11/11] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31a3804..684870b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Master -# 2.0.6 - 10 Mar, 2021 +# 2.0.6 - 27 Mar, 2021 ### Improvements - Added support for relative URLs and variables in servers #59