Skip to content

Commit 4cf8fe7

Browse files
committed
fixes enums being skipped when calling MapFromAttributes()
1 parent 4e628c1 commit 4cf8fe7

File tree

6 files changed

+88
-6
lines changed

6 files changed

+88
-6
lines changed

src/Nest/Resolvers/Writers/TypeMappingWriter.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ internal void WriteProperties(JsonWriter jsonWriter)
136136

137137
var propertyName = this.Infer.PropertyName(p);
138138
var type = GetElasticSearchType(att, p);
139-
139+
140140
if (type == null) //could not get type from attribute or infer from CLR type.
141141
continue;
142142

@@ -234,8 +234,8 @@ private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
234234
return "boolean";
235235
case FieldType.Completion:
236236
return "completion";
237-
case FieldType.Nested:
238-
return "nested";
237+
case FieldType.Nested:
238+
return "nested";
239239
case FieldType.Object:
240240
return "object";
241241
default:
@@ -255,6 +255,9 @@ private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
255255
if (propertyType == typeof(string))
256256
return FieldType.String;
257257

258+
if (propertyType.IsEnum)
259+
return FieldType.Integer;
260+
258261
if (propertyType.IsValueType)
259262
{
260263
switch (propertyType.Name)
@@ -284,7 +287,7 @@ private static Type GetUnderlyingType(Type type)
284287
if (type.IsArray)
285288
return type.GetElementType();
286289

287-
if (type.IsGenericType && type.GetGenericArguments().Length == 1 && (type.GetInterface("IEnumerable") != null || Nullable.GetUnderlyingType(type) != null))
290+
if (type.IsGenericType && type.GetGenericArguments().Length == 1 && (type.GetInterface("IEnumerable") != null || Nullable.GetUnderlyingType(type) != null))
288291
return type.GetGenericArguments()[0];
289292

290293
return type;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"myclass": {
3+
"properties": {
4+
"myEnum": {
5+
"index": "not_analyzed",
6+
"type": "string"
7+
}
8+
}
9+
}
10+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
9+
namespace Nest.Tests.Unit.Core.Map.Enums
10+
{
11+
[TestFixture]
12+
public class EnumMappingTests : BaseJsonTests
13+
{
14+
private class MyClass
15+
{
16+
public MyEnum MyEnum { get; set; }
17+
}
18+
19+
private enum MyEnum
20+
{
21+
Value1,
22+
Value2
23+
}
24+
25+
[Test]
26+
public void EnumShouldMapToIntByDefault()
27+
{
28+
var result = this._client.Map<MyClass>(m => m.MapFromAttributes());
29+
this.JsonEquals(result.ConnectionStatus.Request, MethodBase.GetCurrentMethod());
30+
}
31+
32+
[Test]
33+
public void EnumCanBeOverriddenAfterMapFromAttributes()
34+
{
35+
var result = this._client.Map<MyClass>(m => m
36+
.MapFromAttributes()
37+
.Properties(props=>props
38+
.String(s=>s
39+
.Name(p=>p.MyEnum)
40+
.Index(FieldIndexOption.NotAnalyzed)
41+
)
42+
)
43+
);
44+
this.JsonEquals(result.ConnectionStatus.Request, MethodBase.GetCurrentMethod());
45+
}
46+
47+
}
48+
49+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"myclass": {
3+
"properties": {
4+
"myEnum": {
5+
"type": "integer"
6+
}
7+
}
8+
}
9+
}

src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<Compile Include="Core\Indices\Analysis\TokenFilters\TokenFilterTests.cs" />
108108
<Compile Include="Core\Indices\Similarity\SimilarityTests.cs" />
109109
<Compile Include="Core\Map\CustomMapping\ImagePluginMappingTests.cs" />
110+
<Compile Include="Core\Map\Enums\EnumMappingTests.cs" />
110111
<Compile Include="Core\Map\GenericTypes\GenericTypeMappingTests.cs" />
111112
<Compile Include="Core\Map\GeoShape\GeoShapeMappingTests.cs" />
112113
<Compile Include="Core\Map\GetMappingSerializationTests.cs" />
@@ -176,6 +177,12 @@
176177
<None Include="Core\Map\CustomMapping\RepresentUnknowImageMappingPlugin.json">
177178
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
178179
</None>
180+
<None Include="Core\Map\Enums\EnumCanBeOverriddenAfterMapFromAttributes.json">
181+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
182+
</None>
183+
<None Include="Core\Map\Enums\EnumShouldMapToIntByDefault.json">
184+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
185+
</None>
179186
<None Include="Core\Map\GenericTypes\GenericTypeMapping.json">
180187
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
181188
</None>
@@ -392,7 +399,7 @@
392399
<Compile Include="Reproduce\Reproduce1146Tests.cs" />
393400
<Compile Include="Reproduce\Reproduce629Tests.cs" />
394401
<Compile Include="Reproduce\Reproduce1187Tests.cs" />
395-
<Compile Include="Reproduce\Reproduce991Tests.cs" />
402+
<Compile Include="Reproduce\Reproduce901Tests.cs" />
396403
<Compile Include="Reproduce\Reproduce990Tests.cs" />
397404
<Compile Include="Reproduce\Reproduce974Tests.cs" />
398405
<Compile Include="Reproduce\Reproduce928Tests.cs" />
@@ -819,6 +826,9 @@
819826
<None Include="Reproduce\Issue1199Mapping.json">
820827
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
821828
</None>
829+
<None Include="Reproduce\EnumQueryDefaultsToInt.json">
830+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
831+
</None>
822832
<None Include="Reproduce\Issue928.json">
823833
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
824834
</None>

src/Tests/Nest.Tests.Unit/Reproduce/Reproduce991Tests.cs renamed to src/Tests/Nest.Tests.Unit/Reproduce/Reproduce901Tests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Nest.Tests.Unit.Reproduce
1212
/// tests to reproduce reported errors
1313
/// </summary>
1414
[TestFixture]
15-
public class Reproduce991Tests : BaseJsonTests
15+
public class Reproduce901Tests : BaseJsonTests
1616
{
1717
private class MyClass
1818
{
@@ -32,5 +32,6 @@ public void EnumQueryDefaultsToInt()
3232
.Query(q => q.Term(p => p.MyEnum, MyEnum.Value2));
3333
this.JsonEquals(query, MethodBase.GetCurrentMethod());
3434
}
35+
3536
}
3637
}

0 commit comments

Comments
 (0)