ajv-validator / ajv

The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
https://ajv.js.org
MIT License
13.81k stars 875 forks source link

Error using stringify on schema with uri-encoded definition property #2496

Open JonathanMonroeU opened 3 weeks ago

JonathanMonroeU commented 3 weeks ago

What version of Ajv are you using? Does the issue happen if you use the latest version? I'm using the latest version.

I'm getting "can't resolve reference" error when having a schema with a definition property using a uri-encoding like '%3C' or '%22'. I started seeing this while using a schema with the fields oneOf, allOf or anyOf inside the definition.

The following code, based on a template provided in this repo, was used to debug the error.

const Ajv = require("ajv")
const ajv = new Ajv({allErrors: true})

  const schema = {
    title: 'object with $ref',
    definitions: {
      'Some%3Cloremipsum3E': {
          additionalProperties: {
        type: 'string',
          },
          type: 'object'
      }
    },
    type: 'object',
    properties: {
      obj: {
        $ref: '#/definitions/Some%3Cloremipsum3E'
      }
    }
  }

  const object = {
    obj: {
      str: 'test'
    }
  }
const validate = ajv.compile(schema)

test(object)

function test(data) {
  const valid = validate(data)
  if (valid) console.log("Valid!")
  else console.log("Invalid: " + ajv.errorsText(validate.errors))
}

Validation result, data AFTER validation, error messages

MissingRefError: can't resolve reference #/definitions/Some%3Cloremipsum3E from id #
    at Object.code (./node_modules/ajv/dist/vocabularies/core/ref.js:43:19)
    at keywordCode (./node_modules/ajv/dist/compile/validate/index.js:480:13)
    at ./node_modules/ajv/dist/compile/validate/index.js:193:25
    at CodeGen.code (./node_modules/ajv/dist/compile/codegen/index.js:440:13)
    at CodeGen.block (./node_modules/ajv/dist/compile/codegen/index.js:570:18)
    at schemaKeywords (./node_modules/ajv/dist/compile/validate/index.js:193:13)
    at typeAndKeywords (./node_modules/ajv/dist/compile/validate/index.js:131:16)
    at subSchemaObjCode (./node_modules/ajv/dist/compile/validate/index.js:117:5)
    at subschemaCode (./node_modules/ajv/dist/compile/validate/index.js:92:13)
    at KeywordCxt.subschema (./node_modules/ajv/dist/compile/validate/index.js:448:9) {
  missingRef: '#/definitions/Some%3Cloremipsum3E',
  missingSchema: ''
}

The error is thrown in the line if (schOrEnv === undefined) throw new MissingRefError(it.opts.uriResolver, baseId, $ref). The strucutre up to "it" seems to be as it should, but the props passed to derived from it "resolveRef.call" seem to be missing definitions, turning schOrEnv to undefined and leading to the error.

const def: CodeKeywordDefinition = {
  keyword: "$ref",
  schemaType: "string",
  code(cxt: KeywordCxt): void {
    const {gen, schema: $ref, it} = cxt
    const {baseId, schemaEnv: env, validateName, opts, self} = it
    const {root} = env
    if (($ref === "#" || $ref === "#/") && baseId === root.baseId) return callRootRef()
    const schOrEnv = resolveRef.call(self, root, baseId, $ref)
    if (schOrEnv === undefined) throw new MissingRefError(it.opts.uriResolver, baseId, $ref)
jasoniangreen commented 3 weeks ago

So I think the issue is that the definition needs to be defined with the decoded version like so:

definitions: {
  'Some<loremipsum3E': {
      additionalProperties: {
type: 'string',
      },
      type: 'object'
  }
},

The worked for me with your example. The reason is because the when we use fast-uri to resolve the uri in the ref, it does that conversion, so it's not looking for the encoded version, but the def how it should be.

Does that make sense?

JonathanMonroeU commented 2 weeks ago

The reason is because the when we use fast-uri to resolve the uri in the ref, it does that conversion, so it's not looking for the encoded version, but the def how it should be.

Yeah, it does make sense. But what if I'm doing the encoding precisely to avoid these symbols to go on the definitions and ref string and possibly cause any issues? I'm replacing " with %22 and ' with %27, for example.