Skip to content

Commit

Permalink
Only deserialize data from elasticsearch, not from python
Browse files Browse the repository at this point in the history
  • Loading branch information
honzakral committed Dec 26, 2017
1 parent 91ea631 commit 9574374
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
5 changes: 5 additions & 0 deletions elasticsearch_dsl/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ def _deserialize(self, data):
def _serialize(self, data):
if data is None:
return None

# somebody assigned raw dict to the field, we should tolerate that
if isinstance(data, collections.Mapping):
return data

return data.to_dict()

def clean(self, data):
Expand Down
13 changes: 4 additions & 9 deletions elasticsearch_dsl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,6 @@ def _clone(self):

class ObjectBase(AttrDict):
def __init__(self, **kwargs):
m = self._doc_type.mapping
for k in m:
if k in kwargs and m[k]._coerce:
kwargs[k] = m[k].deserialize(kwargs[k])
super(ObjectBase, self).__init__(kwargs)

@classmethod
Expand All @@ -352,6 +348,10 @@ def from_es(cls, hit):
else:
doc[k] = v

m = cls._doc_type.mapping
for k in m:
if k in doc and m[k]._coerce:
doc[k] = m[k].deserialize(doc[k])
return cls(meta=meta, **doc)

def __getattr__(self, name):
Expand All @@ -368,11 +368,6 @@ def __getattr__(self, name):
return value
raise

def __setattr__(self, name, value):
if name in self._doc_type.mapping:
value = self._doc_type.mapping[name].deserialize(value)
super(ObjectBase, self).__setattr__(name, value)

def to_dict(self):
out = {}
for k, v in iteritems(self._d_):
Expand Down
13 changes: 7 additions & 6 deletions test_elasticsearch_dsl/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_custom_field():
assert {'title': 'Uryyb'} == s.to_dict()
assert s.title == 'Hello'

s.title = 'Uryyb'
s = SecretDoc.from_es({'_source': {'title': 'Uryyb'}})
assert s.title == 'Hello'
assert isinstance(s.title, Secret)

Expand Down Expand Up @@ -148,7 +148,7 @@ def test_multi_works_after_doc_has_been_saved():

def test_multi_works_in_nested_after_doc_has_been_serialized():
# Issue #359
c = DocWithNested(comments=[{'title': 'First!'}])
c = DocWithNested(comments=[Comment(title='First!')])

assert [] == c.comments[0].tags
assert {'comments': [{'title': 'First!'}]} == c.to_dict()
Expand Down Expand Up @@ -195,7 +195,7 @@ def test_attribute_can_be_removed():
assert 'title' not in d._d_

def test_doc_type_can_be_correctly_pickled():
d = DocWithNested(title='Hello World!', comments=[{'title': 'hellp'}], meta={'id': 42})
d = DocWithNested(title='Hello World!', comments=[Comment(title='hellp')], meta={'id': 42})
s = pickle.dumps(d)

d2 = pickle.loads(s)
Expand Down Expand Up @@ -271,7 +271,7 @@ def password(self, pwd):
u.password

def test_nested_can_be_assigned_to():
d1 = DocWithNested(comments=[{'title': 'First!'}])
d1 = DocWithNested(comments=[Comment(title='First!')])
d2 = DocWithNested()

d2.comments = d1.comments
Expand All @@ -295,7 +295,7 @@ def test_nested_defaults_to_list_and_can_be_updated():

def test_to_dict_is_recursive_and_can_cope_with_multi_values():
md = MyDoc(name=['a', 'b', 'c'])
md.inner = [{'old_field': 'of1'}, {'old_field': 'of2'}]
md.inner = [MyInner(old_field='of1'), MyInner(old_field='of2')]

assert isinstance(md.inner[0], MyInner)

Expand Down Expand Up @@ -367,8 +367,9 @@ def test_document_can_be_created_dynamically():

def test_invalid_date_will_raise_exception():
md = MyDoc()
md.created_at = 'not-a-date'
with raises(ValidationException):
md.created_at = 'not-a-date'
md.full_clean()

def test_document_inheritance():
assert issubclass(MySubDoc, MyDoc)
Expand Down
2 changes: 1 addition & 1 deletion test_elasticsearch_dsl/test_integration/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_nested_top_hits_are_wrapped_properly(pull_request):

def test_update_object_field(write_client):
Wiki.init()
w = Wiki(owner={'name': 'Honza Kral'}, _id='elasticsearch-py')
w = Wiki(owner=User(name='Honza Kral'), _id='elasticsearch-py')
w.save()

w.update(owner=[{'name': 'Honza'}, {'name': 'Nick'}])
Expand Down
5 changes: 3 additions & 2 deletions test_elasticsearch_dsl/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ def test_validation_works_for_lists_of_values():
class DT(DocType):
i = Date(required=True)

dt = DT(i=[datetime.now(), 'not date'])
with raises(ValidationException):
DT(i=[datetime.now(), 'not date'])
dt.full_clean()

dt = DT(i=[datetime.now(), datetime.now()])
assert None is dt.full_clean()
Expand Down Expand Up @@ -97,7 +98,7 @@ def test_boolean_doesnt_treat_false_as_empty():


def test_custom_validation_on_nested_gets_run():
d = BlogPost(authors=[{'name': 'Honza', 'email': 'king@example.com'}], created=None)
d = BlogPost(authors=[Author(name='Honza', email='king@example.com')], created=None)

assert isinstance(d.authors[0], Author)

Expand Down

0 comments on commit 9574374

Please sign in to comment.