@amartus/oas-utils 0.2.0 → 0.3.1
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 +70 -3
- package/dist/cli.js +34 -1
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/cleanupDiscriminatorMappings.d.ts +21 -0
- package/dist/lib/cleanupDiscriminatorMappings.d.ts.map +1 -0
- package/dist/lib/cleanupDiscriminatorMappings.js +64 -0
- package/dist/lib/cleanupDiscriminatorMappings.js.map +1 -0
- package/dist/lib/cliActions.d.ts +24 -2
- package/dist/lib/cliActions.d.ts.map +1 -1
- package/dist/lib/cliActions.js +76 -17
- package/dist/lib/cliActions.js.map +1 -1
- package/dist/lib/removeFromOneOfByName.d.ts +1 -0
- package/dist/lib/removeFromOneOfByName.d.ts.map +1 -1
- package/dist/lib/removeFromOneOfByName.js +10 -48
- package/dist/lib/removeFromOneOfByName.js.map +1 -1
- package/dist/lib/schemaTransformUtils.d.ts +83 -0
- package/dist/lib/schemaTransformUtils.d.ts.map +1 -0
- package/dist/lib/schemaTransformUtils.js +192 -0
- package/dist/lib/schemaTransformUtils.js.map +1 -0
- package/dist/lib/sealSchema.d.ts +22 -0
- package/dist/lib/sealSchema.d.ts.map +1 -0
- package/dist/lib/sealSchema.js +420 -0
- package/dist/lib/sealSchema.js.map +1 -0
- package/dist/redocly/cleanup-discriminator-decorator.d.ts +6 -0
- package/dist/redocly/cleanup-discriminator-decorator.d.ts.map +1 -0
- package/dist/redocly/cleanup-discriminator-decorator.js +17 -0
- package/dist/redocly/cleanup-discriminator-decorator.js.map +1 -0
- package/dist/redocly/plugin.d.ts +6 -0
- package/dist/redocly/plugin.d.ts.map +1 -1
- package/dist/redocly/plugin.js +6 -0
- package/dist/redocly/plugin.js.map +1 -1
- package/dist/redocly/seal-schema-decorator.d.ts +6 -0
- package/dist/redocly/seal-schema-decorator.d.ts.map +1 -0
- package/dist/redocly/seal-schema-decorator.js +13 -0
- package/dist/redocly/seal-schema-decorator.js.map +1 -0
- 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, remove entries from oneOf, optimize allOf composition,
|
|
3
|
+
Utilities for working with OpenAPI (OAS) documents. Includes tools to remove unused schemas, remove entries from oneOf, optimize allOf composition, convert allOf + discriminator patterns to oneOf + discriminator, and clean up discriminator mappings. Use them as a CLI or as Redocly decorators.
|
|
4
4
|
|
|
5
5
|
## Definition of "unused schema"
|
|
6
6
|
|
|
@@ -101,6 +101,59 @@ Example transformation (with addDiscriminatorConst enabled, the default):
|
|
|
101
101
|
- Adds `type: {const: "Cat"}` to Cat's properties and `type: {const: "Dog"}` to Dog's properties
|
|
102
102
|
- Replaces references to `Animal` with `AnimalPolymorphic` in array items and other polymorphic contexts
|
|
103
103
|
|
|
104
|
+
### cleanup-discriminators
|
|
105
|
+
|
|
106
|
+
Clean up discriminator mappings by removing entries that reference non-existent schemas. This is useful when schemas are removed but discriminator mappings are not updated, leaving dangling references.
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
oas-utils cleanup-discriminators <input.yaml> -o output.yaml
|
|
110
|
+
# Read from stdin and write to stdout
|
|
111
|
+
cat openapi.yaml | oas-utils cleanup-discriminators > cleaned.yaml
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Options:
|
|
115
|
+
- -o, --output: write result to this file (defaults to stdout).
|
|
116
|
+
|
|
117
|
+
Example:
|
|
118
|
+
- Original discriminator mapping: `{cat: '#/components/schemas/Cat', dog: '#/components/schemas/Dog', bird: '#/components/schemas/Bird'}`
|
|
119
|
+
- After removing `Bird` schema: mapping entries `bird` is invalid
|
|
120
|
+
- After cleanup: `{cat: '#/components/schemas/Cat', dog: '#/components/schemas/Dog'}`
|
|
121
|
+
|
|
122
|
+
### seal-schema
|
|
123
|
+
|
|
124
|
+
Seal object schemas to prevent additional properties. This ensures every final object shape exposed in the API is sealed (no additional properties allowed), without breaking schemas that are extended via `allOf`.
|
|
125
|
+
|
|
126
|
+
The algorithm:
|
|
127
|
+
1. Identifies schemas that are bases for extension (referenced in `allOf`)
|
|
128
|
+
2. For each such schema, creates a `Core` variant and a sealed wrapper
|
|
129
|
+
3. Rewrites `allOf` references to point to `Core` variants (allowing extension)
|
|
130
|
+
4. Seals composition roots (`allOf`, `anyOf`, `oneOf`) and direct-use schemas
|
|
131
|
+
|
|
132
|
+
This ensures:
|
|
133
|
+
- **Objects used directly** in fields or arrays are fully sealed
|
|
134
|
+
- **Objects used as bases** for extension remain unsealed internally but are sealed at the wrapper level
|
|
135
|
+
- **anyOf/oneOf compositions** remain valid alternatives and are sealed at the outer level
|
|
136
|
+
|
|
137
|
+
```
|
|
138
|
+
oas-utils seal-schema <input.yaml> -o output.yaml
|
|
139
|
+
# Use unevaluatedProperties: false (default, recommended for JSON Schema 2019-09+)
|
|
140
|
+
oas-utils seal-schema <input.yaml> -o output.yaml --use-unevaluated-properties
|
|
141
|
+
# Use additionalProperties: false instead
|
|
142
|
+
oas-utils seal-schema <input.yaml> -o output.yaml --use-additional-properties
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Options:
|
|
146
|
+
- -o, --output: write result to this file (defaults to stdout).
|
|
147
|
+
- --use-unevaluated-properties: use `unevaluatedProperties: false` (default, better for JSON Schema 2019-09+).
|
|
148
|
+
- --use-additional-properties: use `additionalProperties: false` instead.
|
|
149
|
+
|
|
150
|
+
Example transformation:
|
|
151
|
+
- Original `Animal` schema (object-like, referenced in `allOf` by `Cat`)
|
|
152
|
+
- Becomes: `AnimalCore` (unsealed original) + `Animal` wrapper with `allOf: [{$ref: AnimalCore}]` and `unevaluatedProperties: false`
|
|
153
|
+
- `Cat` now uses `allOf: [{$ref: AnimalCore}, {...}]` allowing safe extension
|
|
154
|
+
- Direct references to `Animal` point to the sealed wrapper
|
|
155
|
+
- Inline objects and composition roots are sealed with appropriate keywords
|
|
156
|
+
|
|
104
157
|
## As Redocly decorators
|
|
105
158
|
|
|
106
159
|
1) Add the plugin to `plugins` in your `redocly.yaml` (path relative to the config):
|
|
@@ -133,6 +186,13 @@ decorators:
|
|
|
133
186
|
removeDiscriminatorFromBase: false
|
|
134
187
|
addDiscriminatorConst: true
|
|
135
188
|
ignoreSingleSpecialization: false
|
|
189
|
+
|
|
190
|
+
# Clean up discriminator mappings
|
|
191
|
+
oas-utils/cleanup-discriminators: {}
|
|
192
|
+
|
|
193
|
+
# Seal object schemas
|
|
194
|
+
oas-utils/seal-schema:
|
|
195
|
+
useUnevaluatedProperties: true
|
|
136
196
|
```
|
|
137
197
|
|
|
138
198
|
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.
|
|
@@ -143,13 +203,20 @@ Notes:
|
|
|
143
203
|
## Programmatic usage
|
|
144
204
|
|
|
145
205
|
```
|
|
146
|
-
import { removeUnusedSchemas, allOfToOneOf } from 'oas-utils';
|
|
206
|
+
import { removeUnusedSchemas, allOfToOneOf, sealSchema, cleanupDiscriminatorMappings } from 'oas-utils';
|
|
147
207
|
|
|
148
208
|
// Remove unused schemas
|
|
149
|
-
|
|
209
|
+
removeUnusedSchemas(doc, { keep: ['CommonError'], aggressive: true });
|
|
150
210
|
|
|
151
211
|
// Convert allOf + discriminator to oneOf + discriminator
|
|
152
212
|
allOfToOneOf(doc, { removeDiscriminatorFromBase: false, addDiscriminatorConst: true });
|
|
213
|
+
|
|
214
|
+
// Clean up discriminator mappings
|
|
215
|
+
const result = cleanupDiscriminatorMappings(doc);
|
|
216
|
+
console.log(`Removed ${result.mappingsRemoved} invalid mappings from ${result.schemasChecked} schemas`);
|
|
217
|
+
|
|
218
|
+
// Seal object schemas
|
|
219
|
+
sealSchema(doc, { useUnevaluatedProperties: true });
|
|
153
220
|
```
|
|
154
221
|
|
|
155
222
|
## 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, runAllOfToOneOf } from "./lib/cliActions.js";
|
|
4
|
+
import { runRemoveUnused, runRemoveOneOf, optimizeAllOf, runAllOfToOneOf, runSealSchema, runCleanupDiscriminators } from "./lib/cliActions.js";
|
|
5
5
|
import YAML from "yaml";
|
|
6
6
|
const program = new Command();
|
|
7
7
|
program
|
|
@@ -67,6 +67,39 @@ program
|
|
|
67
67
|
process.exitCode = 1;
|
|
68
68
|
}
|
|
69
69
|
});
|
|
70
|
+
program
|
|
71
|
+
.command("seal-schema")
|
|
72
|
+
.showHelpAfterError()
|
|
73
|
+
.description("Seal object schemas to prevent additional properties")
|
|
74
|
+
.argument("[input]", "Path to input OpenAPI file (YAML or JSON). If omitted, reads from stdin")
|
|
75
|
+
.option("-o, --output <file>", "Write result to this file (defaults to stdout)")
|
|
76
|
+
.option("--use-unevaluated-properties", "Use unevaluatedProperties: false instead of additionalProperties: false (default: true)", true)
|
|
77
|
+
.option("--use-additional-properties", "Use additionalProperties: false instead of unevaluatedProperties: false", false)
|
|
78
|
+
.action(async (input, opts) => {
|
|
79
|
+
try {
|
|
80
|
+
const useUnevaluated = !opts.useAdditionalProperties;
|
|
81
|
+
await runSealSchema({ output: opts.output, useUnevaluatedProperties: useUnevaluated }, format, () => reader(input));
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
console.error(`Error: ${err?.message || String(err)}`);
|
|
85
|
+
process.exitCode = 1;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
program
|
|
89
|
+
.command("cleanup-discriminators")
|
|
90
|
+
.showHelpAfterError()
|
|
91
|
+
.description("Clean up discriminator mappings by removing references to non-existent schemas")
|
|
92
|
+
.argument("[input]", "Path to input OpenAPI file (YAML or JSON). If omitted, reads from stdin")
|
|
93
|
+
.option("-o, --output <file>", "Write result to this file (defaults to stdout)")
|
|
94
|
+
.action(async (input, opts) => {
|
|
95
|
+
try {
|
|
96
|
+
await runCleanupDiscriminators(opts, format, () => reader(input));
|
|
97
|
+
}
|
|
98
|
+
catch (err) {
|
|
99
|
+
console.error(`Error: ${err?.message || String(err)}`);
|
|
100
|
+
process.exitCode = 1;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
70
103
|
if (process.argv.length <= 2) {
|
|
71
104
|
program.help();
|
|
72
105
|
}
|
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,CAAC;AACpC,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;
|
|
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,CAAC;AACpC,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,aAAa,EAAE,eAAe,EAAE,aAAa,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAC/I,OAAO,IAAI,MAAM,MAAM,CAAC;AAGxB,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;AAC9B,OAAO;KACJ,OAAO,CAAC,eAAe,CAAC;KACxB,kBAAkB,EAAE;KACpB,WAAW,CAAC,kDAAkD,CAAC;KAC/D,QAAQ,CACP,SAAS,EACT,yEAAyE,CAC1E;KACA,MAAM,CACL,qBAAqB,EACrB,gDAAgD,CACjD;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}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,5 +2,8 @@ export * from './lib/removeUnusedSchemas.js';
|
|
|
2
2
|
export * from './lib/removeFromOneOfByName.js';
|
|
3
3
|
export * from './lib/optimizeAllOfComposition.js';
|
|
4
4
|
export * from './lib/allOfToOneOf.js';
|
|
5
|
+
export * from './lib/sealSchema.js';
|
|
5
6
|
export * from './lib/oasUtils.js';
|
|
7
|
+
export * from './lib/cleanupDiscriminatorMappings.js';
|
|
8
|
+
export * from './lib/schemaTransformUtils.js';
|
|
6
9
|
//# sourceMappingURL=index.d.ts.map
|
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;AAClD,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,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,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uCAAuC,CAAC;AACtD,cAAc,+BAA+B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -2,5 +2,8 @@ export * from './lib/removeUnusedSchemas.js';
|
|
|
2
2
|
export * from './lib/removeFromOneOfByName.js';
|
|
3
3
|
export * from './lib/optimizeAllOfComposition.js';
|
|
4
4
|
export * from './lib/allOfToOneOf.js';
|
|
5
|
+
export * from './lib/sealSchema.js';
|
|
5
6
|
export * from './lib/oasUtils.js';
|
|
7
|
+
export * from './lib/cleanupDiscriminatorMappings.js';
|
|
8
|
+
export * from './lib/schemaTransformUtils.js';
|
|
6
9
|
//# sourceMappingURL=index.js.map
|
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;AAClD,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,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,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uCAAuC,CAAC;AACtD,cAAc,+BAA+B,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cleans up discriminator mappings by removing entries that point to non-existent schemas.
|
|
3
|
+
*
|
|
4
|
+
* This function:
|
|
5
|
+
* 1. Iterates through all schemas looking for discriminator definitions
|
|
6
|
+
* 2. For each discriminator mapping entry, checks if the referenced schema exists
|
|
7
|
+
* 3. Removes mapping entries that reference undefined schemas
|
|
8
|
+
* 4. Returns count of cleaned mappings for reporting
|
|
9
|
+
*
|
|
10
|
+
* @param doc - OpenAPI document to clean
|
|
11
|
+
* @returns Object with cleanup statistics
|
|
12
|
+
*/
|
|
13
|
+
export declare function cleanupDiscriminatorMappings(doc: any): {
|
|
14
|
+
schemasChecked: number;
|
|
15
|
+
mappingsRemoved: number;
|
|
16
|
+
details: Array<{
|
|
17
|
+
schema: string;
|
|
18
|
+
removed: string[];
|
|
19
|
+
}>;
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=cleanupDiscriminatorMappings.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cleanupDiscriminatorMappings.d.ts","sourceRoot":"","sources":["../../src/lib/cleanupDiscriminatorMappings.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;GAWG;AACH,wBAAgB,4BAA4B,CAAC,GAAG,EAAE,GAAG,GAAG;IACtD,cAAc,EAAE,MAAM,CAAC;IACvB,eAAe,EAAE,MAAM,CAAC;IACxB,OAAO,EAAE,KAAK,CAAC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAC;CACvD,CAyDA"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { refToName } from "./oasUtils.js";
|
|
2
|
+
import { updateDiscriminatorMappings } from "./schemaTransformUtils.js";
|
|
3
|
+
/**
|
|
4
|
+
* Cleans up discriminator mappings by removing entries that point to non-existent schemas.
|
|
5
|
+
*
|
|
6
|
+
* This function:
|
|
7
|
+
* 1. Iterates through all schemas looking for discriminator definitions
|
|
8
|
+
* 2. For each discriminator mapping entry, checks if the referenced schema exists
|
|
9
|
+
* 3. Removes mapping entries that reference undefined schemas
|
|
10
|
+
* 4. Returns count of cleaned mappings for reporting
|
|
11
|
+
*
|
|
12
|
+
* @param doc - OpenAPI document to clean
|
|
13
|
+
* @returns Object with cleanup statistics
|
|
14
|
+
*/
|
|
15
|
+
export function cleanupDiscriminatorMappings(doc) {
|
|
16
|
+
if (!doc || typeof doc !== "object") {
|
|
17
|
+
return { schemasChecked: 0, mappingsRemoved: 0, details: [] };
|
|
18
|
+
}
|
|
19
|
+
const schemas = doc.components?.schemas;
|
|
20
|
+
if (!schemas || typeof schemas !== "object") {
|
|
21
|
+
return { schemasChecked: 0, mappingsRemoved: 0, details: [] };
|
|
22
|
+
}
|
|
23
|
+
// Collect all existing schema names for validation
|
|
24
|
+
const existingSchemas = new Set(Object.keys(schemas));
|
|
25
|
+
let totalMappingsRemoved = 0;
|
|
26
|
+
let schemasChecked = 0;
|
|
27
|
+
const details = [];
|
|
28
|
+
// Iterate through all schemas
|
|
29
|
+
for (const [schemaName, schema] of Object.entries(schemas)) {
|
|
30
|
+
if (!schema || typeof schema !== "object")
|
|
31
|
+
continue;
|
|
32
|
+
// Check if schema has discriminator with mapping
|
|
33
|
+
if (schema.discriminator && typeof schema.discriminator === "object") {
|
|
34
|
+
const discriminator = schema.discriminator;
|
|
35
|
+
if (discriminator.mapping && typeof discriminator.mapping === "object") {
|
|
36
|
+
schemasChecked++;
|
|
37
|
+
const removedMappings = [];
|
|
38
|
+
// Update discriminator mappings, removing invalid references
|
|
39
|
+
const removed = updateDiscriminatorMappings(schema, (key, ref) => {
|
|
40
|
+
const referencedSchemaName = refToName(ref);
|
|
41
|
+
// Keep this mapping only if the referenced schema exists
|
|
42
|
+
const shouldKeep = !referencedSchemaName || existingSchemas.has(referencedSchemaName);
|
|
43
|
+
if (!shouldKeep) {
|
|
44
|
+
removedMappings.push(key);
|
|
45
|
+
}
|
|
46
|
+
return shouldKeep;
|
|
47
|
+
});
|
|
48
|
+
totalMappingsRemoved += removed;
|
|
49
|
+
if (removedMappings.length > 0) {
|
|
50
|
+
details.push({
|
|
51
|
+
schema: schemaName,
|
|
52
|
+
removed: removedMappings,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
schemasChecked,
|
|
60
|
+
mappingsRemoved: totalMappingsRemoved,
|
|
61
|
+
details,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=cleanupDiscriminatorMappings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cleanupDiscriminatorMappings.js","sourceRoot":"","sources":["../../src/lib/cleanupDiscriminatorMappings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,OAAO,EAAE,2BAA2B,EAAE,MAAM,2BAA2B,CAAC;AAExE;;;;;;;;;;;GAWG;AACH,M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AAE,GAAW,EAAE,EAAE;oBAC/E,MAAM,oBAAoB,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;oBAC5C,yDAAyD;oBACzD,MAAM,UAAU,GAAG,CAAC,oBAAoB,IAAI,eAAe,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC;oBACtF,IAAI,CAAC,UAAU,EAAE,CAAC;wBAChB,eAAe,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;oBAC5B,CAAC;oBACD,OAAO,UAAU,CAAC;gBACpB,CAAC,CAAC,CAAC;gBAEH,oBAAoB,IAAI,OAAO,CAAC;gBAEhC,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBAC/B,OAAO,CAAC,IAAI,CAAC;wBACX,MAAM,EAAE,UAAU;wBAClB,OAAO,EAAE,eAAe;qBACzB,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO;QACL,cAAc;QACd,eAAe,EAAE,oBAAoB;QACrC,OAAO;KACR,CAAC;AACJ,CAAC"}
|
package/dist/lib/cliActions.d.ts
CHANGED
|
@@ -13,8 +13,9 @@ export declare function runRemoveOneOf(opts: {
|
|
|
13
13
|
/**
|
|
14
14
|
* Optimizes allOf composition in the provided OpenAPI document.
|
|
15
15
|
*
|
|
16
|
-
* @param
|
|
17
|
-
* @param
|
|
16
|
+
* @param opts - Options including output path
|
|
17
|
+
* @param format - Function to format output
|
|
18
|
+
* @param reader - Function to read input
|
|
18
19
|
*/
|
|
19
20
|
export declare function optimizeAllOf(opts: {
|
|
20
21
|
output?: string;
|
|
@@ -32,4 +33,25 @@ export declare function runAllOfToOneOf(opts: {
|
|
|
32
33
|
addDiscriminatorConst?: boolean;
|
|
33
34
|
ignoreSingleSpecialization?: boolean;
|
|
34
35
|
}, format: (doc: any, target?: string) => string, reader: () => Promise<string>): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Seals object schemas to prevent additional properties.
|
|
38
|
+
*
|
|
39
|
+
* @param opts - Options including output path and sealing options
|
|
40
|
+
* @param format - Function to format output
|
|
41
|
+
* @param reader - Function to read input
|
|
42
|
+
*/
|
|
43
|
+
export declare function runSealSchema(opts: {
|
|
44
|
+
output?: string;
|
|
45
|
+
useUnevaluatedProperties?: boolean;
|
|
46
|
+
}, format: (doc: any, target?: string) => string, reader: () => Promise<string>): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Cleans up discriminator mappings by removing entries pointing to non-existent schemas.
|
|
49
|
+
*
|
|
50
|
+
* @param opts - Options including output path
|
|
51
|
+
* @param format - Function to format output
|
|
52
|
+
* @param reader - Function to read input
|
|
53
|
+
*/
|
|
54
|
+
export declare function runCleanupDiscriminators(opts: {
|
|
55
|
+
output?: string;
|
|
56
|
+
}, format: (doc: any, target?: string) => string, reader: () => Promise<string>): Promise<void>;
|
|
35
57
|
//# 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":"AAyEA,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,iBAiB9B;AAQD,wBAAsB,cAAc,CAClC,IAAI,EAAE;IAAE,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAC;IAAC,KAAK,EAAE,OAAO,CAA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AAK,MAAM,EAC7C,MAAM,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,iBAuB9B"}
|
package/dist/lib/cliActions.js
CHANGED
|
@@ -5,6 +5,8 @@ import { optimizeAllOfComposition } from "./optimizeAllOfComposition.js";
|
|
|
5
5
|
import { removeUnusedSchemas } from "./removeUnusedSchemas.js";
|
|
6
6
|
import { removeFromOneOfByName, removeFromOneOfGlobally, } from "./removeFromOneOfByName.js";
|
|
7
7
|
import { allOfToOneOf } from "./allOfToOneOf.js";
|
|
8
|
+
import { sealSchema } from "./sealSchema.js";
|
|
9
|
+
import { cleanupDiscriminatorMappings } from "./cleanupDiscriminatorMappings.js";
|
|
8
10
|
function parseYamlOrJson(data) {
|
|
9
11
|
// Accept pre-parsed objects (useful in tests)
|
|
10
12
|
if (data && typeof data === "object")
|
|
@@ -44,17 +46,34 @@ function logSchemaChanges(before, after) {
|
|
|
44
46
|
}
|
|
45
47
|
}
|
|
46
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Helper to validate that a document has components.schemas
|
|
51
|
+
* Logs error and returns false if invalid
|
|
52
|
+
*/
|
|
53
|
+
function validateComponentSchemas(doc) {
|
|
54
|
+
if (!doc.components || !doc.components.schemas) {
|
|
55
|
+
console.error("[ERROR] The input document does not contain valid components.schemas.");
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Helper to convert option values to arrays if needed
|
|
62
|
+
*/
|
|
63
|
+
function toArray(value) {
|
|
64
|
+
if (Array.isArray(value))
|
|
65
|
+
return value;
|
|
66
|
+
return value ? String(value).split(",").map(s => s.trim()).filter(Boolean) : [];
|
|
67
|
+
}
|
|
47
68
|
export async function runRemoveUnused(opts, format, reader) {
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
const ignoreParents = Array.isArray(opts.ignoreParents) ? opts.ignoreParents : (opts.ignoreParents ? String(opts.ignoreParents).split(",").map(s => s.trim()).filter(Boolean) : []);
|
|
69
|
+
const keep = toArray(opts.keep);
|
|
70
|
+
const ignoreParents = toArray(opts.ignoreParents);
|
|
51
71
|
const ropts = {
|
|
52
72
|
keep,
|
|
53
73
|
aggressive: Boolean(opts.aggressive),
|
|
54
74
|
ignoreParents,
|
|
55
75
|
};
|
|
56
|
-
|
|
57
|
-
doc = parseYamlOrJson(await reader());
|
|
76
|
+
const doc = parseYamlOrJson(await reader());
|
|
58
77
|
const beforeSchemas = Object.keys(doc?.components?.schemas ?? {});
|
|
59
78
|
removeUnusedSchemas(doc, ropts);
|
|
60
79
|
const afterSchemas = Object.keys(doc?.components?.schemas ?? {});
|
|
@@ -67,10 +86,8 @@ function guess(name, doc) {
|
|
|
67
86
|
}
|
|
68
87
|
export async function runRemoveOneOf(opts, format, reader) {
|
|
69
88
|
const doc = parseYamlOrJson(await reader());
|
|
70
|
-
if (!doc
|
|
71
|
-
console.error("[ERROR] The input document does not contain valid components.schemas.");
|
|
89
|
+
if (!validateComponentSchemas(doc))
|
|
72
90
|
return;
|
|
73
|
-
}
|
|
74
91
|
const toRemove = opts.guess ? opts.remove.flatMap((name) => guess(name, doc)) : opts.remove;
|
|
75
92
|
if (opts.parent) {
|
|
76
93
|
let anyChanged = false;
|
|
@@ -109,12 +126,12 @@ export async function runRemoveOneOf(opts, format, reader) {
|
|
|
109
126
|
/**
|
|
110
127
|
* Optimizes allOf composition in the provided OpenAPI document.
|
|
111
128
|
*
|
|
112
|
-
* @param
|
|
113
|
-
* @param
|
|
129
|
+
* @param opts - Options including output path
|
|
130
|
+
* @param format - Function to format output
|
|
131
|
+
* @param reader - Function to read input
|
|
114
132
|
*/
|
|
115
133
|
export async function optimizeAllOf(opts, format, reader) {
|
|
116
|
-
const
|
|
117
|
-
const doc = parseYamlOrJson(data);
|
|
134
|
+
const doc = parseYamlOrJson(await reader());
|
|
118
135
|
optimizeAllOfComposition(doc);
|
|
119
136
|
await writeOutput(doc, opts.output, format);
|
|
120
137
|
}
|
|
@@ -126,12 +143,9 @@ export async function optimizeAllOf(opts, format, reader) {
|
|
|
126
143
|
* @param reader - Function to read input
|
|
127
144
|
*/
|
|
128
145
|
export async function runAllOfToOneOf(opts, format, reader) {
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
if (!doc.components || !doc.components.schemas) {
|
|
132
|
-
console.error("[ERROR] The input document does not contain valid components.schemas.");
|
|
146
|
+
const doc = parseYamlOrJson(await reader());
|
|
147
|
+
if (!validateComponentSchemas(doc))
|
|
133
148
|
return;
|
|
134
|
-
}
|
|
135
149
|
const beforeSchemas = Object.keys(doc.components.schemas);
|
|
136
150
|
const topts = {
|
|
137
151
|
removeDiscriminatorFromBase: Boolean(opts.removeDiscriminatorFromBase),
|
|
@@ -149,4 +163,49 @@ export async function runAllOfToOneOf(opts, format, reader) {
|
|
|
149
163
|
}
|
|
150
164
|
await writeOutput(doc, opts.output, format);
|
|
151
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Seals object schemas to prevent additional properties.
|
|
168
|
+
*
|
|
169
|
+
* @param opts - Options including output path and sealing options
|
|
170
|
+
* @param format - Function to format output
|
|
171
|
+
* @param reader - Function to read input
|
|
172
|
+
*/
|
|
173
|
+
export async function runSealSchema(opts, format, reader) {
|
|
174
|
+
const doc = parseYamlOrJson(await reader());
|
|
175
|
+
const sopts = {
|
|
176
|
+
useUnevaluatedProperties: opts.useUnevaluatedProperties !== false,
|
|
177
|
+
};
|
|
178
|
+
const result = sealSchema(doc, sopts);
|
|
179
|
+
const sealingKeyword = sopts.useUnevaluatedProperties ? "unevaluatedProperties" : "additionalProperties";
|
|
180
|
+
await writeOutput(result, opts.output, format);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Cleans up discriminator mappings by removing entries pointing to non-existent schemas.
|
|
184
|
+
*
|
|
185
|
+
* @param opts - Options including output path
|
|
186
|
+
* @param format - Function to format output
|
|
187
|
+
* @param reader - Function to read input
|
|
188
|
+
*/
|
|
189
|
+
export async function runCleanupDiscriminators(opts, format, reader) {
|
|
190
|
+
const doc = parseYamlOrJson(await reader());
|
|
191
|
+
if (!validateComponentSchemas(doc))
|
|
192
|
+
return;
|
|
193
|
+
const result = cleanupDiscriminatorMappings(doc);
|
|
194
|
+
if (result.schemasChecked > 0) {
|
|
195
|
+
console.error(`[CLEANUP-DISCRIMINATORS] Checked ${result.schemasChecked} schema(s) with discriminators.`);
|
|
196
|
+
if (result.mappingsRemoved > 0) {
|
|
197
|
+
console.error(`[CLEANUP-DISCRIMINATORS] Removed ${result.mappingsRemoved} mapping(s).`);
|
|
198
|
+
for (const detail of result.details) {
|
|
199
|
+
console.error(`[CLEANUP-DISCRIMINATORS] Schema '${detail.schema}': removed mappings [${detail.removed.join(", ")}]`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
console.error("[INFO] All discriminator mappings are valid (no changes needed).");
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
console.error("[INFO] No schemas with discriminators found.");
|
|
208
|
+
}
|
|
209
|
+
await writeOutput(doc, opts.output, format);
|
|
210
|
+
}
|
|
152
211
|
//# 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,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AACzE,OAAO,EAAE,mBAAmB,EAAiB,MAAM,0BAA0B,CAAC;AAC9E,OAAO,EACL,qBAAqB,EACrB,uBAAuB,GACxB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,YAAY,EAAuB,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"cliActions.js","sourceRoot":"","sources":["../../src/lib/cliAct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,CAAC;QAAE,OAAO;IAE3C,MAAM,MAAM,GAAG,4BAA4B,CAAC,GAAG,CAAC,CAAC;IAEjD,IAAI,MAAM,CAAC,cAAc,GAAG,CAAC,EAAE,CAAC;QAC9B,OAAO,CAAC,KAAK,CAAC,oCAAoC,MAAM,CAAC,cAAc,iCAAiC,CAAC,CAAC;QAC1G,IAAI,MAAM,CAAC,eAAe,GAAG,CAAC,EAAE,CAAC;YAC/B,OAAO,CAAC,KAAK,CAAC,oCAAoC,MAAM,CAAC,eAAe,cAAc,CAAC,CAAC;YACxF,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;gBACpC,OAAO,CAAC,KAAK,CAAC,sCAAsC,MAAM,CAAC,MAAM,wBAAwB,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACzH,CAAC;QACH,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,KAAK,CAAC,kEAAkE,CAAC,CAAC;QACpF,CAAC;IACH,CAAC;SAAM,CAAC;QACN,OAAO,CAAC,KAAK,CAAC,8CAA8C,CAAC,CAAC;IAChE,CAAC;IAED,MAAM,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAC9C,CAAC"}
|
|
@@ -10,6 +10,7 @@ export declare function removeFromOneOfGlobally(doc: any, removeName: string): n
|
|
|
10
10
|
* @param doc OpenAPI document
|
|
11
11
|
* @param parentSchemaName Name of the parent schema containing oneOf
|
|
12
12
|
* @param removeName Name of the schema to remove from oneOf
|
|
13
|
+
* @returns true if schema was removed
|
|
13
14
|
*/
|
|
14
15
|
export declare function removeFromOneOfByName(doc: any, parentSchemaName: string, removeName: string): boolean;
|
|
15
16
|
//# sourceMappingURL=removeFromOneOfByName.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"removeFromOneOfByName.d.ts","sourceRoot":"","sources":["../../src/lib/removeFromOneOfByName.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"removeFromOneOfByName.d.ts","sourceRoot":"","sources":["../../src/lib/removeFromOneOfByName.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAU5E;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,GAAG,EAAE,gBAAgB,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAOrG"}
|
|
@@ -1,29 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
if (node.discriminator && node.discriminator.mapping) {
|
|
3
|
-
for (const [key, ref] of Object.entries(node.discriminator.mapping)) {
|
|
4
|
-
const name = refToName(ref);
|
|
5
|
-
if (name === removeName) {
|
|
6
|
-
delete node.discriminator.mapping[key];
|
|
7
|
-
return true;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
function updateOneOf(node, removeName) {
|
|
14
|
-
if (!node || !Array.isArray(node.oneOf)) {
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
17
|
-
const beforeLength = node.oneOf.length;
|
|
18
|
-
node.oneOf = node.oneOf.filter((item) => {
|
|
19
|
-
if (typeof item?.$ref === "string") {
|
|
20
|
-
const name = refToName(item.$ref);
|
|
21
|
-
return name !== removeName;
|
|
22
|
-
}
|
|
23
|
-
return true;
|
|
24
|
-
});
|
|
25
|
-
return beforeLength !== node.oneOf.length;
|
|
26
|
-
}
|
|
1
|
+
import { removeFromCollectionAndUpdateDiscriminator, traverseAndTransform } from "./schemaTransformUtils.js";
|
|
27
2
|
/**
|
|
28
3
|
* Remove a schema from all oneOfs in the OAS and update discriminator mappings globally.
|
|
29
4
|
* @param doc OpenAPI document
|
|
@@ -31,40 +6,27 @@ function updateOneOf(node, removeName) {
|
|
|
31
6
|
* @returns Number of schemas modified
|
|
32
7
|
*/
|
|
33
8
|
export function removeFromOneOfGlobally(doc, removeName) {
|
|
9
|
+
const schemaNames = new Set([removeName]);
|
|
34
10
|
let modified = 0;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
node.forEach(traverse);
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
if (updateOneOf(node, removeName)) {
|
|
43
|
-
updateDiscriminator(node, removeName);
|
|
44
|
-
modified++;
|
|
45
|
-
}
|
|
46
|
-
for (const k of Object.keys(node)) {
|
|
47
|
-
traverse(node[k]);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
traverse(doc);
|
|
11
|
+
const transformer = (node) => {
|
|
12
|
+
return removeFromCollectionAndUpdateDiscriminator(node, "oneOf", schemaNames);
|
|
13
|
+
};
|
|
14
|
+
modified = traverseAndTransform(doc, transformer);
|
|
51
15
|
return modified;
|
|
52
16
|
}
|
|
53
|
-
import { refToName } from './oasUtils.js';
|
|
54
17
|
/**
|
|
55
18
|
* Remove a schema from oneOf by name and update discriminator mappings.
|
|
56
19
|
* @param doc OpenAPI document
|
|
57
20
|
* @param parentSchemaName Name of the parent schema containing oneOf
|
|
58
21
|
* @param removeName Name of the schema to remove from oneOf
|
|
22
|
+
* @returns true if schema was removed
|
|
59
23
|
*/
|
|
60
24
|
export function removeFromOneOfByName(doc, parentSchemaName, removeName) {
|
|
61
25
|
if (!doc?.components?.schemas)
|
|
62
26
|
return false;
|
|
63
27
|
const parentSchema = doc.components.schemas[parentSchemaName];
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
return false;
|
|
28
|
+
if (!parentSchema)
|
|
29
|
+
return false;
|
|
30
|
+
return removeFromCollectionAndUpdateDiscriminator(parentSchema, "oneOf", new Set([removeName]));
|
|
69
31
|
}
|
|
70
32
|
//# sourceMappingURL=removeFromOneOfByName.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"removeFromOneOfByName.js","sourceRoot":"","sources":["../../src/lib/removeFromOneOfByName.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"removeFromOneOfByName.js","sourceRoot":"","sources":["../../src/lib/removeFromOneOfByName.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,0CAA0C,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAE7G;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB,CAAC,GAAQ,EAAE,UAAkB;IAClE,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;IAC1C,IAAI,QAAQ,GAAG,CAAC,CAAC;IAEjB,MAAM,WAAW,GAAG,CAAC,IAAS,EAAW,EAAE;QACzC,OAAO,0CAA0C,CAAC,IAAI,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC;IAChF,CAAC,CAAC;IAEF,QAAQ,GAAG,oBAAoB,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;IAClD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CAAC,GAAQ,EAAE,gBAAwB,EAAE,UAAkB;IAC1F,IAAI,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO;QAAE,OAAO,KAAK,CAAC;IAE5C,MAAM,YAAY,GAAG,GAAG,CAAC,UAAU,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC9D,IAAI,CAAC,YAAY;QAAE,OAAO,KAAK,CAAC;IAEhC,OAAO,0CAA0C,CAAC,YAAY,EAAE,OAAO,EAAE,IAAI,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AAClG,CAAC"}
|