question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

handling of $dynamicRef on openApi v3.1 spec schema

See original GitHub issue

What 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:open
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
epoberezkincommented, Nov 13, 2022

Sorry, didn’t come around to improving it yet. Is there still an interest in this case being supported?

1reaction
epoberezkincommented, Apr 28, 2021

Will investigate. $dynamicRef is quite limited indeed, it might be unsupported case…

Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenAPI Specification - Version 3.0.3 - Swagger
The OpenAPI Specification defines a standard interface to RESTful APIs which ... Note this behavior is different than the Schema Object's treatment of ......
Read more >
OpenAPI Specification v3.1.0 | Introduction, Definitions, & More
The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for HTTP APIs.
Read more >
jsonschema 4.17.3 documentation
jsonschema is an implementation of the JSON Schema specification for Python. ... except for dynamicRef / recursiveRef and $vocabulary (in-progress).
Read more >
draft 2020-12 - Ajv JSON schema validator
The data array to be valid should not have more (less) items than the keyword value. Example. schema: {type: "array", maxItems: 3}. valid:...
Read more >
Validating OpenAPI and JSON Schema
1 const OasSchema = require("@hyperjump/oas-schema-validator"); 2const example = require("./example.openapi.json"); 3 4(async function () { 5 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found