Skip to content

Commit

Permalink
Merge pull request #280 from rmspeers/51-nested-oneOf
Browse files Browse the repository at this point in the history
#51 handle nested anyof, ensure that data gets passed down via object creators
  • Loading branch information
cwacek authored Mar 1, 2024
2 parents 081a50d + 15f06d7 commit fd28c9c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
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 == "[email protected]"


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": "[email protected]"} }'
)

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 = "[email protected]"
out = x.serialize()
y = ns.Test.from_json(out)
assert y.ExampleAnyOf.exampleAnyOf == "[email protected]"


def test_simple_array_anyOf_withoutConfig():
basicSchemaDefn = {
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down

0 comments on commit fd28c9c

Please sign in to comment.