Skip to content

Commit

Permalink
refactor: initial attempt to replace legacy url module
Browse files Browse the repository at this point in the history
Refs #2291
  • Loading branch information
char0n committed Dec 14, 2021
1 parent 5ef18a3 commit db78c1f
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 55 deletions.
15 changes: 10 additions & 5 deletions src/execute/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import getIn from 'lodash/get';
import url from 'url';
import cookie from 'cookie';
import { isPlainObject } from 'is-plain-object';

Expand All @@ -9,7 +8,13 @@ import SWAGGER2_PARAMETER_BUILDERS from './swagger2/parameter-builders.js';
import * as OAS3_PARAMETER_BUILDERS from './oas3/parameter-builders.js';
import oas3BuildRequest from './oas3/build-request.js';
import swagger2BuildRequest from './swagger2/build-request.js';
import { getOperationRaw, legacyIdFromPathMethod, isOAS3 } from '../helpers.js';
import {
getOperationRaw,
legacyIdFromPathMethod,
isOAS3,
urlResolve,
urlParse,
} from '../helpers.js';

const arrayOrEmpty = (ar) => (Array.isArray(ar) ? ar : []);

Expand Down Expand Up @@ -328,8 +333,8 @@ function oas3BaseUrl({ spec, pathName, method, server, contextUrl, serverVariabl
function buildOas3UrlWithContext(ourUrl = '', contextUrl = '') {
// relative server url should be resolved against contextUrl
const parsedUrl =
ourUrl && contextUrl ? url.parse(url.resolve(contextUrl, ourUrl)) : url.parse(ourUrl);
const parsedContextUrl = url.parse(contextUrl);
ourUrl && contextUrl ? urlParse(urlResolve(contextUrl, ourUrl)) : urlParse(ourUrl);
const parsedContextUrl = urlParse(contextUrl);

const computedScheme =
stripNonAlpha(parsedUrl.protocol) || stripNonAlpha(parsedContextUrl.protocol) || '';
Expand Down Expand Up @@ -362,7 +367,7 @@ function getVariableTemplateNames(str) {

// Compose the baseUrl ( scheme + host + basePath )
function swagger2BaseUrl({ spec, scheme, contextUrl = '' }) {
const parsedContextUrl = url.parse(contextUrl);
const parsedContextUrl = urlParse(contextUrl);
const firstSchemeInSpec = Array.isArray(spec.schemes) ? spec.schemes[0] : null;

const computedScheme =
Expand Down
16 changes: 16 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,19 @@ export function normalizeSwagger(parsedSpec) {

return parsedSpec;
}

// WHATWG URL API replacement for url.resolve function
export function urlResolve(from, to) {
const resolvedUrl = new URL(to, new URL(from, 'resolve://'));
if (resolvedUrl.protocol === 'resolve:') {
// `from` is a relative URL.
const { pathname, search, hash } = resolvedUrl;
return pathname + search + hash;
}
return resolvedUrl.toString();
}

// WHATWG URL API replacement for url.resolve function
export function urlParse(url) {
return new URL(url);
}
5 changes: 3 additions & 2 deletions src/specmap/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import traverse from 'traverse';
import URL from 'url';

import { urlResolve } from '../helpers.js';

// This will match if the direct parent's key exactly matches an item.
const freelyNamedKeyParents = ['properties'];
Expand Down Expand Up @@ -73,7 +74,7 @@ export function generateAbsoluteRefPatches(

export function absolutifyPointer(pointer, baseUrl) {
const [urlPart, fragmentPart] = pointer.split('#');
const newRefUrlPart = URL.resolve(urlPart || '', baseUrl || '');
const newRefUrlPart = urlResolve(urlPart || '', baseUrl || '');

return fragmentPart ? `${newRefUrlPart}#${fragmentPart}` : newRefUrlPart;
}
3 changes: 2 additions & 1 deletion test/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"rules": {
"global-require": 0, // needs to be eliminated in future
"import/no-dynamic-require": 0,
"import/no-extraneous-dependencies": 0,
"max-classes-per-file": 0,
"no-underscore-dangle": 0
"no-underscore-dangle": 0,
}
}
6 changes: 3 additions & 3 deletions test/specmap/data/cyclic/external/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module.exports = {
name: 'link to a cyclic node',
spec: {
x: {
$ref: 'http://1/spec#/a',
$ref: 'http://0.0.0.1/spec#/a',
},
},
external: {
'http://1/spec': {
'http://0.0.0.1/spec': {
a: {
b: {
$ref: '#/a',
Expand All @@ -17,7 +17,7 @@ module.exports = {
output: {
x: {
b: {
$ref: 'http://1/spec#/a',
$ref: 'http://0.0.0.1/spec#/a',
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions test/specmap/data/cyclic/external/10.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module.exports = {
name: 'link to 2 cyclic nodes',
spec: {
x: {
$ref: 'http://2/spec#/a',
$ref: 'http://0.0.0.2/spec#/a',
},
},
external: {
'http://2/spec': {
'http://0.0.0.2/spec': {
a: {
$ref: '#/b',
},
Expand All @@ -17,7 +17,7 @@ module.exports = {
},
output: {
x: {
$ref: 'http://2/spec#/a',
$ref: 'http://0.0.0.2/spec#/a',
},
},
};
14 changes: 7 additions & 7 deletions test/specmap/data/cyclic/external/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ module.exports = {
name: 'a few hops to an internally cyclic doc',
spec: {
x: {
$ref: 'http://8/spec1#/a',
$ref: 'http://0.0.0.8/spec1#/a',
},
},
external: {
'http://8/spec1': {
'http://0.0.0.8/spec1': {
a: {
$ref: 'http://8/spec2#/b',
$ref: 'http://0.0.0.8/spec2#/b',
},
},
'http://8/spec2': {
'http://0.0.0.8/spec2': {
b: {
$ref: 'http://8/spec3#/c',
$ref: 'http://0.0.0.8/spec3#/c',
},
},
'http://8/spec3': {
'http://0.0.0.8/spec3': {
c: {
d: {
$ref: '#/c',
Expand All @@ -27,7 +27,7 @@ module.exports = {
output: {
x: {
d: {
$ref: 'http://8/spec3#/c',
$ref: 'http://0.0.0.8/spec3#/c',
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions test/specmap/data/cyclic/external/20.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module.exports = {
name: 'link to 3 cyclic nodes',
spec: {
x: {
$ref: 'http://3/spec#/defs/d1',
$ref: 'http://0.0.0.3/spec#/defs/d1',
},
},
external: {
'http://3/spec': {
'http://0.0.0.3/spec': {
defs: {
d1: {
d1k: {
Expand All @@ -31,7 +31,7 @@ module.exports = {
d1k: {
d2k: {
d3k: {
$ref: 'http://3/spec#/defs/d1',
$ref: 'http://0.0.0.3/spec#/defs/d1',
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions test/specmap/data/cyclic/external/21.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module.exports = {
name: 'link to 3 cyclic nodes (in array)',
spec: {
x: {
$ref: 'http://4/spec#/defs/0',
$ref: 'http://0.0.0.4/spec#/defs/0',
},
},
external: {
'http://4/spec': {
'http://0.0.0.4/spec': {
defs: [
{
d1k: {
Expand All @@ -31,7 +31,7 @@ module.exports = {
d1k: {
d2k: {
d3k: {
$ref: 'http://4/spec#/defs/0',
$ref: 'http://0.0.0.4/spec#/defs/0',
},
},
},
Expand Down
12 changes: 6 additions & 6 deletions test/specmap/data/cyclic/external/30.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ module.exports = {
name: 'link to cyclic nodes that use absolute reference',
spec: {
x: {
$ref: 'http://5/spec1#/a',
$ref: 'http://0.0.0.5/spec1#/a',
},
},
external: {
'http://5/spec1': {
'http://0.0.0.5/spec1': {
a: {
b: {
$ref: 'http://5/spec2#/c',
$ref: 'http://0.0.0.5/spec2#/c',
},
},
},
'http://5/spec2': {
'http://0.0.0.5/spec2': {
c: {
d: {
$ref: 'http://5/spec1#/a',
$ref: 'http://0.0.0.5/spec1#/a',
},
},
},
Expand All @@ -25,7 +25,7 @@ module.exports = {
x: {
b: {
d: {
$ref: 'http://5/spec1#/a',
$ref: 'http://0.0.0.5/spec1#/a',
},
},
},
Expand Down
16 changes: 8 additions & 8 deletions test/specmap/data/cyclic/external/31.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ module.exports = {
name: 'link to 3 cyclic nodes that use absolute reference',
spec: {
x: {
$ref: 'http://6/spec1#/a',
$ref: 'http://0.0.0.6/spec1#/a',
},
},
external: {
'http://6/spec1': {
'http://0.0.0.6/spec1': {
a: {
b: {
$ref: 'http://6/spec2#/c',
$ref: 'http://0.0.0.6/spec2#/c',
},
},
},
'http://6/spec2': {
'http://0.0.0.6/spec2': {
c: {
d: {
$ref: 'http://6/spec3#/e',
$ref: 'http://0.0.0.6/spec3#/e',
},
},
},
'http://6/spec3': {
'http://0.0.0.6/spec3': {
e: {
f: {
$ref: 'http://6/spec1#/a',
$ref: 'http://0.0.0.6/spec1#/a',
},
},
},
Expand All @@ -33,7 +33,7 @@ module.exports = {
b: {
d: {
f: {
$ref: 'http://6/spec1#/a',
$ref: 'http://0.0.0.6/spec1#/a',
},
},
},
Expand Down
16 changes: 8 additions & 8 deletions test/specmap/data/cyclic/external/32.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@ module.exports = {
name: 'absolute path to itself',
spec: {
x: {
$ref: 'http://9/spec1#/a',
$ref: 'http://0.0.0.9/spec1#/a',
},
},
external: {
'http://9/spec1': {
'http://0.0.0.9/spec1': {
a: {
$ref: 'http://9/spec2#/b',
$ref: 'http://0.0.0.9/spec2#/b',
},
},
'http://9/spec2': {
'http://0.0.0.9/spec2': {
b: {
$ref: 'http://9/spec3#/c',
$ref: 'http://0.0.0.9/spec3#/c',
},
},
'http://9/spec3': {
'http://0.0.0.9/spec3': {
c: {
d: {
$ref: 'http://9/spec3#/c',
$ref: 'http://0.0.0.9/spec3#/c',
},
},
},
},
output: {
x: {
d: {
$ref: 'http://9/spec3#/c',
$ref: 'http://0.0.0.9/spec3#/c',
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions test/specmap/data/cyclic/external/40.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ module.exports = {
name: 'link to 2 cyclic nodes that use relative reference',
spec: {
x: {
$ref: 'http://7/spec1#/a',
$ref: 'http://0.0.0.7/spec1#/a',
},
},
external: {
'http://7/spec1': {
'http://0.0.0.7/spec1': {
a: {
b: {
$ref: '../spec2#/c',
},
},
},
'http://7/spec2': {
'http://0.0.0.7/spec2': {
c: {
d: {
$ref: '../spec1#/a',
Expand All @@ -25,7 +25,7 @@ module.exports = {
x: {
b: {
d: {
$ref: 'http://7/spec1#/a',
$ref: 'http://0.0.0.7/spec1#/a',
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions test/specmap/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ describe('refs', () => {
try {
throw new refs.JSONRefError('Failed to download ref', {
$ref: '#/one',
basePath: 'localhost/one.json',
basePath: 'http://localhost/one.json',
});
} catch (e) {
expect(e.toString()).toEqual('JSONRefError: Failed to download ref');
expect(e.$ref).toEqual('#/one');
expect(e.basePath).toEqual('localhost/one.json');
expect(e.basePath).toEqual('http://localhost/one.json');
}
});

Expand Down

0 comments on commit db78c1f

Please sign in to comment.