Skip to content

Commit 9574374

Browse files
committed
Only deserialize data from elasticsearch, not from python
1 parent 91ea631 commit 9574374

File tree

5 files changed

+20
-18
lines changed

5 files changed

+20
-18
lines changed

elasticsearch_dsl/field.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ def _deserialize(self, data):
159159
def _serialize(self, data):
160160
if data is None:
161161
return None
162+
163+
# somebody assigned raw dict to the field, we should tolerate that
164+
if isinstance(data, collections.Mapping):
165+
return data
166+
162167
return data.to_dict()
163168

164169
def clean(self, data):

elasticsearch_dsl/utils.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,6 @@ def _clone(self):
335335

336336
class ObjectBase(AttrDict):
337337
def __init__(self, **kwargs):
338-
m = self._doc_type.mapping
339-
for k in m:
340-
if k in kwargs and m[k]._coerce:
341-
kwargs[k] = m[k].deserialize(kwargs[k])
342338
super(ObjectBase, self).__init__(kwargs)
343339

344340
@classmethod
@@ -352,6 +348,10 @@ def from_es(cls, hit):
352348
else:
353349
doc[k] = v
354350

351+
m = cls._doc_type.mapping
352+
for k in m:
353+
if k in doc and m[k]._coerce:
354+
doc[k] = m[k].deserialize(doc[k])
355355
return cls(meta=meta, **doc)
356356

357357
def __getattr__(self, name):
@@ -368,11 +368,6 @@ def __getattr__(self, name):
368368
return value
369369
raise
370370

371-
def __setattr__(self, name, value):
372-
if name in self._doc_type.mapping:
373-
value = self._doc_type.mapping[name].deserialize(value)
374-
super(ObjectBase, self).__setattr__(name, value)
375-
376371
def to_dict(self):
377372
out = {}
378373
for k, v in iteritems(self._d_):

test_elasticsearch_dsl/test_document.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def test_custom_field():
119119
assert {'title': 'Uryyb'} == s.to_dict()
120120
assert s.title == 'Hello'
121121

122-
s.title = 'Uryyb'
122+
s = SecretDoc.from_es({'_source': {'title': 'Uryyb'}})
123123
assert s.title == 'Hello'
124124
assert isinstance(s.title, Secret)
125125

@@ -148,7 +148,7 @@ def test_multi_works_after_doc_has_been_saved():
148148

149149
def test_multi_works_in_nested_after_doc_has_been_serialized():
150150
# Issue #359
151-
c = DocWithNested(comments=[{'title': 'First!'}])
151+
c = DocWithNested(comments=[Comment(title='First!')])
152152

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

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

201201
d2 = pickle.loads(s)
@@ -271,7 +271,7 @@ def password(self, pwd):
271271
u.password
272272

273273
def test_nested_can_be_assigned_to():
274-
d1 = DocWithNested(comments=[{'title': 'First!'}])
274+
d1 = DocWithNested(comments=[Comment(title='First!')])
275275
d2 = DocWithNested()
276276

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

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

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

@@ -367,8 +367,9 @@ def test_document_can_be_created_dynamically():
367367

368368
def test_invalid_date_will_raise_exception():
369369
md = MyDoc()
370+
md.created_at = 'not-a-date'
370371
with raises(ValidationException):
371-
md.created_at = 'not-a-date'
372+
md.full_clean()
372373

373374
def test_document_inheritance():
374375
assert issubclass(MySubDoc, MyDoc)

test_elasticsearch_dsl/test_integration/test_document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_nested_top_hits_are_wrapped_properly(pull_request):
8484

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

9090
w.update(owner=[{'name': 'Honza'}, {'name': 'Nick'}])

test_elasticsearch_dsl/test_validation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ def test_validation_works_for_lists_of_values():
5353
class DT(DocType):
5454
i = Date(required=True)
5555

56+
dt = DT(i=[datetime.now(), 'not date'])
5657
with raises(ValidationException):
57-
DT(i=[datetime.now(), 'not date'])
58+
dt.full_clean()
5859

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

9899

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

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

0 commit comments

Comments
 (0)