jsonsystems / json-schema

JSONSchema.Net Public Repository
Apache License 2.0
663 stars 64 forks source link

Does not handle multiple types properly, yet supplied example covers both null or string #111

Closed joe-oli closed 2 years ago

joe-oli commented 3 years ago

When given an array with two objects examples, it considers only the first example to infer the type;

{
    "CustomerNumber": "ABC123",
    "contactPersons": [{
            "PersonFirstName": "Mindi",
            "PersonLastName": "Mink",
            "ContactLevel": "ORG",
            "PartySiteNumber": null
        },
        {
            "PersonFirstName": "Kendra",
            "PersonLastName": "Spade",
            "ContactLevel": "SITE",
            "PartySiteNumber": "S444666"
        }
    ]
}

The inferred type is

{
    "PartySiteNumber": {
        "$id": "#/properties/contactPersons/items/anyOf/0/properties/PartySiteNumber",
        "type": "null",
        "title": "The PartySiteNumber schema",
        "description": "An explanation about the purpose of this instance.",
        "default": null,
        "examples": [
            null
        ]
    }
}

when it should have been: "type" : ["null", "string"]

jackwootton commented 3 years ago

Thanks for taking the time to report this @joe-oli . You're right. I stripped back your example to

[
    {
        "PartySiteNumber": null
    },
    {
        "PartySiteNumber": "S444666"
    }
]

And also removed a few keywords

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "type": "array",
    "default": [],
    "examples": [
        [
            {
                "PartySiteNumber": null
            },
            {
                "PartySiteNumber": "S444666"
            }
        ]
    ],
    "items": {
        "anyOf": [
            {
                "type": "object",
                "default": {},
                "examples": [
                    {
                        "PartySiteNumber": null
                    }
                ],
                "required": [
                    "PartySiteNumber"
                ],
                "properties": {
                    "PartySiteNumber": {
                        "type": "null",
                        "default": null,
                        "examples": [
                            null
                        ]
                    }
                }
            }
        ]
    }
}
jackwootton commented 2 years ago

This has been fixed in the new version.