source: node_modules/swagger-client/lib/specmap/helpers.js@ e48199a

main
Last change on this file since e48199a was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 3.2 KB
Line 
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4exports.__esModule = true;
5exports.absolutifyPointer = absolutifyPointer;
6exports.generateAbsoluteRefPatches = generateAbsoluteRefPatches;
7exports.isFreelyNamed = isFreelyNamed;
8var _traverse = _interopRequireDefault(require("traverse"));
9var _empty = require("@swagger-api/apidom-reference/configuration/empty");
10var _constants = require("../constants.js");
11// This will match if the direct parent's key exactly matches an item.
12const 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!
16const 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"]`
22const 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.
32const freelyNamedAncestors = ['schema/example', 'items/example'];
33function 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}
42function 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}
58function 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}
Note: See TracBrowser for help on using the repository browser.