Skip to content

Commit 76d4209

Browse files
authored
Merge pull request #64 from cwacek/hotfix/ref-resolution-bug
bugfix: Resolve the correct reference type for objects
2 parents edfbb67 + 7545e3b commit 76d4209

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

python_jsonschema_objects/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from jsonschema.compat import iteritems
44
import json
55
import codecs
6+
import copy
67
import os.path
78
import inflection
89
import six
@@ -52,6 +53,17 @@ def __init__(self, schema_uri, resolved={}):
5253
self._classes = None
5354
self._resolved = None
5455

56+
@property
57+
def schema(self):
58+
try:
59+
return copy.deepcopy(self._schema)
60+
except AttributeError:
61+
raise ValidationError("No schema provided")
62+
63+
@schema.setter
64+
def schema(self, val):
65+
setattr(self, '_schema', val)
66+
5567
@property
5668
def classes(self):
5769
if self._classes is None:

python_jsonschema_objects/classbuilder.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ def _build_object(self, nm, clsdata, parents,**kw):
536536
name_translation = {}
537537

538538
for prop, detail in properties.items():
539+
logger.debug(util.lazy_format("Handling property {0}.{1}",nm, prop))
539540
properties[prop]['raw_name'] = prop
540541
name_translation[prop] = prop.replace('@', '')
541542
prop = name_translation[prop]
@@ -556,6 +557,9 @@ def _build_object(self, nm, clsdata, parents,**kw):
556557
elif 'type' not in detail and '$ref' in detail:
557558
ref = detail['$ref']
558559
uri = util.resolve_ref_uri(self.resolver.resolution_scope, ref)
560+
logger.debug(util.lazy_format("Resolving reference {0} for {1}.{2}",
561+
ref, nm, prop
562+
))
559563
if uri not in self.resolved:
560564
with self.resolver.resolving(ref) as resolved:
561565
self.resolved[uri] = self.construct(

test/test_pytest.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,34 @@ def test_regression_9():
4141
builder.build_classes()
4242

4343

44+
def test_build_classes_is_idempotent():
45+
schema = {
46+
"$schema": "http://json-schema.org/schema#",
47+
"title": "test",
48+
"type": "object",
49+
"properties": {
50+
"name": {"$ref": "#/definitions/foo"},
51+
"email": {"oneOf": [{"type": "string"}, {"type": "integer"}]},
52+
},
53+
"required": ["email"],
54+
"definitions": {
55+
"reffed": {
56+
"type": "string"
57+
},
58+
"foo": {
59+
"type": "array",
60+
"items": {
61+
"$ref": "#/definitions/reffed"
62+
}
63+
}
64+
}
65+
}
66+
builder = pjs.ObjectBuilder(schema)
67+
builder.build_classes()
68+
builder.build_classes()
69+
70+
71+
4472
def test_underscore_properties():
4573
schema = {
4674
"$schema": "http://json-schema.org/schema#",

0 commit comments

Comments
 (0)