-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathtransform.js
More file actions
52 lines (41 loc) · 1.49 KB
/
transform.js
File metadata and controls
52 lines (41 loc) · 1.49 KB
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
#!/usr/bin/env node
"use strict";
const csdl = require("odata-csdl");
const lib = require("./csdl2openapi");
const fs = require("fs");
const Ajv = require("ajv-draft-04");
const addFormats = require("ajv-formats");
const { openapiV3 } = require("@apidevtools/openapi-schemas");
const ajv = new Ajv({ strict: false });
addFormats(ajv);
const validate = ajv.compile(openapiV3);
const exampleFolder = "./examples/";
const basePath = {
example: "/V4/OData/(S(nsga2k1tyctb0cn0ofcgcn4o))/OData.svc",
Northwind: "/V4/Northwind/Northwind.svc",
TripPin: "/V4/(S(cnbm44wtbc1v5bgrlek5lpcc))/TripPinServiceRW",
"odata-rw-v3": "/V3/(S(1urrjxgkuh4r30yqim0hqrtj))/OData/OData.svc",
"Northwind-V3": "/V3/Northwind/Northwind.svc",
};
fs.readdirSync(exampleFolder)
.filter((fn) => fn.endsWith(".xml"))
.forEach((xmlfile) => {
const example = xmlfile.substring(0, xmlfile.lastIndexOf("."));
console.log(xmlfile);
const xml = fs.readFileSync(exampleFolder + xmlfile, "utf8");
const json = csdl.xml2json(xml);
const messages = [];
const openapi = lib.csdl2openapi(json, {
scheme: "https",
host: basePath[example] ? "services.odata.org" : "localhost",
basePath: basePath[example] || "/service-root",
diagram: true,
messages,
});
fs.writeFileSync(
exampleFolder + example + ".openapi3.json",
JSON.stringify(openapi, null, 4),
);
if (messages.length > 0) console.dir(messages);
if (!validate(openapi)) console.log(validate.errors);
});