|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.IO; |
3 | 4 | using System.Linq; |
| 5 | +using System.Net; |
4 | 6 | using System.Reflection; |
5 | 7 | using System.Runtime.Serialization; |
| 8 | +using System.Text; |
6 | 9 | using Elastic.Xunit.XunitPlumbing; |
7 | 10 | using Elasticsearch.Net; |
8 | 11 | using FluentAssertions; |
9 | 12 | using Nest; |
| 13 | +using Tests.Core.Serialization; |
| 14 | +using Tests.QueryDsl.Geo.Shape; |
| 15 | +using Tests.XPack.MachineLearning; |
10 | 16 |
|
11 | 17 | namespace Tests.CodeStandards.Serialization |
12 | 18 | { |
@@ -34,5 +40,48 @@ public void EnumsWithEnumMembersShouldBeMarkedWithStringEnumAttribute() |
34 | 40 | } |
35 | 41 | notMarkedStringEnum.Should().BeEmpty(); |
36 | 42 | } |
| 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 | + } |
37 | 86 | } |
38 | 87 | } |
0 commit comments