Skip to content

Commit ccb8f88

Browse files
Add formatter for int -> string (#4541) (#4554)
This commit adds a formatter for converting from int to string. Using this formatter fixes a bug with SnapshotShardFailure that models ShardId as a string instead of an int. Fixes #4537 Co-authored-by: Russ Cam <[email protected]>
1 parent a4dfdc1 commit ccb8f88

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Globalization;
3+
using Elasticsearch.Net.Utf8Json;
4+
5+
namespace Nest
6+
{
7+
/// <summary>
8+
/// A formatter to deserialize an int into a string,
9+
/// and serialize a string into an int.
10+
/// </summary>
11+
internal class IntStringFormatter: IJsonFormatter<string>
12+
{
13+
public void Serialize(ref JsonWriter writer, string value, IJsonFormatterResolver formatterResolver)
14+
{
15+
if (int.TryParse(value, out var i))
16+
writer.WriteInt32(i);
17+
else
18+
throw new InvalidOperationException($"expected a int string value, but found {value}");
19+
}
20+
21+
public string Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
22+
{
23+
switch (reader.GetCurrentJsonToken())
24+
{
25+
case JsonToken.Number:
26+
return reader.ReadInt32().ToString(CultureInfo.InvariantCulture);
27+
case JsonToken.String:
28+
return reader.ReadString();
29+
default:
30+
throw new JsonParsingException($"expected string or int but found {reader.GetCurrentJsonToken()}");
31+
}
32+
}
33+
}
34+
}

src/Nest/Modules/SnapshotAndRestore/Snapshot/SnapshotShardFailure.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Runtime.Serialization;
2+
using Elasticsearch.Net.Utf8Json;
23

34
namespace Nest
45
{
@@ -15,6 +16,7 @@ public class SnapshotShardFailure
1516
public string Reason { get; set; }
1617

1718
[DataMember(Name ="shard_id")]
19+
[JsonFormatter(typeof(IntStringFormatter))]
1820
public string ShardId { get; set; }
1921

2022
[DataMember(Name ="status")]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text;
4+
using Elastic.Xunit.XunitPlumbing;
5+
using FluentAssertions;
6+
using Nest;
7+
using Tests.Core.Client;
8+
9+
namespace Tests.Reproduce
10+
{
11+
public class GitHubIssue4537
12+
{
13+
[U]
14+
public void CanDeserializeSnapshotShardFailure()
15+
{
16+
var json = @"{
17+
""snapshots"": [
18+
{
19+
""snapshot"": ""snapshot_2020-03-31t00:02:18z"",
20+
""uuid"": ""P9oZzuEfS8qbT-FVZLFSgw"",
21+
""version_id"": 7040299,
22+
""version"": ""7.4.2"",
23+
""indices"": [ ""someIndices"" ],
24+
""include_global_state"": true,
25+
""state"": ""FAILED"",
26+
""reason"": ""Indices don't have primary shards [someIndex]"",
27+
""start_time"": ""2020-03-31T00:02:21.478Z"",
28+
""start_time_in_millis"": 1585612941478,
29+
""end_time"": ""2020-03-31T00:02:25.353Z"",
30+
""end_time_in_millis"": 1585612945353,
31+
""duration_in_millis"": 3875,
32+
""failures"": [
33+
{
34+
""index"": ""someIndex"",
35+
""index_uuid"": ""someIndex"",
36+
""shard_id"": 1,
37+
""reason"": ""primary shard is not allocated"",
38+
""status"": ""INTERNAL_SERVER_ERROR""
39+
},
40+
{
41+
""index"": ""someIndex"",
42+
""index_uuid"": ""someIndex"",
43+
""shard_id"": 0,
44+
""reason"": ""primary shard is not allocated"",
45+
""status"": ""INTERNAL_SERVER_ERROR""
46+
},
47+
{
48+
""index"": ""someIndex"",
49+
""index_uuid"": ""someIndex"",
50+
""shard_id"": 2,
51+
""reason"": ""primary shard is not allocated"",
52+
""status"": ""INTERNAL_SERVER_ERROR""
53+
}
54+
],
55+
""shards"": {
56+
""total"": 78,
57+
""failed"": 3,
58+
""successful"": 75
59+
}
60+
}
61+
]
62+
}";
63+
64+
var client = TestClient.FixedInMemoryClient(Encoding.UTF8.GetBytes(json));
65+
66+
Func<GetSnapshotResponse> action = () => client.Snapshot.Get("repo", "snapshot_2020-03-31t00:02:18z");
67+
68+
action.Should().NotThrow();
69+
70+
var response = action();
71+
72+
var failures = response.Snapshots.First().Failures;
73+
failures.Should().HaveCount(3);
74+
failures.First().ShardId.Should().Be("1");
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)