Skip to content

Commit 831cb1c

Browse files
committed
Merge branch 'refResolveBaseURI' of https://github.com/rbuckton/ajv into rbuckton-refResolveBaseURI
2 parents 7419ec3 + ae5c1fe commit 831cb1c

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

lib/compile/resolve.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ export function resolveUrl(baseId: string, id: string): string {
8989

9090
const ANCHOR = /^[a-z_][-a-z0-9._]*$/i
9191

92-
export function getSchemaRefs(this: Ajv, schema: AnySchema): LocalRefs {
92+
export function getSchemaRefs(this: Ajv, schema: AnySchema, baseId: string): LocalRefs {
9393
if (typeof schema == "boolean") return {}
9494
const {schemaId} = this.opts
95-
const schId = normalizeId(schema[schemaId])
95+
const schId = normalizeId(schema[schemaId] || baseId)
9696
const baseIds: {[JsonPtr in string]?: string} = {"": schId}
9797
const pathPrefix = getFullPath(schId, false)
9898
const localRefs: LocalRefs = {}

lib/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,8 @@ export default class Ajv {
694694
let sch = this._cache.get(schema)
695695
if (sch !== undefined) return sch
696696

697-
const localRefs = getSchemaRefs.call(this, schema)
698697
baseId = normalizeId(id || baseId)
698+
const localRefs = getSchemaRefs.call(this, schema, baseId)
699699
sch = new SchemaEnv({schema, schemaId, meta, baseId, localRefs})
700700
this._cache.set(sch.schema, sch)
701701
if (addSchema && !baseId.startsWith("#")) {

spec/resolve.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,29 @@ describe("resolve", () => {
6868
})
6969
})
7070

71+
it("should resolve fragment $id in schema refs when root $id not present", () => {
72+
const schema = {
73+
"$schema": "http://json-schema.org/draft-07/schema#",
74+
"definitions": {
75+
"SeeAlso": { "$id": "#SeeAlso", "type": "number" },
76+
"Engine": {
77+
"$id": "#Engine",
78+
"type": "object",
79+
"properties": {
80+
"see_also": { "$ref": "#SeeAlso" }
81+
}
82+
}
83+
}
84+
}
85+
86+
instances.forEach((ajv) => {
87+
ajv.addSchema(schema, "yaml.json")
88+
const data = { see_also: 1 }
89+
const validate = ajv.validate("yaml.json#/definitions/Engine", data)
90+
validate.should.equal(true)
91+
})
92+
})
93+
7194
it("should throw if the same id resolves to two different schemas", () => {
7295
instances.forEach((ajv) => {
7396
ajv.compile({

0 commit comments

Comments
 (0)