Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#1558): Discrepancy in addUsedSchema between v6 and v8 #2336

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/compile/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
traverse(schema, {allKeys: true}, (sch, jsonPtr, _, parentJsonPtr) => {
if (parentJsonPtr === undefined) return
const fullPath = pathPrefix + jsonPtr
let baseId = baseIds[parentJsonPtr]

Check warning on line 105 in lib/compile/resolve.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

'baseId' is already declared in the upper scope on line 93 column 61

Check warning on line 105 in lib/compile/resolve.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'baseId' is already declared in the upper scope on line 93 column 61

Check warning on line 105 in lib/compile/resolve.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'baseId' is already declared in the upper scope on line 93 column 61
if (typeof sch[schemaId] == "string") baseId = addRef.call(this, sch[schemaId])
addAnchor.call(this, sch.$anchor)
addAnchor.call(this, sch.$dynamicAnchor)
Expand All @@ -112,6 +112,7 @@
// eslint-disable-next-line @typescript-eslint/unbound-method
const _resolve = this.opts.uriResolver.resolve
ref = normalizeId(baseId ? _resolve(baseId, ref) : ref)
if (this.opts.addUsedSchema === false) return ref // skip adding schema reference
if (schemaRefs.has(ref)) throw ambiguos(ref)
schemaRefs.add(ref)
let schOrRef = this.refs[ref]
Expand Down
31 changes: 31 additions & 0 deletions spec/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,37 @@ uriResolvers.forEach((resolver) => {
}, /resolves to more than one schema/)
})
})
it("should not throw if the same id resolves to two different schemas when addUsedSchema is false", () => {
const ajvInstance = new _Ajv({
allErrors: true,
verbose: true,
inlineRefs: false,
allowUnionTypes: true,
uriResolver: resolver,
addUsedSchema: false,
})

ajvInstance.compile({
$id: "http://example.com/1.json",
type: "integer",
})

let errorOccurred = false
try {
ajvInstance.compile({
type: "object",
additionalProperties: {
$id: "http://example.com/1.json",
type: "string",
},
})
} catch (error) {
errorOccurred = true
}

// Assert that no error occurred
errorOccurred.should.equal(false)
})

it("should resolve ids defined as urn's (issue #423)", () => {
const schema = {
Expand Down
Loading