Skip to content

Commit 9f2c84a

Browse files
committed
Merge pull request #735 from elasticsearch/fix/675
Fixed template mapping: type name wasn't properly inferred
2 parents 1d2113e + d1012e7 commit 9f2c84a

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

src/Nest/DSL/PutTemplateDescriptor.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,15 @@ public PutTemplateDescriptor AddMapping<T>(Func<PutMappingDescriptor<T>, PutMapp
5454
where T : class
5555
{
5656
mappingSelector.ThrowIfNull("mappingSelector");
57-
var rootObjectMappingDescriptor = mappingSelector(new PutMappingDescriptor<T>(this._connectionSettings));
58-
rootObjectMappingDescriptor.ThrowIfNull("rootObjectMappingDescriptor");
57+
var putMappingDescriptor = mappingSelector(new PutMappingDescriptor<T>(this._connectionSettings));
58+
putMappingDescriptor.ThrowIfNull("rootObjectMappingDescriptor");
5959

6060
var inferrer = new ElasticInferrer(this._connectionSettings);
61-
var typeName = inferrer.TypeName(rootObjectMappingDescriptor._Type);
61+
var typeName = inferrer.TypeName(putMappingDescriptor._Type ?? typeof(T));
6262
if (typeName == null)
6363
return this;
64-
this._TemplateMapping.Mappings[typeName] = rootObjectMappingDescriptor._Mapping;
64+
this._TemplateMapping.Mappings[typeName] = putMappingDescriptor._Mapping;
6565
return this;
66-
6766
}
6867

6968
public PutTemplateDescriptor AddWarmer<T>(Func<CreateWarmerDescriptor, CreateWarmerDescriptor> warmerSelector)

src/Tests/Nest.Tests.Integration/Template/TemplateTests.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public void PutTemplateWithMappings()
6363
this._client.DeleteTemplate("put-template-with-mappings");
6464
var putResponse = this._client.PutTemplate("put-template-with-mappings",t => t
6565
.Template("donotinfluencothertests")
66-
.AddMapping<dynamic>(s=>s
67-
.Type("mytype")
66+
.AddMapping<ElasticsearchProject>(s=>s
6867
.AllField(a=>a.Enabled(false))
6968
)
7069
);
@@ -78,9 +77,9 @@ public void PutTemplateWithMappings()
7877

7978
var mappings = templateResponse.TemplateMapping.Mappings;
8079

81-
Assert.IsTrue(mappings.ContainsKey("mytype"), "put-template-with-mappings template should have a `mytype` mapping");
82-
Assert.NotNull(mappings["mytype"].AllFieldMapping, "`mytype` mapping should contain the _all field mapping");
83-
Assert.AreEqual(false, mappings["mytype"].AllFieldMapping._Enabled, "_all { enabled } should be set to false");
80+
Assert.IsTrue(mappings.ContainsKey("elasticsearchprojects"), "put-template-with-mappings template should have a `mytype` mapping");
81+
Assert.NotNull(mappings["elasticsearchprojects"].AllFieldMapping, "`mytype` mapping should contain the _all field mapping");
82+
Assert.AreEqual(false, mappings["elasticsearchprojects"].AllFieldMapping._Enabled, "_all { enabled } should be set to false");
8483
}
8584

8685
[Test]

0 commit comments

Comments
 (0)