Skip to content

Commit edf1a87

Browse files
authored
fix: reference after merge allOf (#480)
1 parent 6bcef0e commit edf1a87

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ function mergeAllOfSchema (location, schema, mergedSchema) {
489489
}
490490
}
491491
delete mergedSchema.allOf
492+
493+
mergedSchema.$id = `merged_${randomUUID()}`
494+
ajvInstance.addSchema(mergedSchema)
495+
location.schemaId = mergedSchema.$id
492496
}
493497

494498
function buildInnerObject (location) {

test/issue-479.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const build = require('..')
5+
6+
test('should validate anyOf after allOf merge', (t) => {
7+
t.plan(1)
8+
9+
const schema = {
10+
$id: 'schema',
11+
type: 'object',
12+
allOf: [
13+
{
14+
$id: 'base',
15+
type: 'object',
16+
properties: {
17+
name: {
18+
type: 'string'
19+
}
20+
},
21+
required: [
22+
'name'
23+
]
24+
},
25+
{
26+
$id: 'inner_schema',
27+
type: 'object',
28+
properties: {
29+
union: {
30+
$id: '#id',
31+
anyOf: [
32+
{
33+
34+
$id: 'guid',
35+
type: 'string'
36+
},
37+
{
38+
39+
$id: 'email',
40+
type: 'string'
41+
}
42+
]
43+
}
44+
},
45+
required: [
46+
'union'
47+
]
48+
}
49+
]
50+
}
51+
52+
const stringify = build(schema)
53+
54+
t.equal(
55+
stringify({ name: 'foo', union: 'a8f1cc50-5530-5c62-9109-5ba9589a6ae1' }),
56+
'{"name":"foo","union":"a8f1cc50-5530-5c62-9109-5ba9589a6ae1"}')
57+
})

0 commit comments

Comments
 (0)