Skip to content

Commit 1c1c2a5

Browse files
authored
refactor(execute): use reference implementation of server URL templating (#3557)
1 parent 890c203 commit 1c1c2a5

File tree

5 files changed

+32
-31
lines changed

5 files changed

+32
-31
lines changed

config/webpack/browser.config.babel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const browserMin = {
6363
devtool: 'source-map',
6464
performance: {
6565
hints: 'error',
66-
maxEntrypointSize: 460000,
66+
maxEntrypointSize: 470000,
6767
maxAssetSize: 50000000,
6868
},
6969
output: {

package-lock.json

+12-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@
8181
"cookie": "~0.6.0",
8282
"deepmerge": "~4.3.0",
8383
"fast-json-patch": "^3.0.0-1",
84-
"is-plain-object": "^5.0.0",
8584
"js-yaml": "^4.1.0",
8685
"node-abort-controller": "^3.1.1",
8786
"node-fetch-commonjs": "^3.3.2",
8887
"openapi-path-templating": "^1.5.1",
88+
"openapi-server-url-templating": "^1.0.0",
8989
"qs": "^6.10.2",
9090
"ramda-adjunct": "^5.0.0",
9191
"traverse": "=0.6.8"

src/execute/index.js

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import cookie from 'cookie';
2-
import { isPlainObject } from 'is-plain-object';
3-
import { escapeRegExp } from 'ramda-adjunct';
2+
import { isPlainObject } from 'ramda-adjunct';
3+
import {
4+
test as testServerURLTemplate,
5+
substitute as substituteServerURLTemplate,
6+
} from 'openapi-server-url-templating';
47
import { ApiDOMStructuredError } from '@swagger-api/apidom-error';
58
import { url } from '@swagger-api/apidom-reference/configuration/empty';
69

@@ -347,18 +350,18 @@ function oas3BaseUrl({ spec, pathName, method, server, contextUrl, serverVariabl
347350
selectedServerUrl = selectedServerObj.url;
348351
}
349352

350-
if (selectedServerUrl.includes('{')) {
351-
// do variable substitution
352-
const varNames = extractServerVariableNames(selectedServerUrl);
353-
varNames.forEach((variable) => {
354-
if (selectedServerObj.variables && selectedServerObj.variables[variable]) {
355-
// variable is defined in server
356-
const variableDefinition = selectedServerObj.variables[variable];
357-
const variableValue = serverVariables[variable] || variableDefinition.default;
358-
359-
const re = new RegExp(`{${escapeRegExp(variable)}}`, 'g');
360-
selectedServerUrl = selectedServerUrl.replace(re, variableValue);
361-
}
353+
if (testServerURLTemplate(selectedServerUrl, { strict: true })) {
354+
const selectedServerVariables = Object.entries({ ...selectedServerObj.variables }).reduce(
355+
(acc, [serverVariableName, serverVariable]) => {
356+
acc[serverVariableName] = serverVariable.default;
357+
return acc;
358+
},
359+
{}
360+
);
361+
362+
selectedServerUrl = substituteServerURLTemplate(selectedServerUrl, {
363+
...selectedServerVariables,
364+
...serverVariables,
362365
});
363366
}
364367

@@ -390,11 +393,6 @@ function buildOas3UrlWithContext(ourUrl = '', contextUrl = '') {
390393
return res[res.length - 1] === '/' ? res.slice(0, -1) : res;
391394
}
392395

393-
function extractServerVariableNames(serverURL) {
394-
const match = serverURL.matchAll(/\{([^{}]+)}|([^{}]+)/g);
395-
return Array.from(match, ([, variable]) => variable).filter(Boolean);
396-
}
397-
398396
// Compose the baseUrl ( scheme + host + basePath )
399397
function swagger2BaseUrl({ spec, scheme, contextUrl = '' }) {
400398
const parsedContextUrl = parseURIReference(contextUrl);

src/execute/oas3/build-request.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This function runs after the common function,
22
// `src/execute/index.js#buildRequest`
3-
import { isPlainObject } from 'is-plain-object';
3+
import { isPlainObject } from 'ramda-adjunct';
44

55
import btoa from '../../helpers/btoa.node.js';
66

0 commit comments

Comments
 (0)