handling of $dynamicRef on openApi v3.1 spec schema
See original GitHub issueWhat version of Ajv are you using? Does the issue happen if you use the latest version? 8.2.0 Ajv options object
{ strict:false }
JSON Schema
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"in": {
"enum": [
"query",
"header",
"path",
"cookie"
]
},
"required": {
"default": false,
"type": "boolean"
},
"schema": {
"$dynamicRef": "#meta"
},
"content": {
"type": "object"
}
},
"required": [
"in"
],
"oneOf": [
{
"required": [
"schema"
]
},
{
"required": [
"content"
]
}
],
"$defs": {
"schema": {
"$dynamicAnchor": "meta",
"type": [
"object",
"boolean"
]
}
}
}
Sample data
{
"name": "limit",
"in": "query",
"description": "How many items to return at one time (max 100)",
"required": false,
"schema": {
"type": "integer",
"format": "int32"
}
}
Your code
const Ajv = require("ajv/dist/2020.js")
const ajv = new Ajv(options)
const validate = ajv.compile(schema);
console.log(validate(data));
console.log(validate.errors);
https://runkit.com/seriousme/ajv-dynamicref
Validation result, data AFTER validation, error messages
false
[
{
instancePath: '/schema',
schemaPath: '#/oneOf/0/required',
keyword: 'required',
params: { missingProperty: 'schema' },
message: "must have required property 'schema'"
},
{
instancePath: '/schema',
schemaPath: '#/oneOf/1/required',
keyword: 'required',
params: { missingProperty: 'content' },
message: "must have required property 'content'"
},
{
instancePath: '/schema',
schemaPath: '#/oneOf',
keyword: 'oneOf',
params: { passingSchemas: null },
message: 'must match exactly one schema in oneOf'
}
]
What results did you expect? I expected the validation to succeed. https://json-schema.hyperjump.io/ says the data matches the schema.
The schema is part of the OpenApi 3.1 schema (which is not under my control)
The data is part of the well known Petstore example
( I created a v3.1 Petstore example by taking the v3.0 Petstore example, updating the openapi
version to “3.1.0” and adding a licence URL)
Validation of the full v3.1 Petstore example against the full 3.1 schema works with:
Which leads me to think that the issue is not in the schema but in Ajv 😉
I also tried to resolve the issue by tweaking the schema:
- Replacing
"$dynamicRef": "#meta"
by"type": [ "object", "boolean"]
solves the issue. - Replacing
"$dynamicRef": "#meta"
by"$ref": "#/$defs/schema"
solves the issue as well. (when applied to the full OpenApi 3.1 schema this lets Ajv validate the full Petstore example as well) - Removing
/$defs/schema
from the schema still gives the same error.
Hence my suspicion that the problem is caused by the handling of $dynamicRef
by Ajv
Are you going to resolve the issue? I’m happy to help but I think it requires some quite deep knowledge of Ajv internals.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
Sorry, didn’t come around to improving it yet. Is there still an interest in this case being supported?
Will investigate. $dynamicRef is quite limited indeed, it might be unsupported case…