diff --git a/elasticsearch_dsl/field.py b/elasticsearch_dsl/field.py index 428ad3022..ecdc19f7b 100644 --- a/elasticsearch_dsl/field.py +++ b/elasticsearch_dsl/field.py @@ -1,4 +1,5 @@ import base64 +import copy import ipaddress try: @@ -151,7 +152,7 @@ def __init__(self, doc_class=None, dynamic=None, properties=None, **kwargs): if dynamic is not None: self._doc_class._doc_type.mapping.meta('dynamic', dynamic) - self._mapping = self._doc_class._doc_type.mapping + self._mapping = copy.deepcopy(self._doc_class._doc_type.mapping) super(Object, self).__init__(**kwargs) def __getitem__(self, name): diff --git a/test_elasticsearch_dsl/test_document.py b/test_elasticsearch_dsl/test_document.py index 4b5073a9a..c9f834c9b 100644 --- a/test_elasticsearch_dsl/test_document.py +++ b/test_elasticsearch_dsl/test_document.py @@ -582,3 +582,20 @@ class Index: assert c.meta.fields._tags == ['search'] assert c.meta.fields._routing == 'es' assert c._tagline == 'You know, for search' + +def test_nested_and_object_inner_doc(): + class MySubDocWithNested(MyDoc): + nested_inner = field.Nested(MyInner) + + props = MySubDocWithNested._doc_type.mapping.to_dict()['properties'] + assert props == { + "created_at": {"type": "date"}, + "inner": {"properties": {"old_field": {"type": "text"}}, "type": "object"}, + "name": {"type": "text"}, + "nested_inner": { + "properties": {"old_field": {"type": "text"}}, + "type": "nested", + }, + "title": {"type": "keyword"}, + } +