Skip to content

Commit

Permalink
Merge branch 'rbuckton-refResolveBaseURI'
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Sep 12, 2021
2 parents 7419ec3 + d9bc534 commit 9a9656f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/compile/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ export function resolveUrl(baseId: string, id: string): string {

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

export function getSchemaRefs(this: Ajv, schema: AnySchema): LocalRefs {
export function getSchemaRefs(this: Ajv, schema: AnySchema, baseId: string): LocalRefs {
if (typeof schema == "boolean") return {}
const {schemaId} = this.opts
const schId = normalizeId(schema[schemaId])
const schId = normalizeId(schema[schemaId] || baseId)
const baseIds: {[JsonPtr in string]?: string} = {"": schId}
const pathPrefix = getFullPath(schId, false)
const localRefs: LocalRefs = {}
Expand Down
2 changes: 1 addition & 1 deletion lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,8 @@ export default class Ajv {
let sch = this._cache.get(schema)
if (sch !== undefined) return sch

const localRefs = getSchemaRefs.call(this, schema)
baseId = normalizeId(id || baseId)
const localRefs = getSchemaRefs.call(this, schema, baseId)
sch = new SchemaEnv({schema, schemaId, meta, baseId, localRefs})
this._cache.set(sch.schema, sch)
if (addSchema && !baseId.startsWith("#")) {
Expand Down
23 changes: 23 additions & 0 deletions spec/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,29 @@ describe("resolve", () => {
})
})

it("should resolve fragment $id in schema refs when root $id not present", () => {
const schema = {
$schema: "http://json-schema.org/draft-07/schema#",
definitions: {
SeeAlso: {$id: "#SeeAlso", type: "number"},
Engine: {
$id: "#Engine",
type: "object",
properties: {
see_also: {$ref: "#SeeAlso"},
},
},
},
}

instances.forEach((ajv) => {
ajv.addSchema(schema, "yaml.json")
const data = {see_also: 1}
const validate = ajv.validate("yaml.json#/definitions/Engine", data)
validate.should.equal(true)
})
})

it("should throw if the same id resolves to two different schemas", () => {
instances.forEach((ajv) => {
ajv.compile({
Expand Down

0 comments on commit 9a9656f

Please sign in to comment.