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

Discriminator-Property not working in allOf #2281

Open
sebastian-ludwig opened this issue May 23, 2023 · 4 comments
Open

Discriminator-Property not working in allOf #2281

sebastian-ludwig opened this issue May 23, 2023 · 4 comments

Comments

@sebastian-ludwig
Copy link

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

8.12.0

JSON Schema

{
  "type": "object",
  "discriminator": {
    "propertyName": "type"
  },
  "oneOf": [
    {
      "allOf": [
        {
          "$ref": "#/components/schemas/A"
        },
        {
          "type": "object",
          "required": ["type"],
          "properties": {
            "method": {
              "type": "string",
              "enum": ["a"]
            }
          }
        }
      ]
    },
    {
      "allOf": [
        {
          "$ref": "#/components/schemas/B"
        },
        {
          "type": "object",
          "required": ["type"],
          "properties": {
            "method": {
              "type": "string",
              "enum": ["b"]
            }
          }
        }
      ]
    }
  ]
}

When accessing the schema from above, I get this error:
Error: discriminator: oneOf subschemas (or referenced schemas) must have "properties/type"

For me this Schema seems to be valid, but there seems to be a problem with this nested oneOf / allOf structure.
Is there a way to solve this, because I would like to stay with this Schema which is autogenerated from the backend.

@codekie
Copy link

codekie commented May 25, 2023

Looks like a duplicate of #2261

@wcloete
Copy link

wcloete commented May 25, 2023

You may need to add the definitions for "#/components/schemas/A" and "#/components/schemas/B" to your example, because currently there is no property named type. You have a property named method, was this a typo?

@epoberezkin
Copy link
Member

Yes, it's a limitation, as documented. For it to work it should have discriminator tag property defined on the top level in subschema.

@Grafikart
Copy link

Grafikart commented Apr 22, 2024

I encountered the same limitation (thanks for explaining these limitations in the documentation) and I understand the complexity of solving this issue.

Could it be possible to add an arbitrary property to discriminate ?

{
  "type": "object",
  "discriminator": {
    "propertyName": "type"
  },
  "oneOf": [
    {
+     "discriminatorValue": ["a"],
      "allOf": [
        {
          "$ref": "#/components/schemas/A"
        },
        {
          "type": "object",
          "required": ["type"],
          "properties": {
            "method": {
              "type": "string",
              "enum": ["a"]
            }
          }
        }
      ]
    },
    {
+     "discriminatorValue": ["b"],
      "allOf": [
        {
          "$ref": "#/components/schemas/B"
        },
        {
          "type": "object",
          "required": ["type"],
          "properties": {
            "method": {
              "type": "string",
              "enum": ["b"]
            }
          }
        }
      ]
    }
  ]
}

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

No branches or pull requests

5 participants