This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparseOpenAPIObject.js
168 lines (143 loc) · 4.9 KB
/
parseOpenAPIObject.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
165
166
167
168
/* eslint-disable no-underscore-dangle */
const R = require('ramda');
const {
isAnnotation, isExtension, hasKey, getValue,
} = require('../../predicates');
const {
createUnsupportedMemberWarning,
createInvalidMemberWarning,
} = require('../annotations');
const pipeParseResult = require('../../pipeParseResult');
const parseObject = require('../parseObject');
const parseOpenAPI = require('../openapi');
const parseInfoObject = require('./parseInfoObject');
const parsePathsObject = require('./parsePathsObject');
const parseComponentsObject = require('./parseComponentsObject');
const name = 'OpenAPI Object';
const requiredKeys = ['openapi', 'info', 'paths'];
const unsupportedKeys = ['servers', 'security', 'tags', 'externalDocs'];
/**
* Returns whether the given member element is unsupported
* @param member {MemberElement}
* @returns {boolean}
* @see unsupportedKeys
* @private
*/
const isUnsupportedKey = R.anyPass(R.map(hasKey, unsupportedKeys));
const recurseSkippingAnnotations = R.curry((visitor, e) => {
if (isAnnotation(e)) {
return e;
}
if (e) {
if (!e.element) {
return e;
}
visitor(e);
if (e._attributes) {
e.attributes.forEach((value, key, member) => {
recurseSkippingAnnotations(visitor, member);
});
}
if (e._meta) {
e.meta.forEach((value, key, member) => {
recurseSkippingAnnotations(visitor, member);
});
}
if (e.content) {
if (Array.isArray(e.content)) {
e.content.forEach((value) => {
recurseSkippingAnnotations(visitor, value);
});
}
if (e.content.key) {
recurseSkippingAnnotations(visitor, e.content.key);
if (e.content.value) {
recurseSkippingAnnotations(visitor, e.content.value);
}
}
if (e.content.element) {
recurseSkippingAnnotations(visitor, e.content);
}
}
}
return e;
});
function removeSourceMap(e) {
if (e._attributes) {
e.attributes.remove('sourceMap');
}
}
function removeColumnLine(result) {
if (result._attributes) {
const sourceMaps = result.attributes.get('sourceMap');
if (sourceMaps) {
sourceMaps.content.forEach((sourceMap) => {
sourceMap.content.forEach((sourcePoint) => {
sourcePoint.content.forEach((sourceCoordinate) => {
if (sourceCoordinate._attributes) {
sourceCoordinate.attributes.remove('line');
sourceCoordinate.attributes.remove('column');
}
});
});
});
}
}
}
const filterColumnLine = recurseSkippingAnnotations(removeColumnLine);
const filterSourceMaps = recurseSkippingAnnotations(removeSourceMap);
function parseOASObject(context, object) {
const { namespace } = context;
// Takes a parse result, and wraps all of the non annotations inside an array
const asArray = (parseResult) => {
const array = new namespace.elements.Array(R.reject(isAnnotation, parseResult));
return new namespace.elements.ParseResult([array].concat(parseResult.annotations.elements));
};
const parseMember = R.cond([
[hasKey('openapi'), parseOpenAPI(context)],
[hasKey('info'), R.compose(parseInfoObject(context), getValue)],
[hasKey('paths'), R.compose(asArray, parsePathsObject(context), getValue)],
[hasKey('components'), R.compose(parseComponentsObject(context), getValue)],
// FIXME Support exposing extensions into parse result
[isExtension, () => new namespace.elements.ParseResult()],
[isUnsupportedKey, createUnsupportedMemberWarning(namespace, name)],
// Return a warning for additional properties
[R.T, createInvalidMemberWarning(namespace, name)],
]);
const parseOASObject = pipeParseResult(namespace,
parseObject(context, name, parseMember, requiredKeys, ['components']),
(object) => {
const api = object.get('info');
const components = object.get('components');
if (components) {
const schemes = R.or(components.get('securitySchemes'), new namespace.elements.Array());
if (!schemes.isEmpty) {
api.push(new namespace.elements.Category(
schemes.content, { classes: ['authSchemes'] }
));
}
}
const resources = object.get('paths');
if (resources) {
api.content = api.content.concat(resources.content);
}
if (components) {
const schemas = R.or(components.get('schemas'), new namespace.elements.Array())
.content
.filter(member => member.value)
.map(getValue);
if (schemas.length > 0) {
const dataStructures = new namespace.elements.Category(
schemas, { classes: ['dataStructures'] }
);
api.push(dataStructures);
}
}
return api;
});
if (context.options.generateSourceMap) {
return filterColumnLine(parseOASObject(object));
}
return filterSourceMaps(parseOASObject(object));
}
module.exports = R.curry(parseOASObject);