Discriminator-Property not working in `allOf`
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.
Looks like a duplicate of #2261
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?
Yes, it's a limitation, as documented. For it to work it should have discriminator tag property defined on the top level in subschema.
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"]
}
}
}
]
}
]
}