Skip to content

Commit 7162f3c

Browse files
authored
fix: ajv is not defined when oneOf/allOf in external schema (#310)
1 parent 7f7b954 commit 7162f3c

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ function build (schema, options) {
132132
case 'array':
133133
main = '$main'
134134
code = buildArray(location, code, main)
135+
schema = location.schema
135136
break
136137
case undefined:
137138
main = '$asAny'
@@ -980,7 +981,8 @@ function buildArray (location, code, name, key = null) {
980981

981982
if (schema.items.$ref) {
982983
if (!schema[fjsCloned]) {
983-
schema = clone(location.schema)
984+
location.schema = clone(location.schema)
985+
schema = location.schema
984986
schema[fjsCloned] = true
985987
}
986988
location = refFinder(schema.items.$ref, location)

test/ref.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,3 +1202,50 @@ test('Bad key', t => {
12021202

12031203
t.end()
12041204
})
1205+
1206+
test('Regression 2.5.2', t => {
1207+
t.plan(1)
1208+
1209+
const externalSchema = {
1210+
'/models/Bar': {
1211+
$id: '/models/Bar',
1212+
$schema: 'http://json-schema.org/schema#',
1213+
definitions: {
1214+
entity: {
1215+
type: 'object',
1216+
properties: { field: { type: 'string' } }
1217+
}
1218+
}
1219+
},
1220+
'/models/Foo': {
1221+
$id: '/models/Foo',
1222+
$schema: 'http://json-schema.org/schema#',
1223+
definitions: {
1224+
entity: {
1225+
type: 'object',
1226+
properties: {
1227+
field: { type: 'string' },
1228+
sub: {
1229+
oneOf: [
1230+
{ $ref: '/models/Bar#/definitions/entity' },
1231+
{ type: 'null' }
1232+
]
1233+
}
1234+
}
1235+
}
1236+
}
1237+
}
1238+
}
1239+
1240+
const schema = {
1241+
type: 'array',
1242+
items: {
1243+
$ref: '/models/Foo#/definitions/entity'
1244+
}
1245+
}
1246+
1247+
const stringify = build(schema, { schema: externalSchema })
1248+
const output = stringify([{ field: 'parent', sub: { field: 'joined' } }])
1249+
1250+
t.equal(output, '[{"field":"parent","sub":{"field":"joined"}}]')
1251+
})

0 commit comments

Comments
 (0)