oas-toolkit 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/bin.js +5 -0
- package/cli/commands/check-conflicts.js +26 -0
- package/merger.js +86 -3
- package/merger.test.js +106 -0
- package/package.json +3 -2
package/cli/bin.js
CHANGED
|
@@ -9,4 +9,9 @@ yargs(hideBin(process.argv))
|
|
|
9
9
|
"merge the provided OpenAPI files",
|
|
10
10
|
require("./commands/merge")
|
|
11
11
|
)
|
|
12
|
+
.command(
|
|
13
|
+
"check-conflicts <openapi.yaml> <...more.yaml>",
|
|
14
|
+
"check for conflicting components, paths, tags, and security schemes",
|
|
15
|
+
require("./commands/check-conflicts")
|
|
16
|
+
)
|
|
12
17
|
.parse();
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const yaml = require("js-yaml");
|
|
3
|
+
|
|
4
|
+
module.exports = async function ({ argv }) {
|
|
5
|
+
try {
|
|
6
|
+
const oasFiles = argv._.slice(1);
|
|
7
|
+
if (oasFiles.length < 2) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const merger = require("../../merger");
|
|
12
|
+
|
|
13
|
+
const oas = [];
|
|
14
|
+
for (let f of oasFiles) {
|
|
15
|
+
oas.push(yaml.load(fs.readFileSync(f)));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
merger.ensureNoComponentColissions(oas, argv);
|
|
19
|
+
merger.ensureNoPathColissions(oas, argv);
|
|
20
|
+
merger.ensureNoTagColissions(oas, argv);
|
|
21
|
+
merger.ensureNoSecurityColissions(oas, argv);
|
|
22
|
+
} catch (e) {
|
|
23
|
+
console.error(`ERROR: ${e.message}`);
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
};
|
package/merger.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
const mergician = require("mergician");
|
|
2
|
+
const isEqual = require("lodash.isequal");
|
|
3
|
+
const uniqWith = require("lodash.uniqwith");
|
|
2
4
|
|
|
3
5
|
function merge(objects, options) {
|
|
4
|
-
ensureNoComponentColissions(objects, options);
|
|
5
|
-
ensureNoPathColissions(objects, options);
|
|
6
|
-
|
|
7
6
|
// Do the merge
|
|
8
7
|
let combinedSpec = {};
|
|
9
8
|
|
|
@@ -21,6 +20,14 @@ function merge(objects, options) {
|
|
|
21
20
|
combinedSpec = mergeSection(combinedSpec, appendMerge, objects, section);
|
|
22
21
|
}
|
|
23
22
|
|
|
23
|
+
// Values that should be unique
|
|
24
|
+
const uniqueSections = ["security", "tags"];
|
|
25
|
+
for (let section of uniqueSections) {
|
|
26
|
+
if (combinedSpec[section]) {
|
|
27
|
+
combinedSpec[section] = uniqWith(combinedSpec[section], isEqual);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
24
31
|
return combinedSpec;
|
|
25
32
|
}
|
|
26
33
|
|
|
@@ -106,7 +113,83 @@ function ensureNoPathColissions(objects) {
|
|
|
106
113
|
}
|
|
107
114
|
}
|
|
108
115
|
|
|
116
|
+
function ensureListUniqueness(list, key, objects) {
|
|
117
|
+
let all = [];
|
|
118
|
+
for (let object of objects) {
|
|
119
|
+
all = all.concat(object[list] || []);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
for (let c of all) {
|
|
123
|
+
const d = all.filter((t) => {
|
|
124
|
+
return t[key] == c[key] && !isEqual(c, t);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
if (d.length > 0) {
|
|
128
|
+
// Which files does this exist in?
|
|
129
|
+
const sources = [];
|
|
130
|
+
for (let object of objects) {
|
|
131
|
+
if (!object[list]) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const match = object[list].filter((t) => t[key] == c[key]);
|
|
136
|
+
if (match.length) {
|
|
137
|
+
sources.push(object.info.title);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
throw new Error(
|
|
142
|
+
`Conflicting ${list} detected: ${c[key]} (${sources.join(", ")})`
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function ensureNoTagColissions(objects) {
|
|
149
|
+
ensureListUniqueness("tags", "name", objects);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function ensureNoSecurityColissions(objects) {
|
|
153
|
+
let all = [];
|
|
154
|
+
for (let object of objects) {
|
|
155
|
+
all = all.concat(object.security || []);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
all = all.map((s) => Object.entries(s)[0]);
|
|
159
|
+
|
|
160
|
+
for (let c of all) {
|
|
161
|
+
const d = all.filter((t) => {
|
|
162
|
+
return t[0] == c[0] && !isEqual(c, t);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
if (d.length > 0) {
|
|
166
|
+
// Which files does this exist in?
|
|
167
|
+
const sources = [];
|
|
168
|
+
for (let object of objects) {
|
|
169
|
+
if (!object.security) {
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const match = object.security.filter((t) => {
|
|
174
|
+
const k = Object.keys(t)[0];
|
|
175
|
+
return k == c[0];
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
if (match.length) {
|
|
179
|
+
sources.push(object.info.title);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
throw new Error(
|
|
184
|
+
`Conflicting security detected: ${c[0]} (${sources.join(", ")})`
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
109
190
|
module.exports = Object.assign(merge, {
|
|
110
191
|
ensureNoComponentColissions,
|
|
111
192
|
ensureNoPathColissions,
|
|
193
|
+
ensureNoTagColissions,
|
|
194
|
+
ensureNoSecurityColissions,
|
|
112
195
|
});
|
package/merger.test.js
CHANGED
|
@@ -1,12 +1,93 @@
|
|
|
1
1
|
const {
|
|
2
2
|
ensureNoComponentColissions,
|
|
3
3
|
ensureNoPathColissions,
|
|
4
|
+
ensureNoTagColissions,
|
|
5
|
+
ensureNoSecurityColissions,
|
|
4
6
|
} = require("./merger");
|
|
5
7
|
const merger = require("./merger");
|
|
6
8
|
|
|
7
9
|
const FooSchema = { type: "string" };
|
|
8
10
|
const BarSchema = { type: "boolean" };
|
|
9
11
|
|
|
12
|
+
describe("#ensureNoTagColissions", () => {
|
|
13
|
+
it("does not throw when there are no colissions", () => {
|
|
14
|
+
expect(
|
|
15
|
+
ensureNoTagColissions([
|
|
16
|
+
{ info: { title: "One" }, tags: [{ name: "Demo" }] },
|
|
17
|
+
{ info: { title: "Two" }, tags: [{ name: "Demo" }] },
|
|
18
|
+
])
|
|
19
|
+
).toBe(undefined);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("throws when there is a field in one tag but not in another", () => {
|
|
23
|
+
expect(() => {
|
|
24
|
+
ensureNoTagColissions([
|
|
25
|
+
{ info: { title: "One" }, tags: [{ name: "Demo" }] },
|
|
26
|
+
{
|
|
27
|
+
info: { title: "Two" },
|
|
28
|
+
tags: [{ name: "Demo", description: "FOO" }],
|
|
29
|
+
},
|
|
30
|
+
]);
|
|
31
|
+
}).toThrow(new Error("Conflicting tags detected: Demo (One, Two)"));
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("throws when there is a difference in deeply nested fields", () => {
|
|
35
|
+
expect(() => {
|
|
36
|
+
ensureNoTagColissions([
|
|
37
|
+
{
|
|
38
|
+
info: { title: "One" },
|
|
39
|
+
tags: [
|
|
40
|
+
{
|
|
41
|
+
name: "Demo",
|
|
42
|
+
externalDocs: {
|
|
43
|
+
description: "Hello",
|
|
44
|
+
url: "https://example.com",
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
info: { title: "Two" },
|
|
51
|
+
tags: [
|
|
52
|
+
{
|
|
53
|
+
name: "Demo",
|
|
54
|
+
externalDocs: {
|
|
55
|
+
description: "Hello",
|
|
56
|
+
url: "https://another.example.com",
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
},
|
|
61
|
+
]);
|
|
62
|
+
}).toThrow(new Error("Conflicting tags detected: Demo (One, Two)"));
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe("#ensureNoSecurityColissions", () => {
|
|
67
|
+
it("does not throw when there are no colissions", () => {
|
|
68
|
+
expect(
|
|
69
|
+
ensureNoSecurityColissions([
|
|
70
|
+
{ info: { title: "One" }, security: [{ appKey: [] }] },
|
|
71
|
+
{ info: { title: "Two" }, security: [{ appKey: [] }] },
|
|
72
|
+
])
|
|
73
|
+
).toBe(undefined);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("throws when there is a field in one security but not in another", () => {
|
|
77
|
+
expect(() => {
|
|
78
|
+
ensureNoSecurityColissions([
|
|
79
|
+
{ info: { title: "One" }, security: [{ petstore_auth: [] }] },
|
|
80
|
+
{
|
|
81
|
+
info: { title: "Two" },
|
|
82
|
+
security: [{ petstore_auth: ["pets:write"] }],
|
|
83
|
+
},
|
|
84
|
+
]);
|
|
85
|
+
}).toThrow(
|
|
86
|
+
new Error("Conflicting security detected: petstore_auth (One, Two)")
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
10
91
|
describe("#ensureNoComponentColissions", () => {
|
|
11
92
|
it("does not throw when schemas have different prefixes", () => {
|
|
12
93
|
expect(
|
|
@@ -229,3 +310,28 @@ describe("concatenates values for:", () => {
|
|
|
229
310
|
});
|
|
230
311
|
});
|
|
231
312
|
});
|
|
313
|
+
|
|
314
|
+
describe("returns unique items for:", () => {
|
|
315
|
+
it("tags", () => {
|
|
316
|
+
expect(
|
|
317
|
+
merger([
|
|
318
|
+
{ tags: [{ name: "One", description: "Description one" }] },
|
|
319
|
+
{ tags: [{ name: "Two", description: "Description two" }] },
|
|
320
|
+
{ tags: [{ name: "One", description: "Description one" }] },
|
|
321
|
+
])
|
|
322
|
+
).toEqual({
|
|
323
|
+
tags: [
|
|
324
|
+
{ name: "One", description: "Description one" },
|
|
325
|
+
{ name: "Two", description: "Description two" },
|
|
326
|
+
],
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("security", () => {
|
|
331
|
+
expect(
|
|
332
|
+
merger([{ security: [{ appKey: [] }] }, { security: [{ appKey: [] }] }])
|
|
333
|
+
).toEqual({
|
|
334
|
+
security: [{ appKey: [] }],
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oas-toolkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -16,10 +16,11 @@
|
|
|
16
16
|
"jest": "^29.5.0"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@apidevtools/json-schema-ref-parser": "^10.1.0",
|
|
20
19
|
"debug": "^4.3.4",
|
|
21
20
|
"js-yaml": "^4.1.0",
|
|
22
21
|
"jsonpath-plus": "^7.2.0",
|
|
22
|
+
"lodash.isequal": "^4.5.0",
|
|
23
|
+
"lodash.uniqwith": "^4.5.0",
|
|
23
24
|
"mergician": "^1.1.0",
|
|
24
25
|
"yargs": "^17.7.1"
|
|
25
26
|
}
|