oas-toolkit 0.10.4 → 0.12.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 CHANGED
@@ -5,8 +5,14 @@ const { hideBin } = require("yargs/helpers");
5
5
 
6
6
  yargs(hideBin(process.argv))
7
7
  .command(
8
- "merge <openapi> <...more.yaml>",
8
+ "merge <openapi...>",
9
9
  "merge the provided OpenAPI files",
10
+ (yargs) => {
11
+ yargs.option("move-path-to-operation", {
12
+ demandOption: false,
13
+ type: "boolean",
14
+ });
15
+ },
10
16
  require("./commands/merge")
11
17
  )
12
18
  .command(
@@ -73,4 +79,16 @@ yargs(hideBin(process.argv))
73
79
  },
74
80
  require("./commands/canonical-server")
75
81
  )
82
+ .command(
83
+ "expand-allof <openapi>",
84
+ "Remove allOf from a schema by merging them in to a single entity",
85
+ (yargs) => {
86
+ yargs.positional("openapi", {
87
+ require: true,
88
+ describe: "the OpenAPI file to rewrite",
89
+ type: "string",
90
+ });
91
+ },
92
+ require("./commands/expand-allof")
93
+ )
76
94
  .parse();
@@ -0,0 +1,14 @@
1
+ const fs = require("fs");
2
+ const yaml = require("js-yaml");
3
+
4
+ module.exports = async function (argv) {
5
+ try {
6
+ const expand = require("../../expand-allof");
7
+ let oas = yaml.load(fs.readFileSync(argv.openapi));
8
+ oas = expand(oas);
9
+ console.log(yaml.dump(oas));
10
+ } catch (e) {
11
+ console.error(`ERROR: ${e.message}`);
12
+ process.exit(1);
13
+ }
14
+ };
@@ -1,20 +1,22 @@
1
1
  const fs = require("fs");
2
2
  const yaml = require("js-yaml");
3
3
 
4
- module.exports = function ({ argv }) {
4
+ module.exports = function (argv, b, c) {
5
5
  try {
6
- const oasFiles = argv._.slice(1);
7
- if (oasFiles.length < 2) {
8
- return;
9
- }
6
+ const oasFiles = argv.openapi;
10
7
 
11
8
  const merger = require("../../merger");
9
+ const canonical = require("../../canonical-server");
12
10
 
13
- const oas = [];
11
+ let oas = [];
14
12
  for (let f of oasFiles) {
15
13
  oas.push(yaml.load(fs.readFileSync(f)));
16
14
  }
17
15
 
16
+ if (argv.movePathToOperation) {
17
+ oas = oas.map((o) => canonical.run(o));
18
+ }
19
+
18
20
  const combined = merger(oas, argv);
19
21
  console.log(yaml.dump(combined));
20
22
  } catch (e) {
@@ -0,0 +1,27 @@
1
+ const traverse = require("traverse");
2
+ const mergician = require("mergician");
3
+ const { dereferenceSync } = require("dereference-json-schema");
4
+
5
+ module.exports = function (oas) {
6
+ // We only want to dereference in oas.components
7
+ const componentsOnly = {
8
+ components: oas.components,
9
+ };
10
+
11
+ oas.components = dereferenceSync(componentsOnly).components;
12
+ oas = traverse(oas).clone();
13
+
14
+ oas = traverse(oas).map(function (x) {
15
+ const path = this.path.join(".");
16
+ if (!path.startsWith("components")) {
17
+ return;
18
+ }
19
+ if (!this.node || !this.node["allOf"]) {
20
+ return;
21
+ }
22
+
23
+ return mergician({}, ...this.node["allOf"]);
24
+ });
25
+
26
+ return oas;
27
+ };
@@ -0,0 +1,187 @@
1
+ const expand = require("./expand-allof");
2
+
3
+ it("merges allOf in to a single entity", () => {
4
+ const result = expand({
5
+ components: {
6
+ schemas: {
7
+ User: {
8
+ allOf: [
9
+ {
10
+ type: "object",
11
+ properties: {
12
+ name: {
13
+ type: "string",
14
+ },
15
+ },
16
+ },
17
+ {
18
+ type: "object",
19
+ properties: {
20
+ age: {
21
+ type: "number",
22
+ },
23
+ },
24
+ },
25
+ ],
26
+ },
27
+ },
28
+ },
29
+ });
30
+ expect(result).toEqual({
31
+ components: {
32
+ schemas: {
33
+ User: {
34
+ type: "object",
35
+ properties: {
36
+ name: {
37
+ type: "string",
38
+ },
39
+ age: {
40
+ type: "number",
41
+ },
42
+ },
43
+ },
44
+ },
45
+ },
46
+ });
47
+ });
48
+
49
+ it("dereferences before merging allOf", () => {
50
+ const result = expand({
51
+ components: {
52
+ schemas: {
53
+ User: {
54
+ allOf: [
55
+ {
56
+ $ref: "#/components/schemas/Name",
57
+ },
58
+ {
59
+ type: "object",
60
+ properties: {
61
+ age: {
62
+ type: "number",
63
+ },
64
+ },
65
+ },
66
+ ],
67
+ },
68
+ Name: {
69
+ type: "object",
70
+ properties: {
71
+ name: {
72
+ type: "string",
73
+ },
74
+ },
75
+ },
76
+ },
77
+ },
78
+ });
79
+
80
+ expect(result).toEqual({
81
+ components: {
82
+ schemas: {
83
+ User: {
84
+ type: "object",
85
+ properties: {
86
+ name: {
87
+ type: "string",
88
+ },
89
+ age: {
90
+ type: "number",
91
+ },
92
+ },
93
+ },
94
+ Name: {
95
+ type: "object",
96
+ properties: {
97
+ name: {
98
+ type: "string",
99
+ },
100
+ },
101
+ },
102
+ },
103
+ },
104
+ });
105
+ });
106
+
107
+ it("allows for required field overrides", () => {
108
+ const result = expand({
109
+ components: {
110
+ schemas: {
111
+ User: {
112
+ type: "object",
113
+ properties: {
114
+ name: {
115
+ type: "string",
116
+ },
117
+ age: {
118
+ type: "number",
119
+ },
120
+ },
121
+ },
122
+ CreateUserRequest: {
123
+ allOf: [
124
+ {
125
+ $ref: "#/components/schemas/User",
126
+ },
127
+ {
128
+ required: ["name"],
129
+ },
130
+ ],
131
+ },
132
+ UserResponse: {
133
+ allOf: [
134
+ {
135
+ $ref: "#/components/schemas/User",
136
+ },
137
+ {
138
+ required: ["name", "age"],
139
+ },
140
+ ],
141
+ },
142
+ },
143
+ },
144
+ });
145
+
146
+ expect(result).toEqual({
147
+ components: {
148
+ schemas: {
149
+ User: {
150
+ type: "object",
151
+ properties: {
152
+ name: {
153
+ type: "string",
154
+ },
155
+ age: {
156
+ type: "number",
157
+ },
158
+ },
159
+ },
160
+ CreateUserRequest: {
161
+ type: "object",
162
+ properties: {
163
+ name: {
164
+ type: "string",
165
+ },
166
+ age: {
167
+ type: "number",
168
+ },
169
+ },
170
+ required: ["name"],
171
+ },
172
+ UserResponse: {
173
+ type: "object",
174
+ properties: {
175
+ name: {
176
+ type: "string",
177
+ },
178
+ age: {
179
+ type: "number",
180
+ },
181
+ },
182
+ required: ["name", "age"],
183
+ },
184
+ },
185
+ },
186
+ });
187
+ });
package/index.js CHANGED
@@ -1,6 +1,9 @@
1
1
  module.exports = {
2
+ canonicalServer: require("./canonical-server"),
2
3
  components: require("./components"),
4
+ expandAllOf: require("./expand-allof"),
3
5
  merger: require("./merger"),
6
+ removeWithAnnotation: require("./remove-with-annotation"),
7
+ rewritePath: require("./rewrite-path"),
4
8
  tags: require("./tags"),
5
- canonicalServer: require("./canonical-server"),
6
9
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oas-toolkit",
3
- "version": "0.10.4",
3
+ "version": "0.12.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -17,6 +17,7 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "debug": "^4.3.4",
20
+ "dereference-json-schema": "^0.2.1",
20
21
  "js-yaml": "^4.1.0",
21
22
  "jsonpath-plus": "^7.2.0",
22
23
  "lodash.difference": "^4.5.0",