Skip to content

Commit 2a02b71

Browse files
committed
Merge pull request #1480 from elastic/fix/1479
Fix #1479: Full integral type coverage for field type serialization
2 parents 251bd71 + a0c0ec2 commit 2a02b71

File tree

5 files changed

+111
-21
lines changed

5 files changed

+111
-21
lines changed

src/Nest/Enums/FieldType.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ public enum FieldType
5959
[EnumMember(Value = "long")]
6060
Long,
6161
/// <summary>
62+
/// Short type.
63+
/// </summary>
64+
[EnumMember(Value = "short")]
65+
Short,
66+
/// <summary>
67+
/// Byte type.
68+
/// </summary>
69+
[EnumMember(Value = "byte")]
70+
Byte,
71+
/// <summary>
6272
/// Float type.
6373
/// </summary>
6474
[EnumMember(Value = "float")]

src/Nest/Resolvers/Writers/TypeMappingWriter.cs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ internal RootObjectMapping RootObjectMappingFromAttributes()
8585
using (var ms = new MemoryStream(nestedJson.Utf8Bytes()))
8686
return this._elasticSerializer.Deserialize<RootObjectMapping>(ms);
8787
}
88+
8889
internal ObjectMapping ObjectMappingFromAttributes()
8990
{
9091
var json = JObject.Parse(this.MapFromAttributes());
@@ -93,6 +94,7 @@ internal ObjectMapping ObjectMappingFromAttributes()
9394
using (var ms = new MemoryStream(nestedJson.Utf8Bytes()))
9495
return this._elasticSerializer.Deserialize<ObjectMapping>(ms);
9596
}
97+
9698
internal NestedObjectMapping NestedObjectMappingFromAttributes()
9799
{
98100
var json = JObject.Parse(this.MapFromAttributes());
@@ -101,6 +103,7 @@ internal NestedObjectMapping NestedObjectMappingFromAttributes()
101103
using (var ms = new MemoryStream(nestedJson.Utf8Bytes()))
102104
return this._elasticSerializer.Deserialize<NestedObjectMapping>(ms);
103105
}
106+
104107
public string MapFromAttributes()
105108
{
106109
var sb = new StringBuilder();
@@ -139,7 +142,7 @@ internal void WriteProperties(JsonWriter jsonWriter)
139142
continue;
140143

141144
var propertyName = this.Infer.PropertyName(p);
142-
var type = GetElasticSearchType(att, p);
145+
var type = GetElasticsearchTypeName(att, p);
143146

144147
if (type == null) //could not get type from attribute or infer from CLR type.
145148
continue;
@@ -182,38 +185,37 @@ private void WritePropertiesFromAttribute(JsonWriter jsonWriter, IElasticPropert
182185
}
183186

184187
/// <summary>
185-
/// Get the Elastic Search Field Type Related.
188+
/// Gets the Elasticsearch type name for a given ElasticPropertyAttribute.
186189
/// </summary>
187-
/// <param name="att">ElasticPropertyAttribute</param>
188-
/// <param name="p">Property Field</param>
189-
/// <returns>String with the type name or null if can not be inferres</returns>
190-
private string GetElasticSearchType(IElasticPropertyAttribute att, PropertyInfo p)
190+
/// <param name="attribute">ElasticPropertyAttribute</param>
191+
/// <param name="propertyInfo">Property field</param>
192+
/// <returns>String containing the type name, or null if can not be inferred.</returns>
193+
private string GetElasticsearchTypeName(IElasticPropertyAttribute attribute, PropertyInfo propertyInfo)
191194
{
192195
FieldType? fieldType = null;
193-
if (att != null)
194-
{
195-
fieldType = att.Type;
196-
}
196+
197+
if (attribute != null)
198+
fieldType = attribute.Type;
197199

198200
if (fieldType == null || fieldType == FieldType.None)
199201
{
200-
fieldType = this.GetFieldTypeFromType(p.PropertyType);
201-
if (fieldType == null && att != null)
202+
fieldType = this.GetFieldType(propertyInfo.PropertyType);
203+
if (fieldType == null && attribute != null)
202204
{
203-
var message = _noFieldTypeMessage.F(p.Name, this._type.Name);
205+
var message = _noFieldTypeMessage.F(propertyInfo.Name, this._type.Name);
204206
throw new DslException(message);
205207
}
206208
}
207209

208-
return this.GetElasticSearchTypeFromFieldType(fieldType);
210+
return this.GetElasticsearchType(fieldType);
209211
}
210212

211213
/// <summary>
212-
/// Get the Elastic Search Field from a FieldType.
214+
/// Gets the Elasticsearch type name for a given FieldType.
213215
/// </summary>
214216
/// <param name="fieldType">FieldType</param>
215-
/// <returns>String with the type name or null if can not be inferres</returns>
216-
private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
217+
/// <returns>String containing the type name, or null if can not be inferred.</returns>
218+
private string GetElasticsearchType(FieldType? fieldType)
217219
{
218220
switch (fieldType)
219221
{
@@ -231,6 +233,10 @@ private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
231233
return "string";
232234
case FieldType.Integer:
233235
return "integer";
236+
case FieldType.Short:
237+
return "short";
238+
case FieldType.Byte:
239+
return "byte";
234240
case FieldType.Long:
235241
return "long";
236242
case FieldType.Float:
@@ -255,11 +261,11 @@ private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
255261
}
256262

257263
/// <summary>
258-
/// Inferes the FieldType from the type of the property.
264+
/// Gets the FieldType for a given CLR type.
259265
/// </summary>
260-
/// <param name="propertyType">Type of the property</param>
261-
/// <returns>FieldType or null if can not be inferred</returns>
262-
private FieldType? GetFieldTypeFromType(Type propertyType)
266+
/// <param name="propertyType">CLR type of the property</param>
267+
/// <returns>The FieldType, or null if can not be inferred.</returns>
268+
private FieldType? GetFieldType(Type propertyType)
263269
{
264270
propertyType = GetUnderlyingType(propertyType);
265271

@@ -274,8 +280,16 @@ private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
274280
switch (propertyType.Name)
275281
{
276282
case "Int32":
283+
case "UInt32":
277284
return FieldType.Integer;
285+
case "Int16":
286+
case "UInt16":
287+
return FieldType.Short;
288+
case "Byte":
289+
case "SByte":
290+
return FieldType.Byte;
278291
case "Int64":
292+
case "UInt64":
279293
return FieldType.Long;
280294
case "Single":
281295
return FieldType.Float;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using NUnit.Framework;
7+
8+
namespace Nest.Tests.Unit.Core.AttributeBasedMap
9+
{
10+
[TestFixture]
11+
public class AllTypesTest : BaseAttributeMappingTests
12+
{
13+
public class AllTypes
14+
{
15+
public int IntegerField { get; set; }
16+
public short ShortField { get; set; }
17+
public byte ByteField { get; set; }
18+
public long LongField { get; set; }
19+
public float FloatField { get; set; }
20+
public double DoubleField { get; set; }
21+
public DateTime DateField { get; set; }
22+
public bool BoolField { get; set; }
23+
}
24+
25+
[Test]
26+
public void TestAllTypes()
27+
{
28+
var json = this.CreateMapFor<AllTypes>();
29+
this.JsonEquals(json, System.Reflection.MethodInfo.GetCurrentMethod());
30+
}
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"alltypes": {
3+
"properties": {
4+
"integerField": {
5+
"type": "integer"
6+
},
7+
"shortField": {
8+
"type": "short"
9+
},
10+
"byteField": {
11+
"type": "byte"
12+
},
13+
"longField": {
14+
"type": "long"
15+
},
16+
"floatField": {
17+
"type": "float"
18+
},
19+
"doubleField": {
20+
"type": "double"
21+
},
22+
"dateField": {
23+
"type": "date"
24+
},
25+
"boolField": {
26+
"type": "boolean"
27+
}
28+
}
29+
}
30+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<Compile Include="Cluster\NodeTests.cs" />
101101
<Compile Include="Cluster\Reroute\RerouteTests.cs" />
102102
<Compile Include="Cluster\State\StateTests.cs" />
103+
<Compile Include="Core\AttributeBasedMap\AllTypesTest.cs" />
103104
<Compile Include="Core\AttributeBasedMap\IncludeInParentTests.cs" />
104105
<Compile Include="Core\Bulk\BulkTests.cs" />
105106
<Compile Include="Core\Bulk\BulkUrlTests.cs" />
@@ -120,6 +121,9 @@
120121
<None Include="Cluster\Reroute\ClusterReroute.json">
121122
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
122123
</None>
124+
<None Include="Core\AttributeBasedMap\TestAllTypes.json">
125+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
126+
</None>
123127
<None Include="Core\AttributeBasedMap\TestIncludeInParent.json">
124128
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
125129
</None>

0 commit comments

Comments
 (0)