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

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

Open
JonathanMonroeU opened this issue Sep 27, 2024 · 2 comments
Open

Comments

@JonathanMonroeU
Copy link

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
Copy link
Collaborator

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants