Skip to content

Commit fd28c9c

Browse files
authored
Merge pull request #280 from rmspeers/51-nested-oneOf
#51 handle nested anyof, ensure that data gets passed down via object creators
2 parents 081a50d + 15f06d7 commit fd28c9c

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

python_jsonschema_objects/classbuilder.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ def _build_object(self, nm, clsdata, parents, **kw):
672672

673673
if detail.get("type", None) == "object":
674674
uri = "{0}/{1}_{2}".format(nm, prop, "<anonymous>")
675-
self.resolved[uri] = self.construct(uri, detail, (ProtocolBase,))
675+
self.resolved[uri] = self.construct(uri, detail, (ProtocolBase,), **kw)
676676

677677
props[prop] = make_property(
678678
prop, {"type": self.resolved[uri]}, self.resolved[uri].__doc__
@@ -719,7 +719,7 @@ def _build_object(self, nm, clsdata, parents, **kw):
719719
)
720720
)
721721
else:
722-
typ = self.construct(uri, detail["items"])
722+
typ = self.construct(uri, detail["items"], **kw)
723723

724724
constraints = copy.copy(detail)
725725
constraints["strict"] = kw.get("strict")
@@ -746,15 +746,15 @@ def _build_object(self, nm, clsdata, parents, **kw):
746746
typs = []
747747
for i, elem in enumerate(detail["items"]):
748748
uri = "{0}/{1}/<anonymous_{2}>".format(nm, prop, i)
749-
typ = self.construct(uri, elem)
749+
typ = self.construct(uri, elem, **kw)
750750
typs.append(typ)
751751

752752
props[prop] = make_property(prop, {"type": typs})
753753

754754
else:
755755
desc = detail["description"] if "description" in detail else ""
756756
uri = "{0}/{1}".format(nm, prop)
757-
typ = self.construct(uri, detail)
757+
typ = self.construct(uri, detail, **kw)
758758

759759
props[prop] = make_property(prop, {"type": typ}, desc)
760760

test/test_feature_51.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,56 @@ def test_simple_array_anyOf():
4646
assert y.ExampleAnyOf == "[email protected]"
4747

4848

49+
def test_nested_anyOf():
50+
basicSchemaDefn = {
51+
"$schema": "http://json-schema.org/draft-04/schema#",
52+
"title": "Test",
53+
"properties": {"ExampleAnyOf": {"$ref": "#/definitions/externalItem"}},
54+
"required": ["ExampleAnyOf"],
55+
"type": "object",
56+
"definitions": {
57+
"externalItem": {
58+
"type": "object",
59+
"properties": {
60+
"something": {"type": "string"},
61+
"exampleAnyOf": {
62+
"anyOf": [
63+
{"type": "string", "format": "email"},
64+
{"type": "string", "maxlength": 0},
65+
]
66+
},
67+
},
68+
}
69+
},
70+
}
71+
72+
builder = pjo.ObjectBuilder(basicSchemaDefn)
73+
74+
ns = builder.build_classes(any_of="use-first")
75+
ns.Test().from_json(
76+
'{"ExampleAnyOf" : {"something": "someone", "exampleAnyOf": "[email protected]"} }'
77+
)
78+
79+
with pytest.raises(pjo.ValidationError):
80+
# Because this does not match the email format:
81+
ns.Test().from_json(
82+
'{"ExampleAnyOf" : {"something": "someone", "exampleAnyOf": "not-a-email-com"} }'
83+
)
84+
85+
# Does it also work when not deserializing?
86+
x = ns.Test(ExampleAnyOf={"something": "somestring"})
87+
with pytest.raises(pjo.ValidationError):
88+
x.ExampleAnyOf.exampleAnyOf = ""
89+
90+
with pytest.raises(pjo.ValidationError):
91+
x.ExampleAnyOf.exampleAnyOf = "not-an-email"
92+
93+
x.ExampleAnyOf.exampleAnyOf = "[email protected]"
94+
out = x.serialize()
95+
y = ns.Test.from_json(out)
96+
assert y.ExampleAnyOf.exampleAnyOf == "[email protected]"
97+
98+
4999
def test_simple_array_anyOf_withoutConfig():
50100
basicSchemaDefn = {
51101
"$schema": "http://json-schema.org/draft-04/schema#",

0 commit comments

Comments
 (0)