Skip to content

Commit

Permalink
Merge pull request #168 from cwacek/feature/fix-126
Browse files Browse the repository at this point in the history
bugfix: Add a `__hash__` implementation to ProtocolBase
  • Loading branch information
cwacek authored Jul 4, 2019
2 parents a5c6420 + 2f28748 commit ae8f43f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions python_jsonschema_objects/classbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ def construct(self, uri, *args, **kw):
logger.debug(util.lazy_format("Constructing {0}", uri))
if ("override" not in kw or kw["override"] is False) and uri in self.resolved:
logger.debug(util.lazy_format("Using existing {0}", uri))
assert self.resolved[uri] is not None
return self.resolved[uri]
else:
ret = self._construct(uri, *args, **kw)
Expand Down
4 changes: 1 addition & 3 deletions python_jsonschema_objects/wrapper_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ def validate(self):
def validate_uniqueness(self):

if getattr(self, "uniqueItems", False) is True:

testset = set(self.data)
testset = set(repr(item) for item in self.data)
if len(testset) != len(self.data):
raise ValidationError(
"{0} has duplicate elements, but uniqueness required".format(
Expand Down Expand Up @@ -303,7 +302,6 @@ def create(name, item_constraint=None, **addl_constraints):

with klassbuilder.resolver.resolving(uri) as resolved:
# Set incase there is a circular reference in schema definition
klassbuilder.resolved[uri] = None
klassbuilder.resolved[uri] = klassbuilder.construct(
uri, resolved, (classbuilder.ProtocolBase,)
)
Expand Down
58 changes: 58 additions & 0 deletions test/test_regression_126.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest
import python_jsonschema_objects as pjs
import collections


@pytest.fixture
def schema():
return {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Test",
"definitions": {
"MyEnum1": {"type": "string", "enum": ["E_A", "E_B"]},
"MyEnum2": {"type": "string", "enum": ["F_A", "F_B", "F_C", "F_D"]},
"MyInt": {
"default": "0",
"type": "integer",
"minimum": 0,
"maximum": 4294967295,
},
"MyObj1": {
"type": "object",
"properties": {
"e1": {"$ref": "#/definitions/MyEnum1"},
"e2": {"$ref": "#/definitions/MyEnum2"},
"i1": {"$ref": "#/definitions/MyInt"},
},
"required": ["e1", "e2", "i1"],
},
"MyArray": {
"type": "array",
"items": {"$ref": "#/definitions/MyObj1"},
"minItems": 0,
"uniqueItems": True,
},
"MyMsg1": {
"type": "object",
"properties": {"a1": {"$ref": "#/definitions/MyArray"}},
},
"MyMsg2": {"type": "object", "properties": {"s1": {"type": "string"}}},
},
"type": "object",
"oneOf": [{"$ref": "#/definitions/MyMsg1"}, {"$ref": "#/definitions/MyMsg2"}],
}


def test_regression_126(schema):
builder = pjs.ObjectBuilder(schema)
ns = builder.build_classes(standardize_names=False)

Obj1 = ns.MyObj1
Array1 = ns.MyArray
Msg1 = ns.MyMsg1
o1 = Obj1(e1="E_A", e2="F_C", i1=2600)
o2 = Obj1(e1="E_B", e2="F_D", i1=2500)
objs = Array1([o1, o2])
msg = Msg1(a1=objs)

print(msg.serialize())

0 comments on commit ae8f43f

Please sign in to comment.