Skip to content

Commit c3cdbd2

Browse files
stevejgordongithub-actions[bot]
authored andcommitted
Fix MultiGet response deserialization for non-matched IDs (#7181)
* Correct handling for non-matched multi get IDs * Fix license header * Fix converter logic by switching order * Update integration jobs for latest versions
1 parent 8cb751c commit c3cdbd2

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

.github/workflows/integration-jobs.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ jobs:
2727
'8.3.3',
2828
'8.4.3',
2929
"8.5.3",
30-
'8.6.0-SNAPSHOT',
30+
'8.6.1',
31+
"8.7.0-SNAPSHOT"
3132
'latest-8'
3233
]
3334

src/Elastic.Clients.Elasticsearch/Serialization/ResponseItemConverterFactory.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,32 @@ private sealed class ResponseItemConverter<TDocument> : JsonConverter<MultiGetRe
4040

4141
try
4242
{
43-
var result = JsonSerializer.Deserialize<GetResult<TDocument>>(ref readerCopy, options);
43+
var result = JsonSerializer.Deserialize<MultiGetError>(ref reader, options);
4444

45-
// If we have a version number, we can be sure this isn't an error
46-
if (result is not null && result.Version is not null)
45+
if (result is not null && result.Error is not null)
4746
{
48-
reader = readerCopy; // Ensure we swap the reader to reflect the data we have consumed.
4947
return new MultiGetResponseItem<TDocument>(result);
5048
}
5149
}
5250
catch (Exception ex)
5351
{
54-
getResultException = ex;
52+
errorException = ex;
5553
}
5654

5755
try
5856
{
59-
var result = JsonSerializer.Deserialize<MultiGetError>(ref reader, options);
57+
var result = JsonSerializer.Deserialize<GetResult<TDocument>>(ref readerCopy, options);
6058

61-
if (result is not null && result.Error is not null)
59+
// If we have a version number, we can be sure this isn't an error
60+
if (result is not null)
6261
{
62+
reader = readerCopy; // Ensure we swap the reader to reflect the data we have consumed.
6363
return new MultiGetResponseItem<TDocument>(result);
6464
}
6565
}
6666
catch (Exception ex)
6767
{
68-
errorException = ex;
68+
getResultException = ex;
6969
}
7070

7171
Exception innerException = null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Linq;
6+
using Tests.Serialization;
7+
using Xunit;
8+
9+
namespace Tests.Document.Multiple.MGet;
10+
11+
public class MGetResponseSerialization : SerializerTestBase
12+
{
13+
private const string ResponseJson = @"{
14+
""docs"": [
15+
{
16+
""_index"": ""foos"",
17+
""_id"": ""001"",
18+
""_version"": 5,
19+
""_seq_no"": 8,
20+
""_primary_term"": 1,
21+
""found"": true,
22+
""_source"": {
23+
""id"": ""001"",
24+
""name"": ""FooA""
25+
}
26+
},
27+
{
28+
""_index"": ""foos"",
29+
""_id"": ""002"",
30+
""_version"": 5,
31+
""_seq_no"": 9,
32+
""_primary_term"": 1,
33+
""found"": true,
34+
""_source"": {
35+
""id"": ""002"",
36+
""name"": ""FooB""
37+
}
38+
},
39+
{
40+
""_index"": ""foos"",
41+
""_id"": ""nonexistant"",
42+
""found"": false
43+
}
44+
]
45+
}";
46+
47+
[U]
48+
public void MultiGetResponse_DeserializesCorrectly_WhenIdsAreNotFound()
49+
{
50+
var response = DeserializeJsonString<MultiGetResponse<Foo>>(ResponseJson);
51+
52+
response.Docs.Should().HaveCount(3);
53+
response.Docs.ElementAt(0).Match(r => r.Found.Should().BeTrue(), _ => Assert.Fail("Union item should not have matched."));
54+
response.Docs.ElementAt(1).Match(r => r.Found.Should().BeTrue(), _ => Assert.Fail("Union item should not have matched."));
55+
response.Docs.ElementAt(2).Match(r => r.Found.Should().BeFalse(), _ => Assert.Fail("Union item should not have matched."));
56+
}
57+
}
58+
59+
internal class Foo
60+
{
61+
public string Id { get; set; }
62+
public string Name { get; set; }
63+
}

0 commit comments

Comments
 (0)