Skip to content

Commit

Permalink
refactor(execute): use reference implementation of server URL templat…
Browse files Browse the repository at this point in the history
…ing (#3557)
  • Loading branch information
char0n committed Jun 6, 2024
1 parent 890c203 commit 1c1c2a5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion config/webpack/browser.config.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const browserMin = {
devtool: 'source-map',
performance: {
hints: 'error',
maxEntrypointSize: 460000,
maxEntrypointSize: 470000,
maxAssetSize: 50000000,
},
output: {
Expand Down
21 changes: 12 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@
"cookie": "~0.6.0",
"deepmerge": "~4.3.0",
"fast-json-patch": "^3.0.0-1",
"is-plain-object": "^5.0.0",
"js-yaml": "^4.1.0",
"node-abort-controller": "^3.1.1",
"node-fetch-commonjs": "^3.3.2",
"openapi-path-templating": "^1.5.1",
"openapi-server-url-templating": "^1.0.0",
"qs": "^6.10.2",
"ramda-adjunct": "^5.0.0",
"traverse": "=0.6.8"
Expand Down
36 changes: 17 additions & 19 deletions src/execute/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import cookie from 'cookie';
import { isPlainObject } from 'is-plain-object';
import { escapeRegExp } from 'ramda-adjunct';
import { isPlainObject } from 'ramda-adjunct';
import {
test as testServerURLTemplate,
substitute as substituteServerURLTemplate,
} from 'openapi-server-url-templating';
import { ApiDOMStructuredError } from '@swagger-api/apidom-error';
import { url } from '@swagger-api/apidom-reference/configuration/empty';

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

if (selectedServerUrl.includes('{')) {
// do variable substitution
const varNames = extractServerVariableNames(selectedServerUrl);
varNames.forEach((variable) => {
if (selectedServerObj.variables && selectedServerObj.variables[variable]) {
// variable is defined in server
const variableDefinition = selectedServerObj.variables[variable];
const variableValue = serverVariables[variable] || variableDefinition.default;

const re = new RegExp(`{${escapeRegExp(variable)}}`, 'g');
selectedServerUrl = selectedServerUrl.replace(re, variableValue);
}
if (testServerURLTemplate(selectedServerUrl, { strict: true })) {
const selectedServerVariables = Object.entries({ ...selectedServerObj.variables }).reduce(
(acc, [serverVariableName, serverVariable]) => {
acc[serverVariableName] = serverVariable.default;
return acc;
},
{}
);

selectedServerUrl = substituteServerURLTemplate(selectedServerUrl, {
...selectedServerVariables,
...serverVariables,
});
}

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

function extractServerVariableNames(serverURL) {
const match = serverURL.matchAll(/\{([^{}]+)}|([^{}]+)/g);
return Array.from(match, ([, variable]) => variable).filter(Boolean);
}

// Compose the baseUrl ( scheme + host + basePath )
function swagger2BaseUrl({ spec, scheme, contextUrl = '' }) {
const parsedContextUrl = parseURIReference(contextUrl);
Expand Down
2 changes: 1 addition & 1 deletion src/execute/oas3/build-request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This function runs after the common function,
// `src/execute/index.js#buildRequest`
import { isPlainObject } from 'is-plain-object';
import { isPlainObject } from 'ramda-adjunct';

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

Expand Down

0 comments on commit 1c1c2a5

Please sign in to comment.