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

Using discriminator in a definition causes failure to compile if oneOf contains a reference #1899

Closed
mshick opened this issue Feb 14, 2022 · 3 comments · Fixed by #1901
Closed

Comments

@mshick
Copy link

mshick commented Feb 14, 2022

What version of Ajv are you using? Does the issue happen if you use the latest version?

8.10.0

Ajv options object

{ discriminator: true }

JSON Schema

{
  "type": "object",
  "properties": {
    "container": {
      "$ref": "#/definitions/Container"
    }
  },
  "definitions": {
    "Block": {
      "type": "object",
      "properties": {
        "_type": {
          "type": "string",
          "enum": ["foobar"]
        }
      },
      "required": ["_type"],
      "title": "Block"
    },
    "Container": {
      "type": "object",
      "properties": {
        "object": {
          "type": "object",
          "oneOf": [
            {
              "$ref": "#/definitions/Block"
            }
          ],
          "discriminator": {
            "propertyName": "_type"
          },
          "required": ["_type"]
        },
        "array": {
          "type": "array",
          "items": {
            "oneOf": [
              {
                "$ref": "#/definitions/Block"
              }
            ],
            "discriminator": {
              "propertyName": "_type"
            },
            "required": ["_type"]
          }
        }
      }
    }
  }
}

Sample data

n/a

Your code

var ajv = new Ajv(options);
var validate = ajv.compile(schema);

Validation result, data AFTER validation, error messages

Error: discriminator: oneOf subschemas (or referenced schemas) must have "properties/_type"

What results did you expect?

The schema to compile.

Are you going to resolve the issue?

Not immediately.

Reproduction

https://runkit.com/mshick/620a919455b3e30009ef1f50

@epoberezkin
Copy link
Member

Try using full references based on schema $id (like https://example.com/schema#/definitions/Block) - would it work like that?

@mshick
Copy link
Author

mshick commented Feb 24, 2022

Nice! Glad that PR landed. Appears to solve the issue.

@Cameramorphic
Copy link

Hi, I also tested this locally and it solves the issue.
I have a question though: I'm glad to see the fix was already merged, but I'm currently blocked by this bug and am wondering when you plan to release it? @epoberezkin

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

Successfully merging a pull request may close this issue.

3 participants