The README contains this example for oneOf:
{
"title": "MultipleObjects",
"id": "foo",
"type": "object",
"oneOf":[
{"$ref": "#/definitions/ErrorResponse"},
{"$ref": "#/definitions/VersionGetResponse"}
],
"definitions": {
"ErrorResponse": {
"title": "Error Response",
"id": "Error Response",
"type": "object",
"properties": {
"message": {"type": "string"},
"status": {"type": "integer"}
},
"required": ["message", "status"]
},
"VersionGetResponse": {
"title": "Version Get Response",
"type": "object",
"properties": {
"local": {"type": "boolean"},
"version": {"type": "string"}
},
"required": ["version"]
}
}
}
I see this:
$ ipython
>>> import sys
>>> sys.version
'3.7.1 (default, Oct 22 2018, 11:21:55) \n[GCC 8.2.0]'
>>> import python_jsonschema_objects as pjs
>>> pjs.__version__
'0.3.5'
>>> builder = pjs.ObjectBuilder("oneOf-example.json")
>>> classes = builder.build_classes(named_only=True)
# create two test objects
>>> er = classes.ErrorResponse(message="Danger!", status=99)
>>> vgr = classes.VersionGetResponse(local=False, version="1.2.3")
# round-trip serialize-deserialize into named classes
>>> er2 = classes.ErrorResponse.from_json(er.serialize())
>>> vgr2 = classes.VersionGetResponse.from_json(vgr.serialize())
# round-trip serialize-deserialize into class defined with `oneOf`
>>> er2 = classes.Multipleobjects.from_json(er.serialize())
>>> vgr2 = classes.Multipleobjects.from_json(vgr.serialize())
Traceback (most recent call last):
File "<ipython-input-10-53386795083b>", line 1, in <module>
vgr2 = classes.Multipleobjects.from_json(vgr.serialize())
File "/home/reece/.virtualenvs/default-3.7.1/lib/python3.7/site-packages/python_jsonschema_objects/classbuilder.py", line 116, in from_json
obj.validate()
File "/home/reece/.virtualenvs/default-3.7.1/lib/python3.7/site-packages/python_jsonschema_objects/classbuilder.py", line 272, in validate
.format(missing, self.__class__.__name__))
ValidationError: '['message', 'status']' are required attributes for ErrorResponse
Swapping the order of the ErrorResponse and VersionGetResponse in the schema file does not change the outcome. Removing the required section from the ErrorResponse definition is sufficient to allow the above examples to succeed.
The README contains this example for
oneOf:I see this:
Swapping the order of the
ErrorResponseandVersionGetResponsein the schema file does not change the outcome. Removing therequiredsection from theErrorResponsedefinition is sufficient to allow the above examples to succeed.