From ab454803ede7c94d99f9542e6056897af79334b3 Mon Sep 17 00:00:00 2001 From: Oliwia Rogala Date: Wed, 8 May 2024 14:21:20 +0200 Subject: [PATCH 1/4] fix(execute): replace regular expressions in path builders Refs #3503 --- .eslintrc | 3 +- config/webpack/browser.config.babel.js | 2 +- package-lock.json | 17 +++++++++ package.json | 1 + src/execute/oas3/parameter-builders.js | 41 +++++++++++++++------- src/execute/swagger2/parameter-builders.js | 11 +++++- 6 files changed, 60 insertions(+), 15 deletions(-) diff --git a/.eslintrc b/.eslintrc index d16e0791d..a3407e899 100644 --- a/.eslintrc +++ b/.eslintrc @@ -44,7 +44,8 @@ "^@swagger-api/apidom-reference/dereference/strategies/openapi-3-1/selectors/uri$", "^@swagger-api/apidom-reference/resolve/resolvers/file$", "^@swagger-api/apidom-reference/resolve/strategies/openapi-3-1$", - "^@swagger-api/apidom-reference/parse/parsers/binary$" + "^@swagger-api/apidom-reference/parse/parsers/binary$", + "openapi-path-templating" ] } ], diff --git a/config/webpack/browser.config.babel.js b/config/webpack/browser.config.babel.js index 68deb58a6..0a6b7683e 100644 --- a/config/webpack/browser.config.babel.js +++ b/config/webpack/browser.config.babel.js @@ -63,7 +63,7 @@ const browserMin = { devtool: 'source-map', performance: { hints: 'error', - maxEntrypointSize: 440000, + maxEntrypointSize: 460000, maxAssetSize: 50000000, }, output: { diff --git a/package-lock.json b/package-lock.json index e36f364ee..332efbff3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "js-yaml": "^4.1.0", "node-abort-controller": "^3.1.1", "node-fetch-commonjs": "^3.3.2", + "openapi-path-templating": "^1.5.0", "qs": "^6.10.2", "traverse": "=0.6.8" }, @@ -4844,6 +4845,11 @@ "node": ">= 8" } }, + "node_modules/apg-lite": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/apg-lite/-/apg-lite-1.0.3.tgz", + "integrity": "sha512-lOoNkL7vN7PGdyQMFPey1aok2oVVqvs3n7UMFBRvQ9FoELSbKhgPc3rd7JptaGwCmo4125gLX9Cqb8ElvLCFaQ==" + }, "node_modules/arg": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", @@ -12406,6 +12412,17 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/openapi-path-templating": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/openapi-path-templating/-/openapi-path-templating-1.5.0.tgz", + "integrity": "sha512-CETC/tMiElOfZSj9jcViJALE2Bbio5fKlepFzhddQvJrGcpadgqw3tAeaM2SpPqVfyLOGUwesoYlfUOFLhjAHQ==", + "dependencies": { + "apg-lite": "^1.0.3" + }, + "engines": { + "node": ">=12.20.0" + } + }, "node_modules/optionator": { "version": "0.9.3", "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.3.tgz", diff --git a/package.json b/package.json index 08cdb6a5e..0a306d687 100644 --- a/package.json +++ b/package.json @@ -122,6 +122,7 @@ "js-yaml": "^4.1.0", "node-abort-controller": "^3.1.1", "node-fetch-commonjs": "^3.3.2", + "openapi-path-templating": "^1.5.0", "qs": "^6.10.2", "traverse": "=0.6.8" }, diff --git a/src/execute/oas3/parameter-builders.js b/src/execute/oas3/parameter-builders.js index fe5193a9e..fb59449a9 100644 --- a/src/execute/oas3/parameter-builders.js +++ b/src/execute/oas3/parameter-builders.js @@ -1,3 +1,6 @@ +import { resolve } from 'openapi-path-templating'; + +import { DEFAULT_BASE_URL } from '../../constants.js'; import stylize, { encodeCharacters } from './style-serializer.js'; import serialize from './content-serializer.js'; @@ -6,22 +9,36 @@ export function path({ req, value, parameter }) { if (value === undefined) return; + const url = new URL(req.url, DEFAULT_BASE_URL); + const pathname = decodeURIComponent(url.pathname); + if (content) { const effectiveMediaType = Object.keys(content)[0]; - req.url = req.url - .split(`{${name}}`) - .join(encodeCharacters(serialize(value, effectiveMediaType))); - } else { - const styledValue = stylize({ - key: parameter.name, - value, - style: style || 'simple', - explode: explode || false, - escape: 'reserved', - }); + const resolvedPathname = resolve( + pathname, + { [name]: value }, + { encoder: (val) => encodeCharacters(serialize(val, effectiveMediaType)) } + ); - req.url = req.url.replace(new RegExp(`{${name}}`, 'g'), styledValue); + req.url = req.url.replace(pathname, resolvedPathname); + } else { + const resolvedPathname = resolve( + pathname, + { [name]: value }, + { + encoder: (val) => + stylize({ + key: parameter.name, + value: val, + style: style || 'simple', + explode: explode || false, + escape: 'reserved', + }), + } + ); + + req.url = req.url.replace(pathname, resolvedPathname); } } diff --git a/src/execute/swagger2/parameter-builders.js b/src/execute/swagger2/parameter-builders.js index 1e7aefead..4da0e2226 100644 --- a/src/execute/swagger2/parameter-builders.js +++ b/src/execute/swagger2/parameter-builders.js @@ -1,3 +1,7 @@ +import { resolve } from 'openapi-path-templating'; + +import { DEFAULT_BASE_URL } from '../../constants.js'; + // These functions will update the request. // They'll be given {req, value, paramter, spec, operation}. @@ -51,7 +55,12 @@ function headerBuilder({ req, parameter, value }) { // Replace path paramters, with values ( ie: the URL ) function pathBuilder({ req, value, parameter }) { if (value !== undefined) { - req.url = req.url.replace(new RegExp(`{${parameter.name}}`, 'g'), encodeURIComponent(value)); + const url = new URL(req.url, DEFAULT_BASE_URL); + const pathname = decodeURIComponent(url.pathname); + + const resolvedPathname = resolve(pathname, { [parameter.name]: value }); + + req.url = req.url.replace(pathname, resolvedPathname); } } From 14e3e6cbe63ed10dffede61c70a5cacc48861485 Mon Sep 17 00:00:00 2001 From: Oliwia Rogala Date: Wed, 8 May 2024 14:42:23 +0200 Subject: [PATCH 2/4] update name of the import --- src/execute/oas3/parameter-builders.js | 6 +++--- src/execute/swagger2/parameter-builders.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/execute/oas3/parameter-builders.js b/src/execute/oas3/parameter-builders.js index fb59449a9..a9c0fa7cb 100644 --- a/src/execute/oas3/parameter-builders.js +++ b/src/execute/oas3/parameter-builders.js @@ -1,4 +1,4 @@ -import { resolve } from 'openapi-path-templating'; +import { resolve as resolvePathTemplate } from 'openapi-path-templating'; import { DEFAULT_BASE_URL } from '../../constants.js'; import stylize, { encodeCharacters } from './style-serializer.js'; @@ -15,7 +15,7 @@ export function path({ req, value, parameter }) { if (content) { const effectiveMediaType = Object.keys(content)[0]; - const resolvedPathname = resolve( + const resolvedPathname = resolvePathTemplate( pathname, { [name]: value }, { encoder: (val) => encodeCharacters(serialize(val, effectiveMediaType)) } @@ -23,7 +23,7 @@ export function path({ req, value, parameter }) { req.url = req.url.replace(pathname, resolvedPathname); } else { - const resolvedPathname = resolve( + const resolvedPathname = resolvePathTemplate( pathname, { [name]: value }, { diff --git a/src/execute/swagger2/parameter-builders.js b/src/execute/swagger2/parameter-builders.js index 4da0e2226..fc1196480 100644 --- a/src/execute/swagger2/parameter-builders.js +++ b/src/execute/swagger2/parameter-builders.js @@ -1,4 +1,4 @@ -import { resolve } from 'openapi-path-templating'; +import { resolve as resolvePathTemplate } from 'openapi-path-templating'; import { DEFAULT_BASE_URL } from '../../constants.js'; @@ -58,7 +58,7 @@ function pathBuilder({ req, value, parameter }) { const url = new URL(req.url, DEFAULT_BASE_URL); const pathname = decodeURIComponent(url.pathname); - const resolvedPathname = resolve(pathname, { [parameter.name]: value }); + const resolvedPathname = resolvePathTemplate(pathname, { [parameter.name]: value }); req.url = req.url.replace(pathname, resolvedPathname); } From 21520e5f2eece6b4a02d4d34ba155487fd250baf Mon Sep 17 00:00:00 2001 From: Oliwia Rogala Date: Wed, 8 May 2024 15:00:05 +0200 Subject: [PATCH 3/4] pass pathName to path builder --- src/execute/index.js | 1 + src/execute/oas3/parameter-builders.js | 14 +++++--------- src/execute/swagger2/parameter-builders.js | 11 +++-------- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/execute/index.js b/src/execute/index.js index f92024c0f..9ca9882f7 100755 --- a/src/execute/index.js +++ b/src/execute/index.js @@ -265,6 +265,7 @@ export function buildRequest(options) { value, operation, spec, + pathName, }); } }); diff --git a/src/execute/oas3/parameter-builders.js b/src/execute/oas3/parameter-builders.js index a9c0fa7cb..7562bee54 100644 --- a/src/execute/oas3/parameter-builders.js +++ b/src/execute/oas3/parameter-builders.js @@ -1,30 +1,26 @@ import { resolve as resolvePathTemplate } from 'openapi-path-templating'; -import { DEFAULT_BASE_URL } from '../../constants.js'; import stylize, { encodeCharacters } from './style-serializer.js'; import serialize from './content-serializer.js'; -export function path({ req, value, parameter }) { +export function path({ req, value, parameter, pathName }) { const { name, style, explode, content } = parameter; if (value === undefined) return; - const url = new URL(req.url, DEFAULT_BASE_URL); - const pathname = decodeURIComponent(url.pathname); - if (content) { const effectiveMediaType = Object.keys(content)[0]; const resolvedPathname = resolvePathTemplate( - pathname, + pathName, { [name]: value }, { encoder: (val) => encodeCharacters(serialize(val, effectiveMediaType)) } ); - req.url = req.url.replace(pathname, resolvedPathname); + req.url = req.url.replace(pathName, resolvedPathname); } else { const resolvedPathname = resolvePathTemplate( - pathname, + pathName, { [name]: value }, { encoder: (val) => @@ -38,7 +34,7 @@ export function path({ req, value, parameter }) { } ); - req.url = req.url.replace(pathname, resolvedPathname); + req.url = req.url.replace(pathName, resolvedPathname); } } diff --git a/src/execute/swagger2/parameter-builders.js b/src/execute/swagger2/parameter-builders.js index fc1196480..125c4d7fe 100644 --- a/src/execute/swagger2/parameter-builders.js +++ b/src/execute/swagger2/parameter-builders.js @@ -1,7 +1,5 @@ import { resolve as resolvePathTemplate } from 'openapi-path-templating'; -import { DEFAULT_BASE_URL } from '../../constants.js'; - // These functions will update the request. // They'll be given {req, value, paramter, spec, operation}. @@ -53,14 +51,11 @@ function headerBuilder({ req, parameter, value }) { } // Replace path paramters, with values ( ie: the URL ) -function pathBuilder({ req, value, parameter }) { +function pathBuilder({ req, value, parameter, pathName }) { if (value !== undefined) { - const url = new URL(req.url, DEFAULT_BASE_URL); - const pathname = decodeURIComponent(url.pathname); - - const resolvedPathname = resolvePathTemplate(pathname, { [parameter.name]: value }); + const resolvedPathname = resolvePathTemplate(pathName, { [parameter.name]: value }); - req.url = req.url.replace(pathname, resolvedPathname); + req.url = req.url.replace(pathName, resolvedPathname); } } From 0a64afb5a7e36fa936ea1dad0f906428f2e1a6e4 Mon Sep 17 00:00:00 2001 From: Vladimir Gorej Date: Wed, 8 May 2024 15:21:45 +0200 Subject: [PATCH 4/4] chore: enhancements --- .eslintrc | 3 +-- package-lock.json | 8 ++++---- package.json | 2 +- src/execute/oas3/parameter-builders.js | 12 ++++++------ 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/.eslintrc b/.eslintrc index a3407e899..d16e0791d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -44,8 +44,7 @@ "^@swagger-api/apidom-reference/dereference/strategies/openapi-3-1/selectors/uri$", "^@swagger-api/apidom-reference/resolve/resolvers/file$", "^@swagger-api/apidom-reference/resolve/strategies/openapi-3-1$", - "^@swagger-api/apidom-reference/parse/parsers/binary$", - "openapi-path-templating" + "^@swagger-api/apidom-reference/parse/parsers/binary$" ] } ], diff --git a/package-lock.json b/package-lock.json index 332efbff3..3e95bd36b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,7 @@ "js-yaml": "^4.1.0", "node-abort-controller": "^3.1.1", "node-fetch-commonjs": "^3.3.2", - "openapi-path-templating": "^1.5.0", + "openapi-path-templating": "^1.5.1", "qs": "^6.10.2", "traverse": "=0.6.8" }, @@ -12413,9 +12413,9 @@ } }, "node_modules/openapi-path-templating": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/openapi-path-templating/-/openapi-path-templating-1.5.0.tgz", - "integrity": "sha512-CETC/tMiElOfZSj9jcViJALE2Bbio5fKlepFzhddQvJrGcpadgqw3tAeaM2SpPqVfyLOGUwesoYlfUOFLhjAHQ==", + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/openapi-path-templating/-/openapi-path-templating-1.5.1.tgz", + "integrity": "sha512-kgRHToVP571U1YzUnaZnWaUIygon2itg5g96kwaFIi8bnpsw4oXYOk7k59Ivn+ley1iQnMENe/1HSovpPVZuXA==", "dependencies": { "apg-lite": "^1.0.3" }, diff --git a/package.json b/package.json index 0a306d687..fe2a947b2 100644 --- a/package.json +++ b/package.json @@ -122,7 +122,7 @@ "js-yaml": "^4.1.0", "node-abort-controller": "^3.1.1", "node-fetch-commonjs": "^3.3.2", - "openapi-path-templating": "^1.5.0", + "openapi-path-templating": "^1.5.1", "qs": "^6.10.2", "traverse": "=0.6.8" }, diff --git a/src/execute/oas3/parameter-builders.js b/src/execute/oas3/parameter-builders.js index 7562bee54..0e5e7e8db 100644 --- a/src/execute/oas3/parameter-builders.js +++ b/src/execute/oas3/parameter-builders.js @@ -8,18 +8,18 @@ export function path({ req, value, parameter, pathName }) { if (value === undefined) return; + let resolvedPathname; + if (content) { const effectiveMediaType = Object.keys(content)[0]; - const resolvedPathname = resolvePathTemplate( + resolvedPathname = resolvePathTemplate( pathName, { [name]: value }, { encoder: (val) => encodeCharacters(serialize(val, effectiveMediaType)) } ); - - req.url = req.url.replace(pathName, resolvedPathname); } else { - const resolvedPathname = resolvePathTemplate( + resolvedPathname = resolvePathTemplate( pathName, { [name]: value }, { @@ -33,9 +33,9 @@ export function path({ req, value, parameter, pathName }) { }), } ); - - req.url = req.url.replace(pathName, resolvedPathname); } + + req.url = req.url.replace(pathName, resolvedPathname); } export function query({ req, value, parameter }) {