diff --git a/python_jsonschema_objects/classbuilder.py b/python_jsonschema_objects/classbuilder.py index 211b9c8..e9aaabe 100644 --- a/python_jsonschema_objects/classbuilder.py +++ b/python_jsonschema_objects/classbuilder.py @@ -672,7 +672,7 @@ def _build_object(self, nm, clsdata, parents, **kw): if detail.get("type", None) == "object": uri = "{0}/{1}_{2}".format(nm, prop, "") - self.resolved[uri] = self.construct(uri, detail, (ProtocolBase,)) + self.resolved[uri] = self.construct(uri, detail, (ProtocolBase,), **kw) props[prop] = make_property( prop, {"type": self.resolved[uri]}, self.resolved[uri].__doc__ @@ -719,7 +719,7 @@ def _build_object(self, nm, clsdata, parents, **kw): ) ) else: - typ = self.construct(uri, detail["items"]) + typ = self.construct(uri, detail["items"], **kw) constraints = copy.copy(detail) constraints["strict"] = kw.get("strict") @@ -746,7 +746,7 @@ def _build_object(self, nm, clsdata, parents, **kw): typs = [] for i, elem in enumerate(detail["items"]): uri = "{0}/{1}/".format(nm, prop, i) - typ = self.construct(uri, elem) + typ = self.construct(uri, elem, **kw) typs.append(typ) props[prop] = make_property(prop, {"type": typs}) @@ -754,7 +754,7 @@ def _build_object(self, nm, clsdata, parents, **kw): else: desc = detail["description"] if "description" in detail else "" uri = "{0}/{1}".format(nm, prop) - typ = self.construct(uri, detail) + typ = self.construct(uri, detail, **kw) props[prop] = make_property(prop, {"type": typ}, desc) diff --git a/test/test_feature_51.py b/test/test_feature_51.py index 4716249..1be0db3 100644 --- a/test/test_feature_51.py +++ b/test/test_feature_51.py @@ -46,6 +46,56 @@ def test_simple_array_anyOf(): assert y.ExampleAnyOf == "test@example.com" +def test_nested_anyOf(): + basicSchemaDefn = { + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Test", + "properties": {"ExampleAnyOf": {"$ref": "#/definitions/externalItem"}}, + "required": ["ExampleAnyOf"], + "type": "object", + "definitions": { + "externalItem": { + "type": "object", + "properties": { + "something": {"type": "string"}, + "exampleAnyOf": { + "anyOf": [ + {"type": "string", "format": "email"}, + {"type": "string", "maxlength": 0}, + ] + }, + }, + } + }, + } + + builder = pjo.ObjectBuilder(basicSchemaDefn) + + ns = builder.build_classes(any_of="use-first") + ns.Test().from_json( + '{"ExampleAnyOf" : {"something": "someone", "exampleAnyOf": "test@example.com"} }' + ) + + with pytest.raises(pjo.ValidationError): + # Because this does not match the email format: + ns.Test().from_json( + '{"ExampleAnyOf" : {"something": "someone", "exampleAnyOf": "not-a-email-com"} }' + ) + + # Does it also work when not deserializing? + x = ns.Test(ExampleAnyOf={"something": "somestring"}) + with pytest.raises(pjo.ValidationError): + x.ExampleAnyOf.exampleAnyOf = "" + + with pytest.raises(pjo.ValidationError): + x.ExampleAnyOf.exampleAnyOf = "not-an-email" + + x.ExampleAnyOf.exampleAnyOf = "test@example.com" + out = x.serialize() + y = ns.Test.from_json(out) + assert y.ExampleAnyOf.exampleAnyOf == "test@example.com" + + def test_simple_array_anyOf_withoutConfig(): basicSchemaDefn = { "$schema": "http://json-schema.org/draft-04/schema#",