Skip to content

[Backport 8.0] Fix MultiGet response deserialization for non-matched IDs #7183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/integration-jobs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ jobs:
'8.3.3',
'8.4.3',
"8.5.3",
'8.6.0-SNAPSHOT',
'8.6.1',
"8.7.0-SNAPSHOT"
'latest-8'
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,32 @@ private sealed class ResponseItemConverter<TDocument> : JsonConverter<MultiGetRe

try
{
var result = JsonSerializer.Deserialize<GetResult<TDocument>>(ref readerCopy, options);
var result = JsonSerializer.Deserialize<MultiGetError>(ref reader, options);

// If we have a version number, we can be sure this isn't an error
if (result is not null && result.Version is not null)
if (result is not null && result.Error is not null)
{
reader = readerCopy; // Ensure we swap the reader to reflect the data we have consumed.
return new MultiGetResponseItem<TDocument>(result);
}
}
catch (Exception ex)
{
getResultException = ex;
errorException = ex;
}

try
{
var result = JsonSerializer.Deserialize<MultiGetError>(ref reader, options);
var result = JsonSerializer.Deserialize<GetResult<TDocument>>(ref readerCopy, options);

if (result is not null && result.Error is not null)
// If we have a version number, we can be sure this isn't an error
if (result is not null)
{
reader = readerCopy; // Ensure we swap the reader to reflect the data we have consumed.
return new MultiGetResponseItem<TDocument>(result);
}
}
catch (Exception ex)
{
errorException = ex;
getResultException = ex;
}

Exception innerException = null;
Expand Down
63 changes: 63 additions & 0 deletions tests/Tests/Document/Multiple/MGet/MGetResponseSerialization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System.Linq;
using Tests.Serialization;
using Xunit;

namespace Tests.Document.Multiple.MGet;

public class MGetResponseSerialization : SerializerTestBase
{
private const string ResponseJson = @"{
""docs"": [
{
""_index"": ""foos"",
""_id"": ""001"",
""_version"": 5,
""_seq_no"": 8,
""_primary_term"": 1,
""found"": true,
""_source"": {
""id"": ""001"",
""name"": ""FooA""
}
},
{
""_index"": ""foos"",
""_id"": ""002"",
""_version"": 5,
""_seq_no"": 9,
""_primary_term"": 1,
""found"": true,
""_source"": {
""id"": ""002"",
""name"": ""FooB""
}
},
{
""_index"": ""foos"",
""_id"": ""nonexistant"",
""found"": false
}
]
}";

[U]
public void MultiGetResponse_DeserializesCorrectly_WhenIdsAreNotFound()
{
var response = DeserializeJsonString<MultiGetResponse<Foo>>(ResponseJson);

response.Docs.Should().HaveCount(3);
response.Docs.ElementAt(0).Match(r => r.Found.Should().BeTrue(), _ => Assert.Fail("Union item should not have matched."));
response.Docs.ElementAt(1).Match(r => r.Found.Should().BeTrue(), _ => Assert.Fail("Union item should not have matched."));
response.Docs.ElementAt(2).Match(r => r.Found.Should().BeFalse(), _ => Assert.Fail("Union item should not have matched."));
}
}

internal class Foo
{
public string Id { get; set; }
public string Name { get; set; }
}