-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath-parameters-to-operations.js
63 lines (57 loc) · 1.63 KB
/
path-parameters-to-operations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @copyright Copyright 2019 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
* @module "openapi-transformers/path-parameters-to-operations.js"
*/
import OpenApiTransformerBase from 'openapi-transformer-base';
const PATH_METHODS = [
'delete',
'get',
'head',
'options',
'patch',
'post',
'put',
'trace',
];
/**
* Transformer to move parameters defined on Path Item Objects to the beginning
* of the parameters array of the Operation Objects so that they will be
* generated in the desired order (path-first).
*/
export default class PathParametersToOperationTransformer
extends OpenApiTransformerBase {
// eslint-disable-next-line class-methods-use-this
transformPathItem(pathItem) {
if (!Array.isArray(pathItem.parameters)
|| pathItem.parameters.length === 0) {
return pathItem;
}
const {
parameters,
...newPathItem
} = pathItem;
for (const method of PATH_METHODS) {
if (Object.hasOwn(pathItem, method)) {
const operation = pathItem[method];
const opParams = operation.parameters;
newPathItem[method] = {
...operation,
// TODO: Operation parameters can override path parameters (where
// .name and .in match). Either can be $ref. Handle these.
parameters:
!Array.isArray(opParams) || opParams.length === 0 ? parameters
: [...parameters, ...opParams],
};
}
}
return newPathItem;
}
// Performance optimization: Only need to transform paths.
transformOpenApi(spec) {
return {
...spec,
paths: this.transformPaths(spec.paths),
};
}
}