1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports.default = void 0;
|
---|
5 | var _ramda = require("ramda");
|
---|
6 | var _apidomCore = require("@swagger-api/apidom-core");
|
---|
7 | var _apidomNsOpenapi = require("@swagger-api/apidom-ns-openapi-3-0");
|
---|
8 | /**
|
---|
9 | * Inheritance of Parameter Objects.
|
---|
10 | *
|
---|
11 | * OpenAPI 3.1 specification excerpt that defines the inheritance behavior:
|
---|
12 | *
|
---|
13 | * A list of parameters that are applicable for this operation. If a parameter is already defined at the Path Item,
|
---|
14 | * the new definition will override it but can never remove it. The list MUST NOT include duplicated parameters.
|
---|
15 | * A unique parameter is defined by a combination of a name and location.
|
---|
16 | */
|
---|
17 | /* eslint-disable no-param-reassign */
|
---|
18 | const plugin = () => ({
|
---|
19 | predicates
|
---|
20 | }) => {
|
---|
21 | /**
|
---|
22 | * Establishes identity between two Parameter Objects.
|
---|
23 | *
|
---|
24 | * {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#user-content-operationparameters}
|
---|
25 | */
|
---|
26 | const parameterEquals = (parameter1, parameter2) => {
|
---|
27 | if (!predicates.isParameterElement(parameter1)) return false;
|
---|
28 | if (!predicates.isParameterElement(parameter2)) return false;
|
---|
29 | if (!predicates.isStringElement(parameter1.name)) return false;
|
---|
30 | if (!predicates.isStringElement(parameter1.in)) return false;
|
---|
31 | if (!predicates.isStringElement(parameter2.name)) return false;
|
---|
32 | if (!predicates.isStringElement(parameter2.in)) return false;
|
---|
33 | return (0, _apidomCore.toValue)(parameter1.name) === (0, _apidomCore.toValue)(parameter2.name) && (0, _apidomCore.toValue)(parameter1.in) === (0, _apidomCore.toValue)(parameter2.in);
|
---|
34 | };
|
---|
35 | const pathItemParameters = [];
|
---|
36 | return {
|
---|
37 | visitor: {
|
---|
38 | PathItemElement: {
|
---|
39 | enter(pathItemElement, key, parent, path, ancestors) {
|
---|
40 | // skip visiting this Path Item
|
---|
41 | if (ancestors.some(predicates.isComponentsElement)) {
|
---|
42 | return;
|
---|
43 | }
|
---|
44 | const {
|
---|
45 | parameters
|
---|
46 | } = pathItemElement;
|
---|
47 | if (predicates.isArrayElement(parameters)) {
|
---|
48 | pathItemParameters.push([...parameters.content]);
|
---|
49 | } else {
|
---|
50 | pathItemParameters.push([]);
|
---|
51 | }
|
---|
52 | },
|
---|
53 | leave() {
|
---|
54 | pathItemParameters.pop();
|
---|
55 | }
|
---|
56 | },
|
---|
57 | OperationElement: {
|
---|
58 | leave(operationElement) {
|
---|
59 | const parentPathItemParameters = (0, _ramda.last)(pathItemParameters);
|
---|
60 |
|
---|
61 | // no Path Item Object parameters to inherit from
|
---|
62 | if (!Array.isArray(parentPathItemParameters) || parentPathItemParameters.length === 0) {
|
---|
63 | return;
|
---|
64 | }
|
---|
65 | const operationParameters = (0, _ramda.pathOr)([], ['parameters', 'content'], operationElement);
|
---|
66 |
|
---|
67 | // prefers the first item if two items compare equal based on the predicate
|
---|
68 | const mergedParameters = (0, _ramda.uniqWith)(parameterEquals, [...operationParameters, ...parentPathItemParameters]);
|
---|
69 | operationElement.parameters = new _apidomNsOpenapi.OperationParametersElement(mergedParameters);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | };
|
---|
74 | };
|
---|
75 | /* eslint-enable */
|
---|
76 | var _default = exports.default = plugin; |
---|