|
1 | 1 | 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'; |
4 | 7 | import { ApiDOMStructuredError } from '@swagger-api/apidom-error';
|
5 | 8 | import { url } from '@swagger-api/apidom-reference/configuration/empty';
|
6 | 9 |
|
@@ -347,18 +350,18 @@ function oas3BaseUrl({ spec, pathName, method, server, contextUrl, serverVariabl
|
347 | 350 | selectedServerUrl = selectedServerObj.url;
|
348 | 351 | }
|
349 | 352 |
|
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, |
362 | 365 | });
|
363 | 366 | }
|
364 | 367 |
|
@@ -390,11 +393,6 @@ function buildOas3UrlWithContext(ourUrl = '', contextUrl = '') {
|
390 | 393 | return res[res.length - 1] === '/' ? res.slice(0, -1) : res;
|
391 | 394 | }
|
392 | 395 |
|
393 |
| -function extractServerVariableNames(serverURL) { |
394 |
| - const match = serverURL.matchAll(/\{([^{}]+)}|([^{}]+)/g); |
395 |
| - return Array.from(match, ([, variable]) => variable).filter(Boolean); |
396 |
| -} |
397 |
| - |
398 | 396 | // Compose the baseUrl ( scheme + host + basePath )
|
399 | 397 | function swagger2BaseUrl({ spec, scheme, contextUrl = '' }) {
|
400 | 398 | const parsedContextUrl = parseURIReference(contextUrl);
|
|
0 commit comments