From f1132a1de90e674eac4fa21d81787584dc471576 Mon Sep 17 00:00:00 2001 From: Ryan Speers Date: Wed, 28 Feb 2024 22:42:54 -0500 Subject: [PATCH 1/2] #51 handle nested anyof, ensure that data gets passed down via object creators --- python_jsonschema_objects/classbuilder.py | 8 ++-- test/test_feature_51.py | 46 +++++++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) 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..91e2c2b 100644 --- a/test/test_feature_51.py +++ b/test/test_feature_51.py @@ -46,6 +46,52 @@ 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#", From 15f06d741726b045538cc64a7db29f72910575f5 Mon Sep 17 00:00:00 2001 From: Ryan Speers Date: Wed, 28 Feb 2024 22:50:33 -0500 Subject: [PATCH 2/2] #51 fix black formatting --- test/test_feature_51.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/test_feature_51.py b/test/test_feature_51.py index 91e2c2b..1be0db3 100644 --- a/test/test_feature_51.py +++ b/test/test_feature_51.py @@ -63,8 +63,8 @@ def test_nested_anyOf(): {"type": "string", "format": "email"}, {"type": "string", "maxlength": 0}, ] - } - } + }, + }, } }, } @@ -72,14 +72,18 @@ def test_nested_anyOf(): builder = pjo.ObjectBuilder(basicSchemaDefn) ns = builder.build_classes(any_of="use-first") - ns.Test().from_json('{"ExampleAnyOf" : {"something": "someone", "exampleAnyOf": "test@example.com"} }') + 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"} }') + 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'}) + x = ns.Test(ExampleAnyOf={"something": "somestring"}) with pytest.raises(pjo.ValidationError): x.ExampleAnyOf.exampleAnyOf = ""