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 #2427 compileAsync a schema with discriminator and $ref #2429

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
5 changes: 4 additions & 1 deletion lib/vocabularies/discriminator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {KeywordCxt} from "../../compile/validate"
import {_, getProperty, Name} from "../../compile/codegen"
import {DiscrError, DiscrErrorObj} from "../discriminator/types"
import {resolveRef, SchemaEnv} from "../../compile"
import MissingRefError from "../../compile/ref_error"
import {schemaHasRulesButRef} from "../../compile/util"

export type DiscriminatorError = DiscrErrorObj<DiscrError.Tag> | DiscrErrorObj<DiscrError.Mapping>
Expand Down Expand Up @@ -66,8 +67,10 @@ const def: CodeKeywordDefinition = {
for (let i = 0; i < oneOf.length; i++) {
let sch = oneOf[i]
if (sch?.$ref && !schemaHasRulesButRef(sch, it.self.RULES)) {
sch = resolveRef.call(it.self, it.schemaEnv.root, it.baseId, sch?.$ref)
const ref = sch.$ref
sch = resolveRef.call(it.self, it.schemaEnv.root, it.baseId, ref)
if (sch instanceof SchemaEnv) sch = sch.schema
if (sch === undefined) throw new MissingRefError(it.opts.uriResolver, it.baseId, ref)
}
const propSch = sch?.properties?.[tagName]
if (typeof propSch != "object") {
Expand Down
61 changes: 61 additions & 0 deletions spec/discriminator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,67 @@ describe("discriminator keyword", function () {
})
})

describe("schema with external $refs", () => {
const schemas = {
main: {
type: "object",
discriminator: {propertyName: "foo"},
required: ["foo"],
oneOf: [
{
$ref: "schema1",
},
{
$ref: "schema2",
},
],
},
schema1: {
type: "object",
properties: {
foo: {const: "x"},
},
},
schema2: {
type: "object",
properties: {
foo: {enum: ["y", "z"]},
},
},
}

const data = {foo: "x"}
const badData = {foo: "w"}

it("compile should resolve each $ref to a schema that was added with addSchema", () => {
const opts = {
discriminator: true,
}
const ajv = new _Ajv(opts)
ajv.addSchema(schemas.main, "https://host/main")
ajv.addSchema(schemas.schema1, "https://host/schema1")
ajv.addSchema(schemas.schema2, "https://host/schema2")

const validate = ajv.compile({$ref: "https://host/main"})
assert.strictEqual(validate(data), true)
assert.strictEqual(validate(badData), false)
})
it("compileAsync should loadSchema each $ref", async () => {
const opts = {
discriminator: true,
loadSchema(url) {
if (!url.startsWith("https://host/")) return undefined
const name = url.substring("https://host/".length)
return schemas[name]
},
}
const ajv = new _Ajv(opts)
const validate = await ajv.compileAsync({$ref: "https://host/main"})
assert.strictEqual(validate(data), true)
assert.strictEqual(validate(badData), false)
})
})

describe("validation with deeply referenced schemas", () => {
const schema = [
{
Expand Down
Loading