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 pathparser.js
155 lines (129 loc) · 4.29 KB
/
parser.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
/* eslint-disable no-underscore-dangle */
const R = require('ramda');
const parseYAML = require('./parser/parseYAML');
const { isAnnotation, isWarningAnnotation, isObject } = require('./predicates');
const { createError } = require('./elements');
const pipeParseResult = require('./pipeParseResult');
const parseOpenAPIObject = require('./parser/oas/parseOpenAPIObject');
const isObjectOrAnnotation = R.either(isObject, isAnnotation);
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);
const isUnsupportedWarning = R.both(
isWarningAnnotation,
annotation => (
annotation.content.includes('contains unsupported key')
|| annotation.content === "'Media Type Object' 'examples' only one example is supported, other examples have been ignored"
)
);
const deduplicateUnsupportedAnnotations = R.curry((namespace, parseResult) => {
// {str: Annotation[]} - indexes all warning annotations by message
const warnings = {}; // {str: Annotation[]}
const filterWarnings = R.ifElse(isUnsupportedWarning,
(warning) => {
const annotations = warnings[warning.content];
if (annotations !== undefined) {
annotations.push(warning);
return false;
}
warnings[warning.content] = [warning];
return true;
},
R.T);
const result = new namespace.elements.ParseResult(R.filter(filterWarnings, parseResult));
// Update warning messages to include count
R.forEach(
(annotations) => {
const warning = annotations[0];
// eslint-disable-next-line no-param-reassign
warning.content = `${warning.content} (${annotations.length} occurances)`;
},
R.filter(annotations => annotations.length > 1, R.values(warnings))
);
return result;
});
function parse(source, context) {
const document = parseYAML(source, context);
const parseDocument = pipeParseResult(context.namespace,
R.unless(isObjectOrAnnotation, createError(context.namespace, 'Source document is not an object')),
R.unless(isAnnotation, parseOpenAPIObject(context)));
const parseResult = R.chain(
R.pipe(
parseDocument,
deduplicateUnsupportedAnnotations(context.namespace),
context.options.generateSourceMap ? filterColumnLine : filterSourceMaps
),
document
);
if (!isAnnotation(parseResult.content[0])) {
const formatVersion = (/3\.\d+\.\d+/).exec(source)[0];
const formatLink = `https://spec.openapis.org/oas/v${formatVersion}`;
const { Link } = context.namespace.elements;
const link = new Link();
link.title = `OpenAPI ${formatVersion}`;
link.relation = 'via';
link.href = formatLink;
parseResult.links.push(link);
}
return parseResult;
}
module.exports = parse;