python-jsonschema/hypothesis-jsonschema

Issue with contains group

ramkesh-vats opened this issue · 1 comments

Here I got error
Unsatisfiable: Unable to satisfy assumptions of hypothesis example_generating_inner_function.
when I called
sample = from_schema(schema).example()
using the JSON schema.

{ 'type': 'object', 'required': [ 'Header' ], 'properties': { 'Header': { 'type': 'object', 'required': [ 'Address' ], 'properties': { 'Address': { 'type': 'array', 'items': { 'type': 'object', 'required': [ 'AddressTypeCode' ], 'properties': { 'AddressTypeCode': { 'type': 'string' } } }, 'minItems': 2, 'allOf': [ { 'contains': { 'required': [ 'AddressTypeCode' ], 'type': 'object', 'properties': { 'AddressTypeCode': { 'type': 'string', 'enum': [ 'ST' ] } } } }, { 'contains': { 'required': [ 'AddressTypeCode' ], 'type': 'object', 'properties': { 'AddressTypeCode': { 'type': 'string', 'enum': [ 'SF' ] } } } } ] } }, 'description': 'Encloses all document header elements.' } } }

Schema looks straight forword, this could be due to contains group, am not sure.

The problem comes from this part:

{   "type": "array",
    "items": {
        "properties": {"AddressTypeCode": {"type": "string"}},
        "required": ["AddressTypeCode"],
        "type": "object",
    },
    "allOf": [
        {"contains": {
            "properties": {"AddressTypeCode": {"const": "SF"}},
            "required": ["AddressTypeCode"],
            "type": "object",
        }},
        {"contains": {
            "properties": {"AddressTypeCode": {"const": "ST"}},
            "required": ["AddressTypeCode"],
            "type": "object",
        }}
    ]
}

because hypothesis-jsonschema isn't (yet) smart enough to 'see through' the allOf to the pair of contains requirements, and it's very unlikely to generate the two specific strings you need by chance.

As a workaround, you could list them as options in the items code, like so:

      "items": {
-         "properties": {"AddressTypeCode": {"type": "string"}},
+         "properties": {"AddressTypeCode": {
+             "anyOf": [{"type": "string"}, {"enum": ["SF", "ST"]}]
+         },
          "required": ["AddressTypeCode"],

Hope that helps!