Skip to content

Commit 9421df9

Browse files
Merge branch 'main' into resolve-paths
2 parents 083ee0f + 3c9b54d commit 9421df9

File tree

5 files changed

+86
-109
lines changed

5 files changed

+86
-109
lines changed

tools/V4-CSDL-to-OpenAPI.xsl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
- @Extends for entity container: include /paths from referenced container
1919
- both "clickable" and freestyle $expand, $select, $orderby - does not work yet, open issue for Swagger UI
2020
- system query options for actions/functions/imports depending on "Collection("
21-
- 200 response for PATCH if $odata-version!='2.0'
2221
- ETag for GET / If-Match for PATCH and DELETE depending on @Core.OptimisticConcurrency
2322
- external targeting for Core.Permission/Read
2423
- example values via Core.Example: Int

tools/package-lock.json

Lines changed: 20 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "odata-openapi-xslt",
3-
"version": "0.9.0",
3+
"version": "0.9.1",
44
"description": "OData to OpenAPI transformer",
55
"homepage": "https://github.com/oasis-tcs/odata-openapi/tree/master/tools#transformjs-for-nodejs",
66
"bugs": "https://github.com/oasis-tcs/odata-csdl-schemas/issues",
@@ -20,9 +20,6 @@
2020
"c8": "^9.1.0",
2121
"mocha": "^10.3.0"
2222
},
23-
"dependencies": {
24-
"minimist": "^1.2.8"
25-
},
2623
"scripts": {
2724
"build": "pwsh ./build.ps1 && npm run validate",
2825
"test": "c8 mocha",

tools/test/transform.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe("CLI", function () {
1818
it("invalid option", async () => {
1919
const result = await transform(["-x"]);
2020
assert.equal(result.code, 1);
21-
assert.equal(result.stderr, "Unknown option: -x\n");
21+
assert.match(result.stderr, /Unknown option '-x'/);
2222
});
2323

2424
it("non-existing file", async () => {

tools/transform.js

100644100755
Lines changed: 64 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
#!/usr/bin/env node
22

33
/* TODO:
4-
- glob for source file patterns
4+
- glob for source file patterns
55
- arguments for
66
- info-title/description/version?
77
- externalDocs-url/description?
88
*/
9-
//console.dir(argv);
109

1110
"use strict";
1211

13-
const minimist = require("minimist");
12+
const { parseArgs } = require("node:util");
1413
const path = require("path");
1514
const fs = require("fs");
1615
const { spawnSync } = require("child_process");
@@ -20,55 +19,37 @@ const toolsPath = path.dirname(require.main.filename) + path.sep;
2019
const classPath = `${toolsPath}xalan/xalan.jar${path.delimiter}${toolsPath}xalan/serializer.jar`;
2120

2221
let unknown = false;
22+
let argv;
23+
24+
try {
25+
argv = parseArgs({
26+
options: {
27+
basePath: { type: "string", default: "/service-root" },
28+
diagram: { type: "boolean", short: "d" },
29+
help: { type: "boolean", short: "h" },
30+
host: { type: "string", default: "localhost" },
31+
"openapi-version": { type: "string", short: "o", default: "3.0.0" },
32+
pretty: { type: "boolean", short: "p" },
33+
references: { type: "boolean", short: "r" },
34+
scheme: { type: "string", default: "https" },
35+
skipBatchPath: { type: "boolean" },
36+
target: { type: "string", short: "t" },
37+
"used-schemas-only": { type: "boolean", short: "u" },
38+
verbose: { type: "boolean" },
39+
},
40+
allowPositionals: true,
41+
});
42+
} catch (e) {
43+
console.error(e.message);
44+
unknown = true;
45+
}
2346

24-
let argv = minimist(process.argv.slice(2), {
25-
string: ["o", "openapi-version", "t", "target", "scheme", "host", "basePath"],
26-
boolean: [
27-
"d",
28-
"diagram",
29-
"h",
30-
"help",
31-
"p",
32-
"pretty",
33-
"r",
34-
"references",
35-
"u",
36-
"used-schemas-only",
37-
"verbose",
38-
],
39-
alias: {
40-
d: "diagram",
41-
h: "help",
42-
o: "openapi-version",
43-
p: "pretty",
44-
r: "references",
45-
t: "target",
46-
u: "used-schemas-only",
47-
v: "odata-version",
48-
},
49-
default: {
50-
basePath: "/service-root",
51-
diagram: false,
52-
host: "localhost",
53-
"odata-version": "4.0",
54-
"openapi-version": "3.0.0",
55-
pretty: false,
56-
references: false,
57-
scheme: "https",
58-
verbose: false,
59-
},
60-
unknown: (arg) => {
61-
if (arg.substring(0, 1) == "-") {
62-
console.error(`Unknown option: ${arg}`);
63-
unknown = true;
64-
return false;
65-
}
66-
},
67-
});
68-
if (argv.o == "2") argv.o = "2.0";
69-
if (argv.o == "3") argv.o = "3.0.0";
47+
if (argv.values["openapi-version"] == "2")
48+
argv.values["openapi-version"] = "2.0";
49+
if (argv.values["openapi-version"] == "3")
50+
argv.values["openapi-version"] = "3.0.0";
7051

71-
if (unknown || argv._.length == 0 || argv.h) {
52+
if (unknown || argv.positionals.length == 0 || argv.values.help) {
7253
console.log(`Usage: odata-openapi <options> <source files>
7354
Options:
7455
--basePath base path (default: /service-root)
@@ -84,8 +65,8 @@ Options:
8465
--verbose output additional progress information`);
8566
process.exit(1);
8667
} else {
87-
for (let i = 0; i < argv._.length; i++) {
88-
transform(argv._[i]);
68+
for (let i = 0; i < argv.positionals.length; i++) {
69+
transform(argv.positionals[i]);
8970
}
9071
}
9172

@@ -95,7 +76,7 @@ function transform(source) {
9576
process.exit(1);
9677
}
9778

98-
if (argv.verbose)
79+
if (argv.values.verbose)
9980
console.log(`Checking OData version used in source file: ${source}`);
10081

10182
const result = xalan("OData-Version.xsl", "-IN", source);
@@ -107,7 +88,7 @@ function transform(source) {
10788

10889
if (result.stderr.length) {
10990
console.error(`Source file not XML: ${source}`);
110-
if (argv.verbose) console.log(result.stderr.toString());
91+
if (argv.values.verbose) console.log(result.stderr.toString());
11192
process.exit(1);
11293
}
11394

@@ -118,10 +99,12 @@ function transform(source) {
11899
}
119100

120101
if (version == "2.0" || version == "3.0") {
121-
if (argv.verbose) console.log(`Source file is OData version: ${version}`);
102+
if (argv.values.verbose)
103+
console.log(`Source file is OData version: ${version}`);
122104
transformV2V3(source, version);
123105
} else {
124-
if (argv.verbose) console.log(`Source file is OData version: ${version}`);
106+
if (argv.values.verbose)
107+
console.log(`Source file is OData version: ${version}`);
125108
transformV4(source, version, false);
126109
}
127110
}
@@ -140,7 +123,7 @@ function xalan(xslt, ...args) {
140123
function transformV2V3(source, version) {
141124
const target = source.substring(0, source.lastIndexOf(".") + 1) + "tmp";
142125

143-
if (argv.verbose)
126+
if (argv.values.verbose)
144127
console.log(`Transforming ${source} to OData V4, target file: ${target}`);
145128

146129
const result = xalan("V2-to-V4-CSDL.xsl", "-IN", source, "-OUT", target);
@@ -153,52 +136,58 @@ function transformV2V3(source, version) {
153136

154137
function transformV4(source, version, deleteSource) {
155138
const target =
156-
argv.t || source.substring(0, source.lastIndexOf(".") + 1) + "openapi.json";
139+
argv.values.target ||
140+
source.substring(0, source.lastIndexOf(".") + 1) + "openapi.json";
157141

158-
if (argv.verbose)
142+
if (argv.values.verbose)
159143
console.log(
160-
`Transforming ${source} to OpenAPI ${argv.o}, target file: ${target}`,
144+
`Transforming ${source} to OpenAPI ${argv.values["openapi-version"]}, target file: ${target}`,
161145
);
162146

163147
const params = ["-IN", source];
164-
if (!argv.u && !argv.pretty) params.push("-OUT", target);
165-
if (argv.basePath) params.push("-PARAM", "basePath", argv.basePath);
166-
if (argv.diagram) params.push("-PARAM", "diagram", argv.diagram);
167-
if (argv.host) params.push("-PARAM", "host", argv.host);
148+
if (!argv.values["used-schemas-only"] && !argv.values.pretty)
149+
params.push("-OUT", target);
150+
if (argv.values.basePath)
151+
params.push("-PARAM", "basePath", argv.values.basePath);
152+
if (argv.values.diagram)
153+
params.push("-PARAM", "diagram", argv.values.diagram);
154+
if (argv.values.host) params.push("-PARAM", "host", argv.values.host);
168155
params.push("-PARAM", "odata-version", version);
169-
params.push("-PARAM", "openapi-version", argv.o);
170-
if (argv.references) params.push("-PARAM", "references", argv.references);
171-
if (argv.scheme) params.push("-PARAM", "scheme", argv.scheme);
156+
params.push("-PARAM", "openapi-version", argv.values["openapi-version"]);
157+
if (argv.values.references)
158+
params.push("-PARAM", "references", argv.values.references);
159+
if (argv.values.scheme) params.push("-PARAM", "scheme", argv.values.scheme);
172160

173161
const result = xalan("V4-CSDL-to-OpenAPI.xsl", ...params);
174162

175163
if (result.stderr.length) console.error(result.stderr.toString());
176164
if (result.status !== 0) process.exit(1);
177165

178-
if (argv.pretty || argv.u) {
166+
if (argv.values.pretty || argv.values["used-schemas-only"]) {
179167
try {
180168
let openapi = JSON.parse(result.stdout);
181169

182-
if (argv.u) {
183-
if (argv.verbose) console.log("Deleting unused schemas");
170+
if (argv.values["used-schemas-only"]) {
171+
if (argv.values.verbose) console.log("Deleting unused schemas");
184172
deleteUnusedSchemas(openapi);
185173
}
186-
if (argv.verbose) console.log(`Writing target file: ${target}`);
174+
if (argv.values.verbose) console.log(`Writing target file: ${target}`);
187175
fs.writeFileSync(
188176
target,
189-
JSON.stringify(openapi, null, argv.pretty ? 4 : 0),
177+
JSON.stringify(openapi, null, argv.values.pretty ? 4 : 0),
190178
);
191179
} catch (e) {
192-
if (argv.verbose) console.log("Ooops, something went wrong: ");
180+
if (argv.values.verbose) console.log("Ooops, something went wrong: ");
193181
console.log(e);
194182
fs.writeFileSync(target, result.stdout);
195183
}
196184
}
197185

198186
if (deleteSource) {
199-
if (argv.verbose) console.log(`Removing intermediate file: ${source}`);
187+
if (argv.values.verbose)
188+
console.log(`Removing intermediate file: ${source}`);
200189
fs.unlinkSync(source);
201190
}
202191

203-
if (argv.verbose) console.log("Done.");
192+
if (argv.values.verbose) console.log("Done.");
204193
}

0 commit comments

Comments
 (0)