hypothesis-jsonschema
hypothesis-jsonschema copied to clipboard
Mis-canonicalisation of `const` in draft04 schemas (not a keyword until draft06)
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...
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
Yep.
We'll need to start passing the schema version around though, as well as adding version-awarness to gen_schema().