Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions python_jsonschema_objects/classbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<anonymous>")
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__
Expand Down Expand Up @@ -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")
Expand All @@ -746,15 +746,15 @@ def _build_object(self, nm, clsdata, parents, **kw):
typs = []
for i, elem in enumerate(detail["items"]):
uri = "{0}/{1}/<anonymous_{2}>".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})

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)

Expand Down
50 changes: 50 additions & 0 deletions test/test_feature_51.py
Original file line number Diff line number Diff line change
Expand Up @@ -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#",
Expand Down