Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: raise proper error on mismatched schema, use string representation as fallback #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions calamus/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from marshmallow.exceptions import ValidationError

from calamus.schema import JsonLDSchema
from calamus.utils import ONTOLOGY_QUERY, Proxy, normalize_type, normalize_value
from calamus.utils import ONTOLOGY_QUERY, Proxy, normalize_type, normalize_value, get_type_name

logger = logging.getLogger("calamus")

Expand Down Expand Up @@ -457,6 +457,7 @@ def schema(self):
_schema._visited = self.root._visited
self._schema["from"][rdf_type] = _schema
self._schema["to"][model] = _schema
self._schema["to"][get_type_name(model)] = _schema
else:
if isinstance(nest, type) and issubclass(nest, SchemaABC):
schema_class = nest
Expand Down Expand Up @@ -488,6 +489,7 @@ def schema(self):
_top_level=False,
)
self._schema["to"][model] = self._schema["from"][rdf_type]
self._schema["to"][get_type_name(model)] = self._schema["from"][rdf_type]
return self._schema

def _serialize_single_obj(self, obj, **kwargs):
Expand All @@ -505,10 +507,13 @@ def _serialize_single_obj(self, obj, **kwargs):
# resolve Proxy object
obj = obj.__wrapped__

if type(obj) not in self.schema["to"]:
ValueError("Type {} not found in field {}.{}".format(type(obj), type(self.parent), self.name))
if type(obj) in self.schema["to"]:
schema = self.schema["to"][type(obj)]
elif get_type_name(obj) in self.schema["to"]:
schema = self.schema["to"][get_type_name(obj)]
else:
raise ValueError("Type {} not found in field {}.{}".format(type(obj), type(self.parent), self.name))

schema = self.schema["to"][type(obj)]
schema._top_level = False
return schema.dump(obj)

Expand Down
17 changes: 17 additions & 0 deletions calamus/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,20 @@ def __getattr__(self, name):
raise AttributeError(name)
else:
return getattr(self.__wrapped__, name)


def get_type_name(object) -> str:
"""Return fully-qualified object's type name.

Args:
object: The object to get the type name for.

Returns:
Optional[str]: The fully qualified type name.

"""
if object is None:
raise ValueError("Cannot get type name for None.")

object_type = object if isinstance(object, type) else type(object)
return f"{object_type.__module__}.{object_type.__qualname__}"