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