hypothesis-jsonschema icon indicating copy to clipboard operation
hypothesis-jsonschema copied to clipboard

Mis-canonicalisation of `const` in draft04 schemas (not a keyword until draft06)

Open Zac-HD opened this issue 4 years ago • 2 comments

The const keyword was added in draft-06, and therefore has no effect whatsoever in draft-04 schemas. While authors probably intended to constrain the value somewhat, we still have to follow the spec!

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "oneOf": [{"type": "boolean"}, {"const": None}],
    "type": ["boolean", "null"],
}
# Should canonicalise 
#    {"const": None} -> {}
#    "oneOf": [{"type": "boolean"}, {}] -> "not": {"type": "boolean"}
# and ultimately to
{"enum": [None]}

See also https://github.com/Julian/jsonschema/issues/778; longer term this is another argument for my do-it-right schema processing redesign...

Zac-HD avatar Feb 03 '21 12:02 Zac-HD

By coincidence, the very same behavior happened to me yesterday - I was canonicalising the Swagger schema (where $schema points to draft04) components and my test suite started failing when single-item enums were transformed to const :) Probably having a condition on the draft version should be sufficient to fix it

Stranger6667 avatar Feb 03 '21 12:02 Stranger6667

Yep.

We'll need to start passing the schema version around though, as well as adding version-awarness to gen_schema().

Zac-HD avatar Feb 03 '21 12:02 Zac-HD