Skip to content

Commit 559cbf1

Browse files
authored
Support $ref in allOf (#224)
* Support $ref in allOf Closes #223 * Test external $refs in allOf
1 parent c4fbf83 commit 559cbf1

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ function sanitizeKey (key) {
627627
}
628628

629629
function buildCode (schema, code, laterCode, name, externalSchema, fullSchema) {
630+
if (schema.$ref) {
631+
schema = refFinder(schema.$ref, fullSchema, externalSchema)
632+
}
633+
630634
Object.keys(schema.properties || {}).forEach((key, i, a) => {
631635
if (schema.properties[key].$ref) {
632636
// if the schema object is deep in the tree, we must resolve the ref in the parent scope

test/allof.test.js

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,200 @@ test('object with nested allOfs', (t) => {
184184
t.fail()
185185
}
186186
})
187+
188+
test('object with $ref in allOf', (t) => {
189+
t.plan(1)
190+
191+
const schema = {
192+
title: 'object with $ref in allOf',
193+
type: 'object',
194+
definitions: {
195+
id1: {
196+
type: 'object',
197+
properties: {
198+
id1: {
199+
type: 'integer'
200+
}
201+
}
202+
}
203+
},
204+
allOf: [
205+
{
206+
$ref: '#/definitions/id1'
207+
}
208+
]
209+
}
210+
211+
try {
212+
const stringify = build(schema)
213+
try {
214+
const value = stringify({
215+
id1: 1,
216+
id2: 2 // extra prop shouldn't be in result
217+
})
218+
t.is(value, '{"id1":1}')
219+
} catch (e) {
220+
t.fail()
221+
}
222+
} catch (e) {
223+
t.fail()
224+
}
225+
})
226+
227+
test('object with $ref and other object in allOf', (t) => {
228+
t.plan(1)
229+
230+
const schema = {
231+
title: 'object with $ref in allOf',
232+
type: 'object',
233+
definitions: {
234+
id1: {
235+
type: 'object',
236+
properties: {
237+
id1: {
238+
type: 'integer'
239+
}
240+
}
241+
}
242+
},
243+
allOf: [
244+
{
245+
$ref: '#/definitions/id1'
246+
},
247+
{
248+
type: 'object',
249+
properties: {
250+
id2: {
251+
type: 'integer'
252+
}
253+
}
254+
}
255+
]
256+
}
257+
258+
try {
259+
const stringify = build(schema)
260+
try {
261+
const value = stringify({
262+
id1: 1,
263+
id2: 2,
264+
id3: 3 // extra prop shouldn't be in result
265+
})
266+
t.is(value, '{"id1":1,"id2":2}')
267+
} catch (e) {
268+
t.fail()
269+
}
270+
} catch (e) {
271+
t.fail()
272+
}
273+
})
274+
275+
test('object with multiple $refs in allOf', (t) => {
276+
t.plan(1)
277+
278+
const schema = {
279+
title: 'object with $ref in allOf',
280+
type: 'object',
281+
definitions: {
282+
id1: {
283+
type: 'object',
284+
properties: {
285+
id1: {
286+
type: 'integer'
287+
}
288+
}
289+
},
290+
id2: {
291+
type: 'object',
292+
properties: {
293+
id2: {
294+
type: 'integer'
295+
}
296+
}
297+
}
298+
},
299+
allOf: [
300+
{
301+
$ref: '#/definitions/id1'
302+
},
303+
{
304+
$ref: '#/definitions/id2'
305+
}
306+
]
307+
}
308+
309+
try {
310+
const stringify = build(schema)
311+
try {
312+
const value = stringify({
313+
id1: 1,
314+
id2: 2,
315+
id3: 3 // extra prop shouldn't be in result
316+
})
317+
t.is(value, '{"id1":1,"id2":2}')
318+
} catch (e) {
319+
t.fail()
320+
}
321+
} catch (e) {
322+
t.fail()
323+
}
324+
})
325+
326+
test('object with external $refs in allOf', (t) => {
327+
t.plan(1)
328+
329+
const externalSchema = {
330+
first: {
331+
definitions: {
332+
id1: {
333+
type: 'object',
334+
properties: {
335+
id1: {
336+
type: 'integer'
337+
}
338+
}
339+
}
340+
}
341+
},
342+
second: {
343+
id2: {
344+
$id: '#id2',
345+
type: 'object',
346+
properties: {
347+
id2: {
348+
type: 'integer'
349+
}
350+
}
351+
}
352+
}
353+
}
354+
355+
const schema = {
356+
title: 'object with $ref in allOf',
357+
type: 'object',
358+
allOf: [
359+
{
360+
$ref: 'first#/definitions/id1'
361+
},
362+
{
363+
$ref: 'second#id2'
364+
}
365+
]
366+
}
367+
368+
try {
369+
const stringify = build(schema, { schema: externalSchema })
370+
try {
371+
const value = stringify({
372+
id1: 1,
373+
id2: 2,
374+
id3: 3 // extra prop shouldn't be in result
375+
})
376+
t.is(value, '{"id1":1,"id2":2}')
377+
} catch (e) {
378+
t.fail()
379+
}
380+
} catch (e) {
381+
t.fail()
382+
}
383+
})

0 commit comments

Comments
 (0)