-
Notifications
You must be signed in to change notification settings - Fork 140
Description
Jane version(s) affected: 7.9.0
Description
I expect that when using oneOf to make an object property nullable, that the NotNullValidator is not used for that property.
How to reproduce
Using a schema with a property like this, enable validation for Jane, and generate the classes. The MyObjectConstraint class will have a NotNullValidator for the myProperty property.
// myObject.json
{
"type": "object",
"properties": {
"myProperty": {
"oneOf": [
{
"type": "null"
},
{
"$ref": "/ref/to/myPropertySchema.json"
}
]
}
},
"required": [
],
"additionalProperties": false,
}
Possible Solution
In the ObjectGuesser class, you already check if the property is nullable by checking the 'type' property of each of the oneOf values:
"myProperty": {
"oneOf": [
{
"type": "null" <=== this one
},
{
"$ref": "/ref/to/myPropertySchema.json" <==== and the one inside here
}
]
}
That works correctly. Once the data is passed to the ChainValidator's guess method, that is ignored.
The NotNullValidator checks the 'type' property of the object that contains the oneOf values:
"myProperty": {
"oneOf": [
{
"type": "null"
},
{
"$ref": "/ref/to/myPropertySchema.json"
}
]
// type <==== this one, which is null because I never set one
}
To fix this, I think you can do one of the following:
- update the NotNullValidator's guess method to include handling for oneOf for JsonSchema
- update the ChainValidator to check the nullable property of the Property $guess parameter it to skip the NotNullValidator
- have the NotNullValidator check the nullable property by passing the $guess to it
Additional context