-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-all-of.js
60 lines (49 loc) · 1.61 KB
/
merge-all-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
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @copyright Copyright 2021, 2025 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
* @module "openapi-transformers/merge-all-of.js"
*/
import { debuglog } from 'node:util';
import intersectSchema from 'json-schema-intersect';
import OpenApiTransformerBase from 'openapi-transformer-base';
const debug = debuglog('openapi-transformers:merge-all-of');
/**
* Transformer to merge allOf schemas into the parent schema.
*
* This may be useful to accommodate tools which do not support allOf well
* (e.g. some strongly-typed code generators).
*/
export default class MergeAllOfTransformer extends OpenApiTransformerBase {
onlySingle = false;
constructor({ onlySingle } = {}) {
super();
this.onlySingle = Boolean(onlySingle);
}
transformSchema(schema) {
const newSchema = super.transformSchema(schema);
if (!newSchema || typeof newSchema !== 'object') {
// Note: warning already emitted by super.transformSchema()
return newSchema;
}
const {
allOf,
...schemaNoAllOf
} = newSchema;
if (!Array.isArray(allOf)) {
this.warn('Unable to merge non-Array allOf', allOf);
return newSchema;
}
if (allOf.length === 0) {
// Empty allOf Array is disallowed by JSON Schema.
// Considered safe to remove since any instance trivially validates
// successfully against all 0 schemas.
debug('Removing empty allOf');
return schemaNoAllOf;
}
if (this.onlySingle && allOf.length > 1) {
debug('Skipping allOf with multiple schemas');
return newSchema;
}
return allOf.reduce(intersectSchema, schemaNoAllOf);
}
}