@amartus/oas-utils 0.1.0 → 0.2.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/README.md +67 -4
- package/dist/cli.js +19 -1
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/allOfToOneOf.d.ts +23 -0
- package/dist/lib/allOfToOneOf.d.ts.map +1 -0
- package/dist/lib/allOfToOneOf.js +189 -0
- package/dist/lib/allOfToOneOf.js.map +1 -0
- package/dist/lib/cliActions.d.ts +13 -0
- package/dist/lib/cliActions.d.ts.map +1 -1
- package/dist/lib/cliActions.js +32 -0
- package/dist/lib/cliActions.js.map +1 -1
- package/dist/lib/oasUtils.d.ts +27 -0
- package/dist/lib/oasUtils.d.ts.map +1 -1
- package/dist/lib/oasUtils.js +92 -0
- package/dist/lib/oasUtils.js.map +1 -1
- package/dist/redocly/allof-to-oneof-decorator.d.ts +6 -0
- package/dist/redocly/allof-to-oneof-decorator.d.ts.map +1 -0
- package/dist/redocly/allof-to-oneof-decorator.js +15 -0
- package/dist/redocly/allof-to-oneof-decorator.js.map +1 -0
- package/dist/redocly/plugin.d.ts +3 -0
- package/dist/redocly/plugin.d.ts.map +1 -1
- package/dist/redocly/plugin.js +3 -0
- package/dist/redocly/plugin.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# oas-utils
|
|
2
2
|
|
|
3
|
-
Utilities for working with OpenAPI (OAS) documents. Includes tools to remove unused schemas
|
|
3
|
+
Utilities for working with OpenAPI (OAS) documents. Includes tools to remove unused schemas, remove entries from oneOf, optimize allOf composition, and convert allOf + discriminator patterns to oneOf + discriminator. Use them as a CLI or as Redocly decorators.
|
|
4
4
|
|
|
5
5
|
## Definition of "unused schema"
|
|
6
6
|
|
|
@@ -26,6 +26,8 @@ This makes the `oas-utils` CLI available system-wide.
|
|
|
26
26
|
|
|
27
27
|
## CLI usage
|
|
28
28
|
|
|
29
|
+
### remove-unused
|
|
30
|
+
|
|
29
31
|
```
|
|
30
32
|
oas-utils remove-unused <input.yaml> -o output.yaml [--keep Name1 Name2] [--aggressive] [--ignore-parents NameX]
|
|
31
33
|
# Read from stdin and write to stdout
|
|
@@ -37,6 +39,8 @@ Options:
|
|
|
37
39
|
- --aggressive: also prune other unused components referenced from paths (responses, headers, requestBodies, parameters, examples, links, callbacks, securitySchemes). Non-referenced entries in these sections are removed.
|
|
38
40
|
- --ignore-parents: schema names that shouldn't promote children via allOf (can be repeated). Useful to avoid allOf upward inclusion when the parent acts as an abstract/base.
|
|
39
41
|
|
|
42
|
+
### remove-oneof
|
|
43
|
+
|
|
40
44
|
Remove entries from oneOf and update discriminators:
|
|
41
45
|
|
|
42
46
|
```
|
|
@@ -47,11 +51,56 @@ oas-utils remove-oneof <input.yaml> --remove Cat
|
|
|
47
51
|
oas-utils remove-oneof <input.yaml> --parent Pet --remove Cat --guess
|
|
48
52
|
```
|
|
49
53
|
|
|
50
|
-
Options
|
|
54
|
+
Options:
|
|
51
55
|
- --parent: parent schema name containing oneOf; if omitted, removal is global across the document.
|
|
52
56
|
- --remove: schema name(s) to remove; can be repeated.
|
|
53
57
|
- --guess: expand each name to include variants starting with `<name>_`.
|
|
54
58
|
|
|
59
|
+
### optimize-allof
|
|
60
|
+
|
|
61
|
+
Optimize allOf composition by removing redundant references:
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
oas-utils optimize-allof <input.yaml> -o output.yaml
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Options:
|
|
68
|
+
- -o, --output: write result to this file (defaults to stdout).
|
|
69
|
+
|
|
70
|
+
### allof-to-oneof
|
|
71
|
+
|
|
72
|
+
Convert allOf + discriminator patterns to oneOf + discriminator. This is useful for transforming inheritance-based polymorphic schemas into composition-based ones.
|
|
73
|
+
|
|
74
|
+
Specifically, it:
|
|
75
|
+
1. Identifies base schemas with discriminators
|
|
76
|
+
2. Finds concrete schemas that extend the base via allOf
|
|
77
|
+
3. Optionally adds a const property to each concrete schema matching its discriminator value (enabled by default)
|
|
78
|
+
4. Creates a new oneOf wrapper schema containing all concrete types
|
|
79
|
+
5. Replaces references to the base schema with the wrapper schema (in polymorphic contexts)
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
oas-utils allof-to-oneof <input.yaml> -o output.yaml
|
|
83
|
+
# Optionally remove discriminator from base schema
|
|
84
|
+
oas-utils allof-to-oneof <input.yaml> -o output.yaml --remove-discriminator-from-base
|
|
85
|
+
# Optionally skip adding const to specialization schemas
|
|
86
|
+
oas-utils allof-to-oneof <input.yaml> -o output.yaml --no-add-discriminator-const
|
|
87
|
+
# Optionally skip transformation if only one specialization is found
|
|
88
|
+
oas-utils allof-to-oneof <input.yaml> -o output.yaml --ignore-single-specialization
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Options:
|
|
92
|
+
- -o, --output: write result to this file (defaults to stdout).
|
|
93
|
+
- --remove-discriminator-from-base: remove the discriminator from base schemas after conversion.
|
|
94
|
+
- --no-add-discriminator-const: do not add const property with discriminator value to specialization schemas.
|
|
95
|
+
- --ignore-single-specialization: skip oneOf transformation if only one specialization is found (useful for bases with only one concrete implementation).
|
|
96
|
+
|
|
97
|
+
Example transformation (with addDiscriminatorConst enabled, the default):
|
|
98
|
+
- Base schema `Animal` with discriminator `type` and mapping `{Cat: ..., Dog: ...}`
|
|
99
|
+
- Concrete schemas `Cat` and `Dog` with `allOf: [{$ref: Animal}, {...}]`
|
|
100
|
+
- Creates `AnimalPolymorphic` with `oneOf: [Cat, Dog]` and the same discriminator
|
|
101
|
+
- Adds `type: {const: "Cat"}` to Cat's properties and `type: {const: "Dog"}` to Dog's properties
|
|
102
|
+
- Replaces references to `Animal` with `AnimalPolymorphic` in array items and other polymorphic contexts
|
|
103
|
+
|
|
55
104
|
## As Redocly decorators
|
|
56
105
|
|
|
57
106
|
1) Add the plugin to `plugins` in your `redocly.yaml` (path relative to the config):
|
|
@@ -61,7 +110,7 @@ plugins:
|
|
|
61
110
|
- ./node_modules/oas-utils/dist/redocly/plugin.js
|
|
62
111
|
```
|
|
63
112
|
|
|
64
|
-
2) Enable the
|
|
113
|
+
2) Enable the decorators:
|
|
65
114
|
|
|
66
115
|
```
|
|
67
116
|
decorators:
|
|
@@ -75,6 +124,15 @@ decorators:
|
|
|
75
124
|
parent: Pet # optional; if omitted, removal is global
|
|
76
125
|
remove: [Cat, Cat_variant1]
|
|
77
126
|
guess: false
|
|
127
|
+
|
|
128
|
+
# Optimize allOf composition
|
|
129
|
+
oas-utils/optimize-allof: {}
|
|
130
|
+
|
|
131
|
+
# Convert allOf + discriminator to oneOf + discriminator
|
|
132
|
+
oas-utils/allof-to-oneof:
|
|
133
|
+
removeDiscriminatorFromBase: false
|
|
134
|
+
addDiscriminatorConst: true
|
|
135
|
+
ignoreSingleSpecialization: false
|
|
78
136
|
```
|
|
79
137
|
|
|
80
138
|
3) Run bundling with Redocly CLI and the decorators will apply the transformations. With `aggressive: true`, unused non-schema components (responses, headers, requestBodies, etc.) are removed as well.
|
|
@@ -85,8 +143,13 @@ Notes:
|
|
|
85
143
|
## Programmatic usage
|
|
86
144
|
|
|
87
145
|
```
|
|
88
|
-
import { removeUnusedSchemas } from 'oas-utils';
|
|
146
|
+
import { removeUnusedSchemas, allOfToOneOf } from 'oas-utils';
|
|
147
|
+
|
|
148
|
+
// Remove unused schemas
|
|
89
149
|
const pruned = removeUnusedSchemas(doc, { keep: ['CommonError'], aggressive: true });
|
|
150
|
+
|
|
151
|
+
// Convert allOf + discriminator to oneOf + discriminator
|
|
152
|
+
allOfToOneOf(doc, { removeDiscriminatorFromBase: false, addDiscriminatorConst: true });
|
|
90
153
|
```
|
|
91
154
|
|
|
92
155
|
## Notes
|
package/dist/cli.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import fs from "node:fs/promises";
|
|
3
3
|
import { Command } from "commander";
|
|
4
|
-
import { runRemoveUnused, runRemoveOneOf, optimizeAllOf } from "./lib/cliActions.js";
|
|
4
|
+
import { runRemoveUnused, runRemoveOneOf, optimizeAllOf, runAllOfToOneOf } from "./lib/cliActions.js";
|
|
5
5
|
import YAML from "yaml";
|
|
6
6
|
const program = new Command();
|
|
7
7
|
program
|
|
@@ -49,6 +49,24 @@ program
|
|
|
49
49
|
.action(async (input, opts) => {
|
|
50
50
|
await optimizeAllOf(opts, format, () => reader(input));
|
|
51
51
|
});
|
|
52
|
+
program
|
|
53
|
+
.command("allof-to-oneof")
|
|
54
|
+
.showHelpAfterError()
|
|
55
|
+
.description("Convert allOf + discriminator patterns to oneOf + discriminator")
|
|
56
|
+
.argument("[input]", "Path to input OpenAPI file (YAML or JSON). If omitted, reads from stdin")
|
|
57
|
+
.option("-o, --output <file>", "Write result to this file (defaults to stdout)")
|
|
58
|
+
.option("--remove-discriminator-from-base", "Remove discriminator from base schemas after conversion", false)
|
|
59
|
+
.option("--no-add-discriminator-const", "Do not add const property with discriminator value to specialization schemas", true)
|
|
60
|
+
.option("--ignore-single-specialization", "Skip oneOf transformation if only one specialization is found", false)
|
|
61
|
+
.action(async (input, opts) => {
|
|
62
|
+
try {
|
|
63
|
+
await runAllOfToOneOf(opts, format, () => reader(input));
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
console.error(`Error: ${err?.message || String(err)}`);
|
|
67
|
+
process.exitCode = 1;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
52
70
|
if (process.argv.length <= 2) {
|
|
53
71
|
program.help();
|
|
54
72
|
}
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,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AyB;IAC7C,IAAI,KAAK,EAAE,CAAC;QACV,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC;IACxB,CAAC;IACD,OAAO,SAAS,EAAE,CAAC;AACrB,CAAC;AAGD,SAAS,MAAM,CAAC,GAAQ,EAAE,MAAe;IACvC,MAAM,MAAM,GAAG,MAAM,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC;IACzC,OAAO,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;AAC5E,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC;AAClD,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mCAAmC,CAAC;AAClD,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface AllOfToOneOfOptions {
|
|
2
|
+
/** If true, remove discriminator from base schema and let oneOf wrapper handle it */
|
|
3
|
+
removeDiscriminatorFromBase?: boolean;
|
|
4
|
+
/** If true, add const property with discriminator value to specialization schemas (default: true) */
|
|
5
|
+
addDiscriminatorConst?: boolean;
|
|
6
|
+
/** If true, skip oneOf transformation if only one specialization is found (default: false) */
|
|
7
|
+
ignoreSingleSpecialization?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Convert allOf + discriminator patterns to oneOf + discriminator.
|
|
11
|
+
*
|
|
12
|
+
* This operation:
|
|
13
|
+
* 1. Finds base schemas with discriminators
|
|
14
|
+
* 2. Identifies concrete schemas that extend the base via allOf
|
|
15
|
+
* 3. For each concrete schema, adds a const property matching the discriminator value
|
|
16
|
+
* 4. Creates a new oneOf wrapper schema containing all concrete schemas
|
|
17
|
+
* 5. Replaces all references to the base schema with the wrapper schema (where polymorphism is used)
|
|
18
|
+
*
|
|
19
|
+
* @param doc - OpenAPI document to transform
|
|
20
|
+
* @param opts - Optional configuration
|
|
21
|
+
*/
|
|
22
|
+
export declare function allOfToOneOf(doc: any, opts?: AllOfToOneOfOptions): any;
|
|
23
|
+
//# sourceMappingURL=allOfToOneOf.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allOfToOneOf.d.ts","sourceRoot":"","sources":["../../src/lib/allOfToOneOf.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,mBAAmB;IAClC,qFAAqF;IACrF,2BAA2B,CAAC,EAAE,OAAO,CAAC;IACtC,qGAAqG;IACrG,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,8FAA8F;IAC9F,0BAA0B,CAAC,EAAE,OAAO,CAAC;CACtC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,GAAE,mBAAwB,GAAG,GAAG,CAmF1E"}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { refToName, buildInheritanceGraph } from "./oasUtils.js";
|
|
2
|
+
/**
|
|
3
|
+
* Convert allOf + discriminator patterns to oneOf + discriminator.
|
|
4
|
+
*
|
|
5
|
+
* This operation:
|
|
6
|
+
* 1. Finds base schemas with discriminators
|
|
7
|
+
* 2. Identifies concrete schemas that extend the base via allOf
|
|
8
|
+
* 3. For each concrete schema, adds a const property matching the discriminator value
|
|
9
|
+
* 4. Creates a new oneOf wrapper schema containing all concrete schemas
|
|
10
|
+
* 5. Replaces all references to the base schema with the wrapper schema (where polymorphism is used)
|
|
11
|
+
*
|
|
12
|
+
* @param doc - OpenAPI document to transform
|
|
13
|
+
* @param opts - Optional configuration
|
|
14
|
+
*/
|
|
15
|
+
export function allOfToOneOf(doc, opts = {}) {
|
|
16
|
+
if (!doc || typeof doc !== "object")
|
|
17
|
+
return doc;
|
|
18
|
+
const schemas = doc.components?.schemas;
|
|
19
|
+
if (!schemas || typeof schemas !== "object")
|
|
20
|
+
return doc;
|
|
21
|
+
// Step 1: Find all base schemas with discriminators
|
|
22
|
+
const baseSchemasWithDiscriminator = new Map();
|
|
23
|
+
for (const [name, schema] of Object.entries(schemas)) {
|
|
24
|
+
if (schema && typeof schema === "object" && schema.discriminator) {
|
|
25
|
+
const disc = schema.discriminator;
|
|
26
|
+
if (disc.propertyName && disc.mapping && typeof disc.mapping === "object") {
|
|
27
|
+
baseSchemasWithDiscriminator.set(name, {
|
|
28
|
+
propertyName: disc.propertyName,
|
|
29
|
+
mapping: { ...disc.mapping }
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (baseSchemasWithDiscriminator.size === 0) {
|
|
35
|
+
return doc; // Nothing to convert
|
|
36
|
+
}
|
|
37
|
+
// Step 2: For each base schema with discriminator, find concrete types extending it
|
|
38
|
+
const inheritanceGraph = buildInheritanceGraph(schemas);
|
|
39
|
+
const polymorphicWrappers = new Map();
|
|
40
|
+
for (const [baseName, discInfo] of baseSchemasWithDiscriminator.entries()) {
|
|
41
|
+
const concreteSchemas = Array.from(inheritanceGraph.get(baseName) || []);
|
|
42
|
+
if (opts.ignoreSingleSpecialization && concreteSchemas.length === 1) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (concreteSchemas.length > 0) {
|
|
46
|
+
const wrapperName = `${baseName}Polymorphic`;
|
|
47
|
+
polymorphicWrappers.set(baseName, { name: wrapperName, concreteSchemas });
|
|
48
|
+
// Step 3: For each concrete schema, optionally add const property for discriminator
|
|
49
|
+
if (opts.addDiscriminatorConst !== false) {
|
|
50
|
+
const cS = Object.fromEntries(concreteSchemas.map(name => [name, schemas[name]]));
|
|
51
|
+
addDiscriminatorConstToConcreteSchemas(cS, discInfo);
|
|
52
|
+
}
|
|
53
|
+
// Step 4: Create wrapper schema with oneOf
|
|
54
|
+
const wrapperSchema = {
|
|
55
|
+
oneOf: concreteSchemas.map(name => ({ $ref: `#/components/schemas/${name}` })),
|
|
56
|
+
discriminator: {
|
|
57
|
+
propertyName: discInfo.propertyName,
|
|
58
|
+
mapping: discInfo.mapping
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
// If available, preserve description from base
|
|
62
|
+
const baseSchema = schemas[baseName];
|
|
63
|
+
if (baseSchema && baseSchema.description) {
|
|
64
|
+
wrapperSchema.description = `OneOf polymorphic ${baseName}. Use the "${discInfo.propertyName}" property to identify the concrete schema.`;
|
|
65
|
+
}
|
|
66
|
+
schemas[wrapperName] = wrapperSchema;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// Step 5: Replace references to base schemas with wrapper schemas where polymorphism is used
|
|
70
|
+
// We need to find where base schemas are referenced and check if they're used polymorphically
|
|
71
|
+
for (const [baseName, wrapperInfo] of polymorphicWrappers.entries()) {
|
|
72
|
+
replacePolymorhicReferences(schemas, baseName, wrapperInfo.name);
|
|
73
|
+
}
|
|
74
|
+
// Step 6: Optionally remove discriminator from base schemas
|
|
75
|
+
if (opts.removeDiscriminatorFromBase) {
|
|
76
|
+
for (const baseName of baseSchemasWithDiscriminator.keys()) {
|
|
77
|
+
if (schemas[baseName] && polymorphicWrappers.has(baseName)) {
|
|
78
|
+
delete schemas[baseName].discriminator;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return doc;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Replace references to a polymorphic base schema with references to its wrapper,
|
|
86
|
+
* but only in contexts where polymorphism would be used (e.g., in array items).
|
|
87
|
+
* Excludes references within concrete schemas' direct allOf references.
|
|
88
|
+
*/
|
|
89
|
+
function replacePolymorhicReferences(schemas, baseName, wrapperName) {
|
|
90
|
+
const baseRef = `#/components/schemas/${baseName}`;
|
|
91
|
+
const wrapperRef = `#/components/schemas/${wrapperName}`;
|
|
92
|
+
for (const schema of Object.values(schemas)) {
|
|
93
|
+
if (!schema || typeof schema !== "object")
|
|
94
|
+
continue;
|
|
95
|
+
replaceInSchema(schema, baseRef, wrapperRef, true);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Recursively replace $ref in a schema, particularly in polymorphic contexts like array items.
|
|
100
|
+
* When skipAllOfReplacement=true, skip replacing $ref directly within allOf arrays.
|
|
101
|
+
*/
|
|
102
|
+
function replaceInSchema(node, oldRef, newRef, skipDirectAllOfRefs = false) {
|
|
103
|
+
if (!node || typeof node !== "object")
|
|
104
|
+
return;
|
|
105
|
+
if (Array.isArray(node)) {
|
|
106
|
+
for (const item of node) {
|
|
107
|
+
if (item && typeof item === "object") {
|
|
108
|
+
if (item.$ref === oldRef) {
|
|
109
|
+
// Don't replace direct $ref in allOf when flag is set
|
|
110
|
+
if (skipDirectAllOfRefs) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
item.$ref = newRef;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
replaceInSchema(item, oldRef, newRef, skipDirectAllOfRefs);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
// For object properties, replace all $ref occurrences
|
|
123
|
+
for (const [key, value] of Object.entries(node)) {
|
|
124
|
+
if (value && typeof value === "object") {
|
|
125
|
+
// Special handling for allOf: skip direct $ref replacements
|
|
126
|
+
if (skipDirectAllOfRefs && key === "allOf" && Array.isArray(value)) {
|
|
127
|
+
for (const item of value) {
|
|
128
|
+
if (item && typeof item === "object" && item.$ref !== oldRef) {
|
|
129
|
+
replaceInSchema(item, oldRef, newRef, skipDirectAllOfRefs);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (value.$ref === oldRef) {
|
|
135
|
+
value.$ref = newRef;
|
|
136
|
+
}
|
|
137
|
+
replaceInSchema(value, oldRef, newRef, skipDirectAllOfRefs);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Add const constraint to each concrete schema matching the discriminator value.
|
|
143
|
+
* Avoids duplicates by checking if the const constraint already exists.
|
|
144
|
+
*
|
|
145
|
+
* @param schemas - Concrete schemas extending a base with discriminator (name -> schema)
|
|
146
|
+
* @param discInfo - Discriminator info (propertyName, mapping)
|
|
147
|
+
*/
|
|
148
|
+
function addDiscriminatorConstToConcreteSchemas(schemas, discInfo) {
|
|
149
|
+
// Iterate through concrete schemas by name
|
|
150
|
+
for (const [concreteName, concreteSchema] of Object.entries(schemas)) {
|
|
151
|
+
if (!concreteSchema)
|
|
152
|
+
continue;
|
|
153
|
+
// Find the discriminator value for this concrete schema
|
|
154
|
+
let discriminatorValue;
|
|
155
|
+
for (const [value, ref] of Object.entries(discInfo.mapping)) {
|
|
156
|
+
if (typeof ref === "string" && refToName(ref) === concreteName) {
|
|
157
|
+
discriminatorValue = value;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (discriminatorValue) {
|
|
162
|
+
// Ensure allOf exists
|
|
163
|
+
if (!Array.isArray(concreteSchema.allOf)) {
|
|
164
|
+
concreteSchema.allOf = [];
|
|
165
|
+
}
|
|
166
|
+
// Check if const constraint already exists for this schema
|
|
167
|
+
const constExists = concreteSchema.allOf.some((item) => item &&
|
|
168
|
+
typeof item === "object" &&
|
|
169
|
+
item.type === "object" &&
|
|
170
|
+
item.properties &&
|
|
171
|
+
item.properties[discInfo.propertyName] &&
|
|
172
|
+
item.properties[discInfo.propertyName].const === discriminatorValue);
|
|
173
|
+
// Only add const if it doesn't already exist
|
|
174
|
+
if (!constExists) {
|
|
175
|
+
// Add const as a separate allOf constraint (not merged with existing inline objects)
|
|
176
|
+
const constConstraint = {
|
|
177
|
+
type: "object",
|
|
178
|
+
properties: {
|
|
179
|
+
[discInfo.propertyName]: {
|
|
180
|
+
const: discriminatorValue
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
concreteSchema.allOf.push(constConstraint);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=allOfToOneOf.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allOfToOneOf.js","sourceRoot":"","sources":["../../src/lib/allOfToOneOf.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAYjE;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,YAAY,CAAC,GAAQ,EAAE,OAA4B,EAAE;IACnE,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,GAAG,CAAC;IAChD,MAAM,OAAO,GAAoC,GAAG,CAAC,UAAU,EAAE,OAAO,CAAC;IACzE,IAAI,CAAC,OAAO,IAAI,OAAO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|
@@ -19,4 +19,17 @@ export declare function runRemoveOneOf(opts: {
|
|
|
19
19
|
export declare function optimizeAllOf(opts: {
|
|
20
20
|
output?: string;
|
|
21
21
|
}, format: (doc: any, target?: string) => string, reader: () => Promise<string>): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Converts allOf + discriminator patterns to oneOf + discriminator.
|
|
24
|
+
*
|
|
25
|
+
* @param opts - Options including output path and transformation options
|
|
26
|
+
* @param format - Function to format output
|
|
27
|
+
* @param reader - Function to read input
|
|
28
|
+
*/
|
|
29
|
+
export declare function runAllOfToOneOf(opts: {
|
|
30
|
+
output?: string;
|
|
31
|
+
removeDiscriminatorFromBase?: boolean;
|
|
32
|
+
addDiscriminatorConst?: boolean;
|
|
33
|
+
ignoreSingleSpecialization?: boolean;
|
|
34
|
+
}, format: (doc: any, target?: string) => string, reader: () => Promise<string>): Promise<void>;
|
|
22
35
|
//# sourceMappingURL=cliActions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cliActions.d.ts","sourceRoot":"","sources":["../../src/lib/cliActions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cliActions.d.ts","sourceRoot":"","sources":["../../src/lib/cliActions.ts"],"names":[],"mappings":"AAmDA,wBAAsB,eAAe,CACnC,IAAI,EAAE;IACJ,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,EACD,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,MAAM,EAC7C,MAAM,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,iBAoB9B;AAQD,wBAAsB,cAAc,CAClC,IAAI,EAAE;IAAE,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAC;IAAC,KAAK,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,EACvF,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,MAAM,EAC7C,MAAM,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,iBAyC9B;AAED;;;;;GAKG;AACH,wBAAsB,aAAa,CAAG,IAAI,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,EAC7D,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,MAAM,EAC7C,MAAM,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,iBAO9B;AAED;;;;;;GAMG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,2BAA2B,CAAC,EAAE,OAAO,CAAC;IAAC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAAC,0BAA0B,CAAC,EAAE,OAAO,CAAA;CAAE,EACvI,MAAM,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,MAAM,EAC7C,MAAM,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,iBA6B9B"}
|
package/dist/lib/cliActions.js
CHANGED
|
@@ -4,6 +4,7 @@ import YAML from "yaml";
|
|
|
4
4
|
import { optimizeAllOfComposition } from "./optimizeAllOfComposition.js";
|
|
5
5
|
import { removeUnusedSchemas } from "./removeUnusedSchemas.js";
|
|
6
6
|
import { removeFromOneOfByName, removeFromOneOfGlobally, } from "./removeFromOneOfByName.js";
|
|
7
|
+
import { allOfToOneOf } from "./allOfToOneOf.js";
|
|
7
8
|
function parseYamlOrJson(data) {
|
|
8
9
|
// Accept pre-parsed objects (useful in tests)
|
|
9
10
|
if (data && typeof data === "object")
|
|
@@ -117,4 +118,35 @@ export async function optimizeAllOf(opts, format, reader) {
|
|
|
117
118
|
optimizeAllOfComposition(doc);
|
|
118
119
|
await writeOutput(doc, opts.output, format);
|
|
119
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Converts allOf + discriminator patterns to oneOf + discriminator.
|
|
123
|
+
*
|
|
124
|
+
* @param opts - Options including output path and transformation options
|
|
125
|
+
* @param format - Function to format output
|
|
126
|
+
* @param reader - Function to read input
|
|
127
|
+
*/
|
|
128
|
+
export async function runAllOfToOneOf(opts, format, reader) {
|
|
129
|
+
const data = await reader();
|
|
130
|
+
const doc = parseYamlOrJson(data);
|
|
131
|
+
if (!doc.components || !doc.components.schemas) {
|
|
132
|
+
console.error("[ERROR] The input document does not contain valid components.schemas.");
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const beforeSchemas = Object.keys(doc.components.schemas);
|
|
136
|
+
const topts = {
|
|
137
|
+
removeDiscriminatorFromBase: Boolean(opts.removeDiscriminatorFromBase),
|
|
138
|
+
addDiscriminatorConst: opts.addDiscriminatorConst !== false,
|
|
139
|
+
ignoreSingleSpecialization: Boolean(opts.ignoreSingleSpecialization),
|
|
140
|
+
};
|
|
141
|
+
allOfToOneOf(doc, topts);
|
|
142
|
+
const afterSchemas = Object.keys(doc.components.schemas);
|
|
143
|
+
const newSchemas = afterSchemas.filter(s => !beforeSchemas.includes(s));
|
|
144
|
+
if (newSchemas.length > 0) {
|
|
145
|
+
console.error(`[ALLOF-TO-ONEOF] Created wrapper schema(s): ${newSchemas.join(", ")}`);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
console.error("[INFO] No allOf + discriminator patterns found to convert.");
|
|
149
|
+
}
|
|
150
|
+
await writeOutput(doc, opts.output, format);
|
|
151
|
+
}
|
|
120
152
|
//# sourceMappingURL=cliActions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cliActions.js","sourceRoot":"","sources":["../../src/lib/cliActions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC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ElC,IAAI,CAAC,GAAG,CAAC,UAAU,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;QAC/C,OAAO,CAAC,KAAK,CAAC,uEAAuE,CAAC,CAAC;QACvF,OAAO;IACT,CAAC;IAED,MAAM,aAAa,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAC1D,MAAM,KAAK,GAAwB;QACjC,2BAA2B,EAAE,OAAO,CAAC,IAAI,CAAC,2BAA2B,CAAC;QACtE,qBAAqB,EAAE,IAAI,CAAC,qBAAqB,KAAK,KAAK;QAC3D,0BAA0B,EAAE,OAAO,CAAC,IAAI,CAAC,0BAA0B,CAAC;KACrE,CAAC;IAEF,YAAY,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAEzB,MAAM,YAAY,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IACzD,MAAM,UAAU,GAAG,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;IAExE,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC1B,OAAO,CAAC,KAAK,CAAC,+CAA+C,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACxF,CAAC;SAAM,CAAC;QACN,OAAO,CAAC,KAAK,CAAC,4DAA4D,CAAC,CAAC;IAC9E,CAAC;IAED,MAAM,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAC9C,CAAC"}
|
package/dist/lib/oasUtils.d.ts
CHANGED
|
@@ -2,4 +2,31 @@
|
|
|
2
2
|
* Helper to extract a components/schemas ref name from a $ref string.
|
|
3
3
|
*/
|
|
4
4
|
export declare function refToName(ref: string): string | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* Builds an inheritance/composition graph from OpenAPI schemas.
|
|
7
|
+
*
|
|
8
|
+
* Returns a Map where:
|
|
9
|
+
* - Key: schema name (parent type)
|
|
10
|
+
* - Value: Set of schema names that extend/compose this parent via allOf
|
|
11
|
+
*
|
|
12
|
+
* @param schemas - The components.schemas object from an OpenAPI document
|
|
13
|
+
* @returns Map<string, Set<string>> - Parent schema to child schemas mapping
|
|
14
|
+
*/
|
|
15
|
+
export declare function buildInheritanceGraph(schemas: Record<string, any>): Map<string, Set<string>>;
|
|
16
|
+
/**
|
|
17
|
+
* Gets all descendants (direct and transitive) of a schema in the inheritance graph.
|
|
18
|
+
*
|
|
19
|
+
* @param parentName - The schema name to find descendants for
|
|
20
|
+
* @param graph - The inheritance graph from buildInheritanceGraph()
|
|
21
|
+
* @returns Set<string> - All descendant schema names
|
|
22
|
+
*/
|
|
23
|
+
export declare function getDescendants(parentName: string, graph: Map<string, Set<string>>): Set<string>;
|
|
24
|
+
/**
|
|
25
|
+
* Gets all ancestors (direct and transitive) of a schema in the inheritance hierarchy.
|
|
26
|
+
*
|
|
27
|
+
* @param childName - The schema name to find ancestors for
|
|
28
|
+
* @param schemas - The components.schemas object from an OpenAPI document
|
|
29
|
+
* @returns Set<string> - All ancestor schema names
|
|
30
|
+
*/
|
|
31
|
+
export declare function getAncestors(childName: string, schemas: Record<string, any>): Set<string>;
|
|
5
32
|
//# sourceMappingURL=oasUtils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oasUtils.d.ts","sourceRoot":"","sources":["../../src/lib/oasUtils.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAGzD"}
|
|
1
|
+
{"version":3,"file":"oasUtils.d.ts","sourceRoot":"","sources":["../../src/lib/oasUtils.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAGzD;AAED;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CA6B5F;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,CAmB/F;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,CAyBzF"}
|
package/dist/lib/oasUtils.js
CHANGED
|
@@ -6,4 +6,96 @@ export function refToName(ref) {
|
|
|
6
6
|
const m = ref.match(/^#\/(?:components\/)?schemas\/([^#/]+)$/);
|
|
7
7
|
return m ? decodeURIComponent(m[1]) : undefined;
|
|
8
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* Builds an inheritance/composition graph from OpenAPI schemas.
|
|
11
|
+
*
|
|
12
|
+
* Returns a Map where:
|
|
13
|
+
* - Key: schema name (parent type)
|
|
14
|
+
* - Value: Set of schema names that extend/compose this parent via allOf
|
|
15
|
+
*
|
|
16
|
+
* @param schemas - The components.schemas object from an OpenAPI document
|
|
17
|
+
* @returns Map<string, Set<string>> - Parent schema to child schemas mapping
|
|
18
|
+
*/
|
|
19
|
+
export function buildInheritanceGraph(schemas) {
|
|
20
|
+
const graph = new Map();
|
|
21
|
+
if (!schemas || typeof schemas !== "object") {
|
|
22
|
+
return graph;
|
|
23
|
+
}
|
|
24
|
+
// Iterate through all schemas
|
|
25
|
+
for (const [childName, schema] of Object.entries(schemas)) {
|
|
26
|
+
if (!schema || typeof schema !== "object")
|
|
27
|
+
continue;
|
|
28
|
+
// Look for allOf references in this schema
|
|
29
|
+
if (Array.isArray(schema.allOf)) {
|
|
30
|
+
for (const item of schema.allOf) {
|
|
31
|
+
if (item && typeof item === "object" && typeof item.$ref === "string") {
|
|
32
|
+
const parentName = refToName(item.$ref);
|
|
33
|
+
if (parentName) {
|
|
34
|
+
// Add this child to the parent's set
|
|
35
|
+
if (!graph.has(parentName)) {
|
|
36
|
+
graph.set(parentName, new Set());
|
|
37
|
+
}
|
|
38
|
+
graph.get(parentName).add(childName);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return graph;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Gets all descendants (direct and transitive) of a schema in the inheritance graph.
|
|
48
|
+
*
|
|
49
|
+
* @param parentName - The schema name to find descendants for
|
|
50
|
+
* @param graph - The inheritance graph from buildInheritanceGraph()
|
|
51
|
+
* @returns Set<string> - All descendant schema names
|
|
52
|
+
*/
|
|
53
|
+
export function getDescendants(parentName, graph) {
|
|
54
|
+
const descendants = new Set();
|
|
55
|
+
const queue = [parentName];
|
|
56
|
+
while (queue.length > 0) {
|
|
57
|
+
const current = queue.shift();
|
|
58
|
+
const children = graph.get(current);
|
|
59
|
+
if (children) {
|
|
60
|
+
for (const child of children) {
|
|
61
|
+
if (!descendants.has(child)) {
|
|
62
|
+
descendants.add(child);
|
|
63
|
+
queue.push(child);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return descendants;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Gets all ancestors (direct and transitive) of a schema in the inheritance hierarchy.
|
|
72
|
+
*
|
|
73
|
+
* @param childName - The schema name to find ancestors for
|
|
74
|
+
* @param schemas - The components.schemas object from an OpenAPI document
|
|
75
|
+
* @returns Set<string> - All ancestor schema names
|
|
76
|
+
*/
|
|
77
|
+
export function getAncestors(childName, schemas) {
|
|
78
|
+
const ancestors = new Set();
|
|
79
|
+
const queue = [childName];
|
|
80
|
+
const visited = new Set();
|
|
81
|
+
while (queue.length > 0) {
|
|
82
|
+
const current = queue.shift();
|
|
83
|
+
if (visited.has(current))
|
|
84
|
+
continue;
|
|
85
|
+
visited.add(current);
|
|
86
|
+
const schema = schemas[current];
|
|
87
|
+
if (!schema || typeof schema !== "object" || !Array.isArray(schema.allOf))
|
|
88
|
+
continue;
|
|
89
|
+
for (const item of schema.allOf) {
|
|
90
|
+
if (item && typeof item === "object" && typeof item.$ref === "string") {
|
|
91
|
+
const parentName = refToName(item.$ref);
|
|
92
|
+
if (parentName && !ancestors.has(parentName)) {
|
|
93
|
+
ancestors.add(parentName);
|
|
94
|
+
queue.push(parentName);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return ancestors;
|
|
100
|
+
}
|
|
9
101
|
//# sourceMappingURL=oasUtils.js.map
|
package/dist/lib/oasUtils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oasUtils.js","sourceRoot":"","sources":["../../src/lib/oasUtils.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAElC;;GAEG;AACH,MAAM,UAAU,SAAS,CAAC,GAAW;IACnC,MAAM,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,yCAAyC,CAAC,CAAC;IAC/D,OAAO,CAAC,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AAClD,CAAC"}
|
|
1
|
+
{"version":3,"file":"oasUtils.js","sourceRoot":"","sources":["../../src/lib/oasUtils.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAElC;;GAEG;AACH,MAAM,UAAU,SAAS,CAAC,GAAW;IACnC,MAAM,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,yCAAyC,CAAC,CAAC;IAC/D,OAAO,CAAC,CAAC,CAAC,CAA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QAEpF,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;YAChC,IAAI,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,OAAQ,IAAY,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBAC/E,MAAM,UAAU,GAAG,SAAS,CAAE,IAAY,CAAC,IAAI,CAAC,CAAC;gBACjD,IAAI,UAAU,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;oBAC7C,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;oBAC1B,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;gBACzB,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allof-to-oneof-decorator.d.ts","sourceRoot":"","sources":["../../src/redocly/allof-to-oneof-decorator.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,OAAO,UAAU,qBAAqB,CAAC,IAAI,EAAE,GAAG;;sBAGnC,GAAG;;EAStB"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { allOfToOneOf } from "../lib/allOfToOneOf.js";
|
|
2
|
+
export default function AllOfToOneOfDecorator(opts) {
|
|
3
|
+
return {
|
|
4
|
+
Root: {
|
|
5
|
+
leave(target) {
|
|
6
|
+
allOfToOneOf(target, {
|
|
7
|
+
removeDiscriminatorFromBase: opts?.removeDiscriminatorFromBase || false,
|
|
8
|
+
addDiscriminatorConst: opts?.addDiscriminatorConst !== false,
|
|
9
|
+
ignoreSingleSpecialization: opts?.ignoreSingleSpecialization || false,
|
|
10
|
+
});
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=allof-to-oneof-decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"allof-to-oneof-decorator.js","sourceRoot":"","sources":["../../src/redocly/allof-to-oneof-decorator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD,MAAM,CAAC,OAAO,UAAU,qBAAqB,CAAC,IAAS;IACrD,OAAO;QACL,IAAI,EAAE;YACJ,KAAK,CAAC,MAAW;gBACf,YAAY,CAAC,MAAM,EAAE;oBACnB,2BAA2B,EAAE,IAAI,EAAE,2BAA2B,IAAI,KAAK;oBACvE,qBAAqB,EAAE,IAAI,EAAE,qBAAqB,KAAK,KAAK;oBAC5D,0BAA0B,EAAE,IAAI,EAAE,0BAA0B,IAAI,KAAK;iBACtE,CAAC,CAAC;YACL,CAAC;SACF;KACF,CAAC;AACJ,CAAC"}
|
package/dist/redocly/plugin.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import RemoveUnusedDecorator from "./remove-unused-decorator.js";
|
|
2
2
|
import RemoveFromOneOfDecorator from "./remove-from-oneof-decorator.js";
|
|
3
3
|
import OptimizeAllOfDecorator from "./optimize-allof-decorator.js";
|
|
4
|
+
import AllOfToOneOfDecorator from "./allof-to-oneof-decorator.js";
|
|
4
5
|
export default function oasRemoveUnusedPlugin(): {
|
|
5
6
|
id: string;
|
|
6
7
|
decorators: {
|
|
@@ -8,6 +9,7 @@ export default function oasRemoveUnusedPlugin(): {
|
|
|
8
9
|
"remove-unused": typeof RemoveUnusedDecorator;
|
|
9
10
|
"remove-oneof": typeof RemoveFromOneOfDecorator;
|
|
10
11
|
"optimize-allof": typeof OptimizeAllOfDecorator;
|
|
12
|
+
"allof-to-oneof": typeof AllOfToOneOfDecorator;
|
|
11
13
|
"remove-unused-schemas": typeof RemoveUnusedDecorator;
|
|
12
14
|
"remove-from-oneof": typeof RemoveFromOneOfDecorator;
|
|
13
15
|
};
|
|
@@ -15,6 +17,7 @@ export default function oasRemoveUnusedPlugin(): {
|
|
|
15
17
|
"remove-unused": typeof RemoveUnusedDecorator;
|
|
16
18
|
"remove-oneof": typeof RemoveFromOneOfDecorator;
|
|
17
19
|
"optimize-allof": typeof OptimizeAllOfDecorator;
|
|
20
|
+
"allof-to-oneof": typeof AllOfToOneOfDecorator;
|
|
18
21
|
"remove-unused-schemas": typeof RemoveUnusedDecorator;
|
|
19
22
|
"remove-from-oneof": typeof RemoveFromOneOfDecorator;
|
|
20
23
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/redocly/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,MAAM,8BAA8B,CAAC;AACjE,OAAO,wBAAwB,MAAM,kCAAkC,CAAC;AACxE,OAAO,sBAAsB,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/redocly/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,MAAM,8BAA8B,CAAC;AACjE,OAAO,wBAAwB,MAAM,kCAAkC,CAAC;AACxE,OAAO,sBAAsB,MAAM,+BAA+B,CAAC;AACnE,OAAO,qBAAqB,MAAM,+BAA+B,CAAC;AAElE,MAAM,CAAC,OAAO,UAAU,qBAAqB;;;;;;;;;;;;;;;;;;;;EA0B5C"}
|
package/dist/redocly/plugin.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import RemoveUnusedDecorator from "./remove-unused-decorator.js";
|
|
2
2
|
import RemoveFromOneOfDecorator from "./remove-from-oneof-decorator.js";
|
|
3
3
|
import OptimizeAllOfDecorator from "./optimize-allof-decorator.js";
|
|
4
|
+
import AllOfToOneOfDecorator from "./allof-to-oneof-decorator.js";
|
|
4
5
|
export default function oasRemoveUnusedPlugin() {
|
|
5
6
|
return {
|
|
6
7
|
id: "oas-utils",
|
|
@@ -10,6 +11,7 @@ export default function oasRemoveUnusedPlugin() {
|
|
|
10
11
|
"remove-unused": RemoveUnusedDecorator,
|
|
11
12
|
"remove-oneof": RemoveFromOneOfDecorator,
|
|
12
13
|
"optimize-allof": OptimizeAllOfDecorator,
|
|
14
|
+
"allof-to-oneof": AllOfToOneOfDecorator,
|
|
13
15
|
// Back-compat aliases
|
|
14
16
|
"remove-unused-schemas": RemoveUnusedDecorator,
|
|
15
17
|
"remove-from-oneof": RemoveFromOneOfDecorator,
|
|
@@ -19,6 +21,7 @@ export default function oasRemoveUnusedPlugin() {
|
|
|
19
21
|
"remove-unused": RemoveUnusedDecorator,
|
|
20
22
|
"remove-oneof": RemoveFromOneOfDecorator,
|
|
21
23
|
"optimize-allof": OptimizeAllOfDecorator,
|
|
24
|
+
"allof-to-oneof": AllOfToOneOfDecorator,
|
|
22
25
|
// Back-compat aliases
|
|
23
26
|
"remove-unused-schemas": RemoveUnusedDecorator,
|
|
24
27
|
"remove-from-oneof": RemoveFromOneOfDecorator,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/redocly/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,MAAM,8BAA8B,CAAC;AACjE,OAAO,wBAAwB,MAAM,kCAAkC,CAAC;AACxE,OAAO,sBAAsB,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/redocly/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,MAAM,8BAA8B,CAAC;AACjE,OAAO,wBAAwB,MAAM,kCAAkC,CAAC;AACxE,OAAO,sBAAsB,MAAM,+BAA+B,CAAC;AACnE,OAAO,qBAAqB,MAAM,+BAA+B,CAAC;AAElE,MAAM,CAAC,OAAO,UAAU,qBAAqB;IAC3C,OAAO;QACP,EAAE,EAAE,WAAW;QACb,UAAU,EAAE;YACV,IAAI,EAAE;gBACR,iDAAiD;gBACjD,eAAe,EAAE,qBAAqB;gBACtC,cAAc,EAAE,wBAAwB;gBACxC,gBAAgB,EAAE,sBAAsB;gBACxC,gBAAgB,EAAE,qBAAqB;gBACvC,sBAAsB;gBACtB,uBAAuB,EAAE,qBAAqB;gBAC9C,mBAAmB,EAAE,wBAAwB;aAC1C;YACD,IAAI,EAAE;gBACR,kBAAkB;gBAClB,eAAe,EAAE,qBAAqB;gBACtC,cAAc,EAAE,wBAAwB;gBACxC,gBAAgB,EAAE,sBAAsB;gBACxC,gBAAgB,EAAE,qBAAqB;gBACvC,sBAAsB;gBACtB,uBAAuB,EAAE,qBAAqB;gBAC9C,mBAAmB,EAAE,wBAAwB;aAC1C;SACF;KACF,CAAC;AACJ,CAAC"}
|