Fix querying an embedded document field by an invalid value (#1440)
This commit is contained in:
committed by
Stefan Wójcik
parent
96d20756ca
commit
9f4b04ea0f
@@ -16,8 +16,7 @@ from mongoengine.base.datastructures import (BaseDict, BaseList,
|
||||
SemiStrictDict, StrictDict)
|
||||
from mongoengine.base.fields import ComplexBaseField
|
||||
from mongoengine.common import _import_class
|
||||
from mongoengine.errors import (FieldDoesNotExist, InvalidDocumentError,
|
||||
LookUpError, OperationError, ValidationError)
|
||||
from mongoengine.errors import (FieldDoesNotExist, InvalidDocumentError, LookUpError, OperationError, ValidationError)
|
||||
|
||||
__all__ = ('BaseDocument',)
|
||||
|
||||
@@ -675,6 +674,9 @@ class BaseDocument(object):
|
||||
if not only_fields:
|
||||
only_fields = []
|
||||
|
||||
if son and not isinstance(son, dict):
|
||||
raise ValueError("The source SON object needs to be of type 'dict'")
|
||||
|
||||
# Get the class name from the document, falling back to the given
|
||||
# class if unavailable
|
||||
class_name = son.get('_cls', cls._class_name)
|
||||
|
||||
@@ -28,7 +28,7 @@ from mongoengine.base import (BaseDocument, BaseField, ComplexBaseField,
|
||||
GeoJsonBaseField, ObjectIdField, get_document)
|
||||
from mongoengine.connection import DEFAULT_CONNECTION_NAME, get_db
|
||||
from mongoengine.document import Document, EmbeddedDocument
|
||||
from mongoengine.errors import DoesNotExist, ValidationError
|
||||
from mongoengine.errors import DoesNotExist, InvalidQueryError, ValidationError
|
||||
from mongoengine.python_support import StringIO
|
||||
from mongoengine.queryset import DO_NOTHING, QuerySet
|
||||
|
||||
@@ -566,7 +566,11 @@ class EmbeddedDocumentField(BaseField):
|
||||
|
||||
def prepare_query_value(self, op, value):
|
||||
if value is not None and not isinstance(value, self.document_type):
|
||||
value = self.document_type._from_son(value)
|
||||
try:
|
||||
value = self.document_type._from_son(value)
|
||||
except ValueError:
|
||||
raise InvalidQueryError("Querying the embedded document '%s' failed, due to an invalid query value" %
|
||||
(self.document_type._class_name,))
|
||||
super(EmbeddedDocumentField, self).prepare_query_value(op, value)
|
||||
return self.to_mongo(value)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user