-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanOpenAPI.js
164 lines (148 loc) · 3.72 KB
/
cleanOpenAPI.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
function noop() {}
/**
* @param {Set<string>} seen
*/
function visitOpenApi(openapi, seen) {
function dereference($ref) {
return $ref.split(/\//).reduce(
(o,k) => o[k],
{'#': openapi}
)
}
function dereferenced(f) {
return function applyOn(object) {
const {$ref} = object;
if (typeof $ref === "string") {
if (! seen.has($ref)) {
seen.add($ref)
applyOn(dereference($ref))
}
} else {
f(object)
}
}
}
function values(o) {
if (Array.isArray(o)) {
return o;
} else {
return Object.values(o || {});
}
}
function isDefined(o) {
return o !== undefined;
}
const visitSchema = dereferenced(({
discriminator,
allOf,
anyOf,
oneOf,
not,
if: _if,
then,
else: _else,
dependentSchemas,
prefixItems,
items,
contains,
properties,
patternProperties,
additionalProperties,
propertyNames,
unevaluatedItems,
unevaluatedProperties,
}) => {
if (discriminator) {
values(discriminator.mapping).map(dereference).forEach(visitSchema)
}
[
allOf,
anyOf,
oneOf,
dependentSchemas,
prefixItems,
properties,
patternProperties,
]
.flatMap(values)
.concat(
not,
_if,
then,
_else,
items,
contains,
additionalProperties,
propertyNames,
unevaluatedItems,
unevaluatedProperties
)
.filter(isDefined)
.forEach(visitSchema)
})
const visitCallback = dereferenced(noop);
const visitLink = dereferenced(noop);
const visitExample = dereferenced(noop);
const visitResponse = dereferenced(({headers, content, links}) => {
values(headers).forEach(visitParameter);
values(content).forEach(visitMediaType);
values(links).forEach(visitLink);
});
const visitRequestBody = dereferenced(({content}) => Object.values(content || {}).forEach(visitMediaType));
const visitParameter = dereferenced(({schema, examples}) => {
if (schema !== undefined) visitSchema(schema);
values(examples).forEach(visitExample);
});
const visitMediaType = visitParameter; // MediaType does the same checks as parameters
const visitOperation = dereferenced(({parameters, requestBody, responses, callbacks}) => {
values(parameters).forEach(visitParameter);
if (requestBody !== undefined) visitRequestBody(requestBody);
values(responses).forEach(visitResponse);
values(callbacks).forEach(visitCallback);
});
const visitPathItem = dereferenced(pathItem => {
values(pathItem.parameters).forEach(visitParameter);
["get", "post", "put", "delete", "options", "head", "patch", "trace"]
.map(op => pathItem[op])
.filter(op => !!op)
.forEach(visitOperation);
});
Object.values(openapi.paths).forEach(visitPathItem)
}
export function cleanOpenAPI(source) {
try {
const seen = new Set();
visitOpenApi(source, seen);
const target = structuredClone(source);
[
"schemas",
"responses",
"parameters",
"examples",
"requestBodies",
"headers",
"links",
"callbacks",
"pathItems"
]
.filter(type => target.components[type] !== undefined)
.forEach(type => {
const refPrefix = `#/components/${type}/`;
const keysToKeep = new Set(seen
.values()
.filter(ref => ref.startsWith(refPrefix))
.map(ref => ref.substring(refPrefix.length))
)
const componentSet = target.components[type];
for (const key of Object.keys(componentSet)) {
if (! keysToKeep.has(key)) {
delete componentSet[key];
}
}
})
return target
} catch (e) {
console.error(e);
return null;
}
}