1 | "use strict";
|
---|
2 |
|
---|
3 | var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
---|
4 | exports.__esModule = true;
|
---|
5 | exports.absolutifyPointer = absolutifyPointer;
|
---|
6 | exports.generateAbsoluteRefPatches = generateAbsoluteRefPatches;
|
---|
7 | exports.isFreelyNamed = isFreelyNamed;
|
---|
8 | var _traverse = _interopRequireDefault(require("traverse"));
|
---|
9 | var _empty = require("@swagger-api/apidom-reference/configuration/empty");
|
---|
10 | var _constants = require("../constants.js");
|
---|
11 | // This will match if the direct parent's key exactly matches an item.
|
---|
12 | const freelyNamedKeyParents = ['properties'];
|
---|
13 |
|
---|
14 | // This will match if the grandparent's key exactly matches an item.
|
---|
15 | // NOTE that this is for finding non-free paths!
|
---|
16 | const nonFreelyNamedKeyGrandparents = ['properties'];
|
---|
17 |
|
---|
18 | // This will match if the joined parent path exactly matches an item.
|
---|
19 | //
|
---|
20 | // This is mostly useful for filtering out root-level reusable item names,
|
---|
21 | // for example `["definitions", "$ref"]`
|
---|
22 | const freelyNamedPaths = [
|
---|
23 | // Swagger 2.0
|
---|
24 | 'definitions', 'parameters', 'responses', 'securityDefinitions',
|
---|
25 | // OpenAPI 3.0
|
---|
26 | 'components/schemas', 'components/responses', 'components/parameters', 'components/securitySchemes'];
|
---|
27 |
|
---|
28 | // This will match if any of these items are substrings of the joined
|
---|
29 | // parent path.
|
---|
30 | //
|
---|
31 | // Warning! These are powerful. Beware of edge cases.
|
---|
32 | const freelyNamedAncestors = ['schema/example', 'items/example'];
|
---|
33 | function isFreelyNamed(parentPath) {
|
---|
34 | const parentKey = parentPath[parentPath.length - 1];
|
---|
35 | const grandparentKey = parentPath[parentPath.length - 2];
|
---|
36 | const parentStr = parentPath.join('/');
|
---|
37 | return (
|
---|
38 | // eslint-disable-next-line max-len
|
---|
39 | freelyNamedKeyParents.indexOf(parentKey) > -1 && nonFreelyNamedKeyGrandparents.indexOf(grandparentKey) === -1 || freelyNamedPaths.indexOf(parentStr) > -1 || freelyNamedAncestors.some(el => parentStr.indexOf(el) > -1)
|
---|
40 | );
|
---|
41 | }
|
---|
42 | function generateAbsoluteRefPatches(obj, basePath, {
|
---|
43 | specmap,
|
---|
44 | getBaseUrlForNodePath = path => specmap.getContext([...basePath, ...path]).baseDoc,
|
---|
45 | targetKeys = ['$ref', '$$ref']
|
---|
46 | } = {}) {
|
---|
47 | const patches = [];
|
---|
48 | (0, _traverse.default)(obj).forEach(function callback() {
|
---|
49 | if (targetKeys.includes(this.key) && typeof this.node === 'string') {
|
---|
50 | const nodePath = this.path; // this node's path, relative to `obj`
|
---|
51 | const fullPath = basePath.concat(this.path);
|
---|
52 | const absolutifiedRefValue = absolutifyPointer(this.node, getBaseUrlForNodePath(nodePath));
|
---|
53 | patches.push(specmap.replace(fullPath, absolutifiedRefValue));
|
---|
54 | }
|
---|
55 | });
|
---|
56 | return patches;
|
---|
57 | }
|
---|
58 | function absolutifyPointer(pointer, baseUrl) {
|
---|
59 | const [urlPart, fragmentPart] = pointer.split('#');
|
---|
60 | const safeBaseUrl = baseUrl != null ? baseUrl : '';
|
---|
61 | const safeUrlPart = urlPart != null ? urlPart : '';
|
---|
62 | let newRefUrlPart;
|
---|
63 | if (!_empty.url.isHttpUrl(safeBaseUrl)) {
|
---|
64 | const absoluteBaseUrl = _empty.url.resolve(_constants.DEFAULT_BASE_URL, safeBaseUrl);
|
---|
65 | const absoluteRefUrlPart = _empty.url.resolve(absoluteBaseUrl, safeUrlPart);
|
---|
66 | const rawRefUrlPart = absoluteRefUrlPart.replace(_constants.DEFAULT_BASE_URL, '');
|
---|
67 | newRefUrlPart = safeUrlPart.startsWith('/') ? rawRefUrlPart : rawRefUrlPart.substring(1);
|
---|
68 | } else {
|
---|
69 | newRefUrlPart = _empty.url.resolve(safeBaseUrl, safeUrlPart);
|
---|
70 | }
|
---|
71 | return fragmentPart ? `${newRefUrlPart}#${fragmentPart}` : newRefUrlPart;
|
---|
72 | } |
---|