Skip to content

Commit

Permalink
Fixed a type error that occurs when you use the name of an object tha…
Browse files Browse the repository at this point in the history
…t is not declared in the file.
  • Loading branch information
kacperpaczos committed Jan 13, 2024
1 parent b00b410 commit ef976b3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
18 changes: 13 additions & 5 deletions python_jsonschema_objects/classbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,19 @@ def __setattr__(self, name, val):
else:
# This is an additional property of some kind
try:
val = self.__extensible__.instantiate(name, val)
# ClassBuilder re-builds a class based on objects already defined by Python,
# adding properties from the JSON file. If an object already exists,
# it's not validated again during the build process.
# Your classes are in _extended_properties
# Properties from file are in _properties
if not isinstance(name, object):
val = self.__extensible__.instantiate(name, val)
except Exception as e:
raise validators.ValidationError(
"Attempted to set unknown property '{0}': {1} ".format(name, e)
)

self._extended_properties[name] = val

""" Implement collections.MutableMapping methods """

def __iter__(self):
Expand All @@ -261,11 +266,13 @@ def __getattr__(self, name):
name = str(name)
if name in self.__prop_names__:
raise AttributeError(name)

if name in self._extended_properties:
return self._extended_properties[name]
raise AttributeError(
"{0} is not a valid property of {1}".format(name, self.__class__.__name__)
)
else:
raise AttributeError(
"{0} is not a valid property of {1}".format(name, self.__class__.__name__)
)

def __setitem__(self, key, val):
key = str(key)
Expand Down Expand Up @@ -803,3 +810,4 @@ def make_property(prop, info, desc=""):

prop = descriptors.AttributeDescriptor(prop, info, desc)
return prop

28 changes: 16 additions & 12 deletions python_jsonschema_objects/pattern_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from python_jsonschema_objects import util, validators, wrapper_types
from python_jsonschema_objects.literals import MakeLiteral

logger = logging.getLogger(__name__)

PatternDef = collections.namedtuple("PatternDef", "pattern schema_type")
Expand Down Expand Up @@ -80,6 +79,7 @@ def _make_type(self, typ, val):
)

def instantiate(self, name, val):

import python_jsonschema_objects.classbuilder as cb

for p in self._pattern_types:
Expand All @@ -89,23 +89,27 @@ def instantiate(self, name, val):
)
return self._make_type(p.schema_type, val)

if self._additional_type is True:
if self._additional_type is True:
# Extract the type of the value
valtype = [
k
for k, t in validators.SCHEMA_TYPE_MAPPING
if t is not None and isinstance(val, t)
key
for key, schema_type in validators.SCHEMA_TYPE_MAPPING
if schema_type is not None and isinstance(val, schema_type)
]

if valtype:
valtype = valtype[0]
return MakeLiteral(name, valtype, val)
else:
# Handle the case where valtype is an empty list
raise validators.ValidationError("Unable to determine valtype")

if self._additional_properties is False:
# Handle the case where valtype is an empty list
raise validators.ValidationError("Unable to determine valtype")
else:
valtype={}
return MakeLiteral(name, valtype, val)
elif isinstance(self._additional_type, (type, cb.TypeProxy)):
return self._make_type(self._additional_type, val)
else:
raise validators.ValidationError(
"additionalProperties not permitted " "and no patternProperties specified"
)

raise validators.ValidationError(
"additionalProperties not permitted " "and no patternProperties specified"
)

0 comments on commit ef976b3

Please sign in to comment.