Skip to content

Commit

Permalink
Merge pull request #64 from cwacek/hotfix/ref-resolution-bug
Browse files Browse the repository at this point in the history
bugfix: Resolve the correct reference type for objects
  • Loading branch information
cwacek authored Dec 6, 2016
2 parents edfbb67 + 7545e3b commit 76d4209
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python_jsonschema_objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from jsonschema.compat import iteritems
import json
import codecs
import copy
import os.path
import inflection
import six
Expand Down Expand Up @@ -52,6 +53,17 @@ def __init__(self, schema_uri, resolved={}):
self._classes = None
self._resolved = None

@property
def schema(self):
try:
return copy.deepcopy(self._schema)
except AttributeError:
raise ValidationError("No schema provided")

@schema.setter
def schema(self, val):
setattr(self, '_schema', val)

@property
def classes(self):
if self._classes is None:
Expand Down
4 changes: 4 additions & 0 deletions python_jsonschema_objects/classbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ def _build_object(self, nm, clsdata, parents,**kw):
name_translation = {}

for prop, detail in properties.items():
logger.debug(util.lazy_format("Handling property {0}.{1}",nm, prop))
properties[prop]['raw_name'] = prop
name_translation[prop] = prop.replace('@', '')
prop = name_translation[prop]
Expand All @@ -556,6 +557,9 @@ def _build_object(self, nm, clsdata, parents,**kw):
elif 'type' not in detail and '$ref' in detail:
ref = detail['$ref']
uri = util.resolve_ref_uri(self.resolver.resolution_scope, ref)
logger.debug(util.lazy_format("Resolving reference {0} for {1}.{2}",
ref, nm, prop
))
if uri not in self.resolved:
with self.resolver.resolving(ref) as resolved:
self.resolved[uri] = self.construct(
Expand Down
28 changes: 28 additions & 0 deletions test/test_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@ def test_regression_9():
builder.build_classes()


def test_build_classes_is_idempotent():
schema = {
"$schema": "http://json-schema.org/schema#",
"title": "test",
"type": "object",
"properties": {
"name": {"$ref": "#/definitions/foo"},
"email": {"oneOf": [{"type": "string"}, {"type": "integer"}]},
},
"required": ["email"],
"definitions": {
"reffed": {
"type": "string"
},
"foo": {
"type": "array",
"items": {
"$ref": "#/definitions/reffed"
}
}
}
}
builder = pjs.ObjectBuilder(schema)
builder.build_classes()
builder.build_classes()



def test_underscore_properties():
schema = {
"$schema": "http://json-schema.org/schema#",
Expand Down

0 comments on commit 76d4209

Please sign in to comment.