Skip to content

Commit 3b7b3d1

Browse files
committed
fix: handler combiners with type
1 parent 8b284ed commit 3b7b3d1

File tree

6 files changed

+284
-3
lines changed

6 files changed

+284
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"title": "Todo Full",
3+
"allOf": [
4+
{
5+
"properties": {
6+
"test": {
7+
"type": "string"
8+
}
9+
}
10+
},
11+
{
12+
"properties": {
13+
"id": {
14+
"type": "integer",
15+
"minimum": 0,
16+
"maximum": 1000000
17+
},
18+
"completed_at": {
19+
"type": [
20+
"string",
21+
"null"
22+
],
23+
"format": "date-time"
24+
},
25+
"created_at": {
26+
"type": "string",
27+
"format": "date-time"
28+
},
29+
"updated_at": {
30+
"type": "string",
31+
"format": "date-time"
32+
}
33+
},
34+
"required": [
35+
"id"
36+
]
37+
}
38+
],
39+
"type": "object"
40+
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface ICombinerNode {
2424
readonly combiner: JSONSchema4CombinerName;
2525
properties?: JSONSchema4[];
2626
annotations: Pick<JSONSchema4, JSONSchema4Annotations>;
27+
readonly type?: JSONSchema4TypeName | JSONSchema4TypeName[];
2728
}
2829

2930
export interface IBaseNode extends Pick<JSONSchema4, 'enum'> {

src/utils/__tests__/__snapshots__/renderSchema.spec.ts.snap

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,239 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`renderSchema util should match combiner-schema.json 1`] = `
4+
Array [
5+
Object {
6+
"canHaveChildren": true,
7+
"id": "random-id",
8+
"level": 0,
9+
"metadata": Object {
10+
"annotations": Object {
11+
"title": "Todo Full",
12+
},
13+
"combiner": "allOf",
14+
"id": "random-id",
15+
"path": Array [],
16+
"properties": Array [
17+
Object {
18+
"properties": Object {
19+
"test": Object {
20+
"type": "string",
21+
},
22+
},
23+
"type": "object",
24+
},
25+
Object {
26+
"properties": Object {
27+
"completed_at": Object {
28+
"format": "date-time",
29+
"type": Array [
30+
"string",
31+
"null",
32+
],
33+
},
34+
"created_at": Object {
35+
"format": "date-time",
36+
"type": "string",
37+
},
38+
"id": Object {
39+
"maximum": 1000000,
40+
"minimum": 0,
41+
"type": "integer",
42+
},
43+
"updated_at": Object {
44+
"format": "date-time",
45+
"type": "string",
46+
},
47+
},
48+
"required": Array [
49+
"id",
50+
],
51+
"type": "object",
52+
},
53+
],
54+
"type": "object",
55+
},
56+
"name": "",
57+
},
58+
Object {
59+
"canHaveChildren": true,
60+
"id": "random-id",
61+
"level": 1,
62+
"metadata": Object {
63+
"additionalProperties": undefined,
64+
"annotations": Object {},
65+
"enum": undefined,
66+
"id": "random-id",
67+
"path": Array [
68+
"properties",
69+
0,
70+
],
71+
"patternProperties": undefined,
72+
"properties": Object {
73+
"test": Object {
74+
"type": "string",
75+
},
76+
},
77+
"showDivider": false,
78+
"type": "object",
79+
"validations": Object {},
80+
},
81+
"name": "",
82+
},
83+
Object {
84+
"id": "random-id",
85+
"level": 2,
86+
"metadata": Object {
87+
"annotations": Object {},
88+
"enum": undefined,
89+
"id": "random-id",
90+
"name": "test",
91+
"path": Array [
92+
"properties",
93+
0,
94+
"properties",
95+
"test",
96+
],
97+
"required": false,
98+
"type": "string",
99+
"validations": Object {},
100+
},
101+
"name": "",
102+
},
103+
Object {
104+
"canHaveChildren": true,
105+
"id": "random-id",
106+
"level": 1,
107+
"metadata": Object {
108+
"additionalProperties": undefined,
109+
"annotations": Object {},
110+
"enum": undefined,
111+
"id": "random-id",
112+
"path": Array [
113+
"properties",
114+
1,
115+
],
116+
"patternProperties": undefined,
117+
"properties": Object {
118+
"completed_at": Object {
119+
"format": "date-time",
120+
"type": Array [
121+
"string",
122+
"null",
123+
],
124+
},
125+
"created_at": Object {
126+
"format": "date-time",
127+
"type": "string",
128+
},
129+
"id": Object {
130+
"maximum": 1000000,
131+
"minimum": 0,
132+
"type": "integer",
133+
},
134+
"updated_at": Object {
135+
"format": "date-time",
136+
"type": "string",
137+
},
138+
},
139+
"showDivider": false,
140+
"type": "object",
141+
"validations": Object {},
142+
},
143+
"name": "",
144+
},
145+
Object {
146+
"id": "random-id",
147+
"level": 2,
148+
"metadata": Object {
149+
"annotations": Object {},
150+
"enum": undefined,
151+
"id": "random-id",
152+
"name": "id",
153+
"path": Array [
154+
"properties",
155+
1,
156+
"properties",
157+
"id",
158+
],
159+
"required": true,
160+
"type": "integer",
161+
"validations": Object {},
162+
},
163+
"name": "",
164+
},
165+
Object {
166+
"id": "random-id",
167+
"level": 2,
168+
"metadata": Object {
169+
"annotations": Object {},
170+
"enum": undefined,
171+
"id": "random-id",
172+
"name": "completed_at",
173+
"path": Array [
174+
"properties",
175+
1,
176+
"properties",
177+
"completed_at",
178+
],
179+
"required": false,
180+
"type": Array [
181+
"string",
182+
"null",
183+
],
184+
"validations": Object {
185+
"format": "date-time",
186+
},
187+
},
188+
"name": "",
189+
},
190+
Object {
191+
"id": "random-id",
192+
"level": 2,
193+
"metadata": Object {
194+
"annotations": Object {},
195+
"enum": undefined,
196+
"id": "random-id",
197+
"name": "created_at",
198+
"path": Array [
199+
"properties",
200+
1,
201+
"properties",
202+
"created_at",
203+
],
204+
"required": false,
205+
"type": "string",
206+
"validations": Object {
207+
"format": "date-time",
208+
},
209+
},
210+
"name": "",
211+
},
212+
Object {
213+
"id": "random-id",
214+
"level": 2,
215+
"metadata": Object {
216+
"annotations": Object {},
217+
"enum": undefined,
218+
"id": "random-id",
219+
"name": "updated_at",
220+
"path": Array [
221+
"properties",
222+
1,
223+
"properties",
224+
"updated_at",
225+
],
226+
"required": false,
227+
"type": "string",
228+
"validations": Object {
229+
"format": "date-time",
230+
},
231+
},
232+
"name": "",
233+
},
234+
]
235+
`;
236+
3237
exports[`renderSchema util should match default-schema.json 1`] = `
4238
Array [
5239
Object {

src/utils/__tests__/renderSchema.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jest.mock('../assignId', () => ({
99
}));
1010

1111
describe('renderSchema util', () => {
12-
it.each([['default-schema.json', ''], ['ref/original.json', 'ref/resolved.json']])(
12+
it.each([['default-schema.json', ''], ['ref/original.json', 'ref/resolved.json'], ['combiner-schema.json', '']])(
1313
'should match %s',
1414
(schema, dereferenced) => {
1515
expect(

src/utils/renderSchema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export const renderSchema: Walker = function*(schema, dereferencedSchema, level
8080
if (node.properties !== undefined) {
8181
const isConditionalCombiner = node.combiner === 'anyOf' || node.combiner === 'oneOf';
8282
for (const [i, property] of node.properties.entries()) {
83+
if ('type' in node) {
84+
property.type = node.type;
85+
}
86+
8387
yield* renderSchema(property, dereferencedSchema, level + 1, {
8488
showDivider: isConditionalCombiner && i !== 0,
8589
path: [...path, 'properties', i],

src/utils/walk.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ function assignNodeSpecificFields(base: IBaseNode, node: JSONSchema4) {
3434
}
3535

3636
function processNode(node: JSONSchema4): SchemaNode | void {
37-
if (node.type !== undefined) {
37+
const combiner = getCombiner(node);
38+
39+
if (node.type !== undefined && combiner === undefined) {
3840
const base: IBaseNode = {
3941
id: assignId(node),
4042
type: node.type,
@@ -74,13 +76,13 @@ function processNode(node: JSONSchema4): SchemaNode | void {
7476
} as IRefNode;
7577
}
7678

77-
const combiner = getCombiner(node);
7879
if (combiner !== undefined) {
7980
return {
8081
id: assignId(node),
8182
combiner,
8283
properties: node[combiner],
8384
annotations: getAnnotations(node),
85+
...(node.type !== undefined && { type: node.type }),
8486
} as ICombinerNode;
8587
}
8688

0 commit comments

Comments
 (0)