Skip to content

Commit ed65a70

Browse files
committed
MapFromAttributes() now throws if an ElasticProperty is set on a value type it can not infer a FieldType for and none is set either
1 parent 9325b7f commit ed65a70

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

src/Nest/Resolvers/Writers/TypeMappingWriter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public class TypeMappingWriter
1515
private readonly Type _type;
1616
private readonly IConnectionSettingsValues _connectionSettings;
1717
private readonly NestSerializer _elasticSerializer;
18+
19+
private readonly static string _noFieldTypeMessage =
20+
"Property {0} on type {1} has an ElasticProperty attribute but its FieldType (Type = ) can not be inferred and is not set explicitly while calling MapFromAttributes";
21+
1822
private ElasticInferrer Infer { get; set; }
1923

2024
private int MaxRecursion { get; set; }
@@ -194,6 +198,11 @@ private string GetElasticSearchType(IElasticPropertyAttribute att, PropertyInfo
194198
if (fieldType == null || fieldType == FieldType.None)
195199
{
196200
fieldType = this.GetFieldTypeFromType(p.PropertyType);
201+
if (fieldType == null && att != null)
202+
{
203+
var message = _noFieldTypeMessage.F(p.Name, this._type.Name);
204+
throw new DslException(message);
205+
}
197206
}
198207

199208
return this.GetElasticSearchTypeFromFieldType(fieldType);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using FluentAssertions;
9+
10+
namespace Nest.Tests.Unit.Core.Map.Structs
11+
{
12+
[TestFixture]
13+
public class StructMappingTests : BaseJsonTests
14+
{
15+
private class MyClass
16+
{
17+
public MyStruct Struct { get; set; }
18+
}
19+
20+
private class MyClass2
21+
{
22+
[ElasticProperty(Analyzer = "default")]
23+
public MyStruct StructProperty { get; set; }
24+
}
25+
26+
private struct MyStruct
27+
{
28+
public string Object { get; set; }
29+
}
30+
31+
[Test]
32+
public void StructWithNoAttributeSetIsIgnored()
33+
{
34+
//unknow value types are not handled by default by MapFromAttributes()
35+
var result = this._client.Map<MyClass>(m => m.MapFromAttributes());
36+
this.JsonEquals(result.ConnectionStatus.Request, MethodBase.GetCurrentMethod());
37+
}
38+
39+
[Test]
40+
public void StructWithAttributeButNoTypeInformationThrows()
41+
{
42+
//unknown value types with missing FieldType information in the attribute should throw an exception
43+
44+
//example
45+
46+
//Nest.DslException : Property Struct on type MyClass2 <continued>
47+
//has an ElasticProperty attribute but its FieldType (Type = ) can not be inferred <continued>
48+
//and is not set explicitly while calling MapFromAttributes
49+
50+
var e = Assert.Throws<DslException>(() =>
51+
{
52+
var result = this._client.Map<MyClass2>(m => m.MapFromAttributes());
53+
this.JsonEquals(result.ConnectionStatus.Request, MethodBase.GetCurrentMethod());
54+
});
55+
56+
e.Message.Should().EndWith("while calling MapFromAttributes");
57+
e.Message.Should().Contain("StructProperty");
58+
}
59+
60+
}
61+
62+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"myclass2": {
3+
"properties": {
4+
}
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"myclass": {
3+
"properties": {
4+
}
5+
}
6+
}

0 commit comments

Comments
 (0)