Skip to content

Commit b86d5c9

Browse files
committed
fix #319, should default to .Name on mapping if .TypeNameMarker is null, this happens when you don't use MapFluent but manually new your mapping objects. Also throws a better exception if both .Name and .TypeNameMarker are null/empty
1 parent 4df71d4 commit b86d5c9

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/Nest.Tests.Integration/Reproduce/Reproduce319Tests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public void CreateIndexShouldNotThrowNullReference()
3131
settings.Add("search.slowlog.threshold.fetch.warn", "1s");
3232
settings.Analysis.Analyzers.Add(new KeyValuePair<string, AnalyzerBase>("keyword", new KeywordAnalyzer()));
3333
settings.Analysis.Analyzers.Add(new KeyValuePair<string, AnalyzerBase>("simple", new SimpleAnalyzer()));
34+
settings.Mappings.Add(new RootObjectMapping
35+
{
36+
Name = "my_root_object",
37+
Properties = new Dictionary<string, IElasticType>
38+
{
39+
{"my_field", new StringMapping() { Name = "my_string_field "}}
40+
}
41+
});
3442

3543
Assert.DoesNotThrow(() =>
3644
{

src/Nest/Domain/Mapping/Types/NumberMapping.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ public class NumberMapping : IElasticType, IElasticCoreType
1212
[JsonIgnore]
1313
public TypeNameMarker TypeNameMarker { get; set; }
1414

15-
[JsonProperty(PropertyName = "name")]
16-
public string Name { get; set; }
15+
[JsonProperty(PropertyName = "name")]
16+
public string Name { get; set; }
1717

1818
private TypeNameMarker __type;
1919
[JsonProperty("type")]
2020
public virtual TypeNameMarker Type { get { return (TypeNameMarker)(__type ?? "double"); } set { __type = value; } }
2121

22-
[JsonProperty("similarity")]
23-
public string Similarity { get; set; }
22+
[JsonProperty("similarity")]
23+
public string Similarity { get; set; }
2424

2525
/// <summary>
2626
/// The name of the field that will be stored in the index. Defaults to the property/field name.

src/Nest/Resolvers/Converters/IndexSettingsConverter.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,15 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
110110
{
111111
writer.WritePropertyName("mappings");
112112
serializer.Serialize(writer,
113-
indexSettings.Mappings.ToDictionary(m => m.TypeNameMarker.Resolve(settings.ConnectionSettings)));
113+
indexSettings.Mappings.ToDictionary(m =>
114+
{
115+
if (m.Name.IsNullOrEmpty() && m.TypeNameMarker == null)
116+
throw new DslException("{0} should have a name!".F(m.GetType()));
117+
118+
119+
var fieldName = m.Name;
120+
return m.TypeNameMarker != null ? m.TypeNameMarker.Resolve(settings.ConnectionSettings) : fieldName;
121+
}));
114122
}
115123
}
116124
if (indexSettings.Warmers.Count > 0)

0 commit comments

Comments
 (0)