diff --git a/lib/compile/resolve.ts b/lib/compile/resolve.ts index 4360eab06..0cc742317 100644 --- a/lib/compile/resolve.ts +++ b/lib/compile/resolve.ts @@ -112,6 +112,7 @@ export function getSchemaRefs(this: Ajv, schema: AnySchema, baseId: string): Loc // 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] diff --git a/spec/resolve.spec.ts b/spec/resolve.spec.ts index 2fe5b1041..99e57c177 100644 --- a/spec/resolve.spec.ts +++ b/spec/resolve.spec.ts @@ -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 = {