Thanks to visit codestin.com
Credit goes to github.com

Skip to content

JsonSchema oneOf nullables not working correctly with NotNullValidator #869

@Moghul

Description

@Moghul

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions