-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-one-of.js
48 lines (42 loc) · 1.47 KB
/
merge-one-of.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @copyright Copyright 2021, 2025 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
* @module "openapi-transformers/merge-one-of.js"
*/
import intersectSchema from 'json-schema-intersect';
import OpenApiTransformerBase from 'openapi-transformer-base';
/**
* Transformer to merge oneOf schemas into the parent schema.
*
* This is useful for converting to OpenAPI 2.0 (which does not support
* oneOf) from later versions, or to accommodate tools which do not support
* oneOf well (e.g. many strongly-typed code generators).
*/
export default class MergeOneOfTransformer extends OpenApiTransformerBase {
transformSchema(schema) {
const newSchema = super.transformSchema(schema);
if (!newSchema || typeof newSchema !== 'object') {
// Note: warning already emitted by super.transformSchema()
return newSchema;
}
const {
oneOf,
...schemaNoOneOf
} = newSchema;
if (!Array.isArray(oneOf)) {
this.warn('Unable to merge non-Array oneOf', oneOf);
return newSchema;
}
if (oneOf.length === 0) {
// Empty oneOf Array is disallowed by JSON Schema.
// Not safe to remove since all instances will fail to "validate
// successfully against exactly one schema".
this.warn('Unable to merge empty oneOf Array');
return newSchema;
}
if (oneOf.length > 1) {
throw new Error('Merging multiple oneOf schemas not implemented');
}
return intersectSchema(schemaNoOneOf, oneOf[0]);
}
}