forked from asyncapi/openapi-schema-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (60 loc) · 1.61 KB
/
index.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
const toJsonSchema = require('@openapi-contrib/openapi-schema-to-json-schema');
module.exports = {
parse,
getMimeTypes
};
async function parse({ message, defaultSchemaFormat }) {
const transformed = toJsonSchema(message.payload, {
cloneSchema: true,
keepNotSupported: [
'discriminator',
'readOnly',
'writeOnly',
'deprecated',
'xml',
'example',
],
});
iterateSchema(transformed);
message['x-parser-original-schema-format'] = message.schemaFormat || defaultSchemaFormat;
message['x-parser-original-payload'] = message.payload;
message.payload = transformed;
delete message.schemaFormat;
}
function iterateSchema(schema) {
if (schema.example !== undefined) {
const examples = schema.examples || [];
examples.push(schema.example);
schema.examples = examples;
delete schema.example;
}
if (schema.$schema !== undefined) {
delete schema.$schema;
}
aliasProps(schema.properties);
aliasProps(schema.patternProperties);
aliasProps(schema.additionalProperties);
aliasProps(schema.items);
aliasProps(schema.additionalItems);
aliasProps(schema.oneOf);
aliasProps(schema.anyOf);
aliasProps(schema.allOf);
aliasProps(schema.not);
}
function aliasProps(obj) {
for (const key in obj) {
const prop = obj[key];
if (prop.xml !== undefined) {
prop['x-xml'] = prop.xml;
delete prop.xml;
}
iterateSchema(obj[key]);
}
}
function getMimeTypes() {
return [
'application/vnd.oai.openapi;version=3.0.0',
'application/vnd.oai.openapi+json;version=3.0.0',
'application/vnd.oai.openapi+yaml;version=3.0.0',
];
}