Skip to content

Commit 7219697

Browse files
committed
Merge branch 'sklirg-fix/4029-enum-get-type-throwing'
(cherry picked from commit 7d974ac)
1 parent d96f641 commit 7219697

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/Elasticsearch.Net/Utf8Json/Formatters/EnumFormatter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ public static object GetDeserializeDelegate(Type type, out bool isBoxed)
151151
// can inherit for set optimize manual serialize/deserialize func.
152152
internal class EnumFormatter<T> : IJsonFormatter<T>, IObjectPropertyNameFormatter<T>
153153
{
154-
readonly static ByteArrayStringHashTable<T> nameValueMapping;
155-
readonly static Dictionary<T, string> valueNameMapping;
154+
private static readonly ByteArrayStringHashTable<T> nameValueMapping;
155+
private static readonly Dictionary<T, string> valueNameMapping;
156156

157-
readonly static JsonSerializeAction<T> defaultSerializeByUnderlyingValue;
158-
readonly static JsonDeserializeFunc<T> defaultDeserializeByUnderlyingValue;
157+
private static readonly JsonSerializeAction<T> defaultSerializeByUnderlyingValue;
158+
private static readonly JsonDeserializeFunc<T> defaultDeserializeByUnderlyingValue;
159159

160160
static EnumFormatter()
161161
{
@@ -166,7 +166,7 @@ static EnumFormatter()
166166
foreach (var item in type.GetFields().Where(fi => fi.FieldType == type))
167167
{
168168
var value = item.GetValue(null);
169-
var name = Enum.GetName(type, value);
169+
var name = item.Name;
170170
var dataMember = item.GetCustomAttributes(typeof(DataMemberAttribute), true)
171171
.OfType<DataMemberAttribute>()
172172
.FirstOrDefault();

src/Tests/Tests/CodeStandards/Serialization/Enums.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
5+
using System.Net;
46
using System.Reflection;
57
using System.Runtime.Serialization;
8+
using System.Text;
69
using Elastic.Xunit.XunitPlumbing;
710
using Elasticsearch.Net;
811
using FluentAssertions;
912
using Nest;
13+
using Tests.Core.Serialization;
14+
using Tests.QueryDsl.Geo.Shape;
15+
using Tests.XPack.MachineLearning;
1016

1117
namespace Tests.CodeStandards.Serialization
1218
{
@@ -34,5 +40,48 @@ public void EnumsWithEnumMembersShouldBeMarkedWithStringEnumAttribute()
3440
}
3541
notMarkedStringEnum.Should().BeEmpty();
3642
}
43+
44+
[U]
45+
public void CanSerializeEnumsWithMultipleMembersMappedToSameValue()
46+
{
47+
var document = new EnumDocument
48+
{
49+
Int = HttpStatusCode.Moved,
50+
String = AnotherEnum.Value1
51+
};
52+
53+
var client = new ElasticClient();
54+
55+
var json = client.RequestResponseSerializer.SerializeToString(document);
56+
57+
// "Value2" will be written for both "Value1" and "Value2" because the underlying integer value
58+
// for both is the same, and "Value2" field member is listed after "Value1", overwriting
59+
// the value mapping.
60+
//
61+
// Json.Net behaves similarly, except the first string mapped for a value
62+
// is not overwritten i.e. "Value1" will be written for both "Value1" and "Value2"
63+
json.Should().Be("{\"int\":301,\"string\":\"Value2\"}");
64+
65+
EnumDocument deserializedDocument;
66+
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
67+
deserializedDocument = client.RequestResponseSerializer.Deserialize<EnumDocument>(stream);
68+
69+
deserializedDocument.Int.Should().Be(document.Int);
70+
deserializedDocument.String.Should().Be(document.String);
71+
}
72+
73+
private class EnumDocument
74+
{
75+
public HttpStatusCode Int { get;set;}
76+
77+
public AnotherEnum String { get; set; }
78+
}
79+
80+
[StringEnum]
81+
public enum AnotherEnum : int
82+
{
83+
Value1 = 1,
84+
Value2 = 1
85+
}
3786
}
3887
}

0 commit comments

Comments
 (0)