Skip to content

Commit d633b46

Browse files
committed
fix: example merging
1 parent 9b75c34 commit d633b46

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

src/utils/__tests__/mergeAllOf.spec.ts

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,80 @@ import { JSONSchema4 } from 'json-schema';
22
import { mergeAllOf } from '../mergeAllOf';
33

44
describe('mergeAllOf util', () => {
5+
describe('example merging', () => {
6+
test('given incompatible, should leave empty', () => {
7+
const schema: JSONSchema4 = {
8+
allOf: [
9+
{
10+
type: 'object',
11+
properties: {
12+
bar: {
13+
type: 'string',
14+
example: 'hello',
15+
},
16+
},
17+
},
18+
{
19+
type: 'object',
20+
properties: {
21+
bar: {
22+
type: 'string',
23+
example: 'bye',
24+
},
25+
},
26+
},
27+
],
28+
};
29+
30+
expect(mergeAllOf(schema, [], jest.fn())).toEqual({
31+
properties: {
32+
bar: {
33+
example: null,
34+
type: 'string',
35+
},
36+
},
37+
type: 'object',
38+
});
39+
});
40+
41+
test('given compatible, should merge normally', () => {
42+
const schema: JSONSchema4 = {
43+
allOf: [
44+
{
45+
type: 'object',
46+
properties: {
47+
bar: {
48+
type: 'string',
49+
example: 'hello',
50+
},
51+
},
52+
},
53+
{
54+
type: 'object',
55+
properties: {
56+
bar: {
57+
type: 'string',
58+
example: 'hello',
59+
},
60+
},
61+
},
62+
],
63+
};
64+
65+
expect(mergeAllOf(schema, [], jest.fn())).toEqual({
66+
properties: {
67+
bar: {
68+
example: 'hello',
69+
type: 'string',
70+
},
71+
},
72+
type: 'object',
73+
});
74+
});
75+
});
76+
577
describe('enums merging', () => {
6-
test('given incompatible, should mark enum as empty', () => {
78+
test('given incompatible, should leave empty', () => {
779
const schema: JSONSchema4 = {
880
allOf: [
981
{
@@ -38,7 +110,7 @@ describe('mergeAllOf util', () => {
38110
});
39111
});
40112

41-
test('given incompatible, should merge normally', () => {
113+
test('given compatible, should merge normally', () => {
42114
const schema: JSONSchema4 = {
43115
allOf: [
44116
{

src/utils/mergeAllOf.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ function _mergeAllOf(schema: JSONSchema4, path: JsonPath, resolveRef: WalkerRefR
1111
return resolveAllOf(cloneDeep(schema), {
1212
deep: false,
1313
resolvers: {
14-
defaultResolver(values: any) {
15-
return Object.assign({}, ...values);
14+
defaultResolver(values: unknown) {
15+
if (Array.isArray(values)) {
16+
return values;
17+
}
18+
19+
return Object.assign({}, ...Object(values));
20+
},
21+
example(values: unknown[]) {
22+
return resolveAllOf.options.resolvers.enum(values) || null;
1623
},
1724
enum(values: unknown[]) {
1825
return resolveAllOf.options.resolvers.enum(compact(values)) || [];

0 commit comments

Comments
 (0)