ajv icon indicating copy to clipboard operation
ajv copied to clipboard

Discriminator-Property not working in `allOf`

Open sebastian-ludwig opened this issue 2 years ago • 4 comments

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.

sebastian-ludwig avatar May 23 '23 12:05 sebastian-ludwig

Looks like a duplicate of #2261

codekie avatar May 25 '23 10:05 codekie

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?

wcloete avatar May 25 '23 10:05 wcloete

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

epoberezkin avatar Jul 29 '23 09:07 epoberezkin

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"]
            }
          }
        }
      ]
    }
  ]
}

Grafikart avatar Apr 22 '24 16:04 Grafikart