Skip to content

Commit 9b75c34

Browse files
committed
fix: handle compatible enums
1 parent 631df8b commit 9b75c34

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { JSONSchema4 } from 'json-schema';
2+
import { mergeAllOf } from '../mergeAllOf';
3+
4+
describe('mergeAllOf util', () => {
5+
describe('enums merging', () => {
6+
test('given incompatible, should mark enum as empty', () => {
7+
const schema: JSONSchema4 = {
8+
allOf: [
9+
{
10+
type: 'object',
11+
properties: {
12+
bar: {
13+
type: 'string',
14+
enum: ['jonas', 'frederik'],
15+
},
16+
},
17+
},
18+
{
19+
type: 'object',
20+
properties: {
21+
bar: {
22+
type: 'string',
23+
enum: ['john', 'chris'],
24+
},
25+
},
26+
},
27+
],
28+
};
29+
30+
expect(mergeAllOf(schema, [], jest.fn())).toEqual({
31+
properties: {
32+
bar: {
33+
enum: [],
34+
type: 'string',
35+
},
36+
},
37+
type: 'object',
38+
});
39+
});
40+
41+
test('given incompatible, should merge normally', () => {
42+
const schema: JSONSchema4 = {
43+
allOf: [
44+
{
45+
type: 'object',
46+
properties: {
47+
bar: {
48+
type: 'string',
49+
enum: ['jonas', 'frederik'],
50+
},
51+
},
52+
},
53+
{
54+
type: 'object',
55+
properties: {
56+
bar: {
57+
type: 'string',
58+
enum: ['jonas', 'frederik'],
59+
},
60+
},
61+
},
62+
],
63+
};
64+
65+
expect(mergeAllOf(schema, [], jest.fn())).toEqual({
66+
properties: {
67+
bar: {
68+
enum: ['jonas', 'frederik'],
69+
type: 'string',
70+
},
71+
},
72+
type: 'object',
73+
});
74+
});
75+
});
76+
});

src/utils/mergeAllOf.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { pathToPointer } from '@stoplight/json';
22
import { JsonPath } from '@stoplight/types';
33
import { JSONSchema4 } from 'json-schema';
4-
import { cloneDeep } from 'lodash';
4+
import { cloneDeep, compact } from 'lodash';
55
import { ResolvingError } from '../errors';
66
import { WalkerRefResolver } from '../tree/utils/populateTree';
77

@@ -14,6 +14,9 @@ function _mergeAllOf(schema: JSONSchema4, path: JsonPath, resolveRef: WalkerRefR
1414
defaultResolver(values: any) {
1515
return Object.assign({}, ...values);
1616
},
17+
enum(values: unknown[]) {
18+
return resolveAllOf.options.resolvers.enum(compact(values)) || [];
19+
},
1720
$ref(values: unknown) {
1821
return {};
1922
},

0 commit comments

Comments
 (0)