Skip to content

Commit 7205e58

Browse files
Do not wrap null ISourceFilter in Union (#4807) (#4823)
This commit updates the .Source() method to not wrap a null ISourceFilter in Union<bool,ISourceFilter>, so that it is not serialized in the request. Fixes #4787 Co-authored-by: Russ Cam <russ.cam@elastic.co>
1 parent 4cb20de commit 7205e58

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/Nest/Search/Search/SearchRequest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ public SearchDescriptor<TInferDocument> Aggregations(AggregationDictionary aggre
298298

299299
/// <inheritdoc cref="ISearchRequest.Source" />
300300
public SearchDescriptor<TInferDocument> Source(Func<SourceFilterDescriptor<TInferDocument>, ISourceFilter> selector) =>
301-
Assign(selector, (a, v) => a.Source = new Union<bool, ISourceFilter>(v?.Invoke(new SourceFilterDescriptor<TInferDocument>())));
301+
Assign(selector?.Invoke(new SourceFilterDescriptor<TInferDocument>()), (a, v) => a.Source = v is null
302+
? null
303+
: new Union<bool,ISourceFilter>(v));
302304

303305
/// <inheritdoc cref="ISearchRequest.Size" />
304306
public SearchDescriptor<TInferDocument> Size(int? size) => Assign(size, (a, v) => a.Size = v);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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;
6+
using System.Text;
7+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
8+
using Elasticsearch.Net;
9+
using FluentAssertions;
10+
using Nest;
11+
using Tests.Core.Client;
12+
13+
namespace Tests.Reproduce
14+
{
15+
public class GithubIssue4787
16+
{
17+
[U]
18+
public void DoNotSerializeNullSourceFilter()
19+
{
20+
var connectionSettings = new ConnectionSettings(new InMemoryConnection()).DisableDirectStreaming();
21+
var client = new ElasticClient(connectionSettings);
22+
23+
Func<ISearchResponse<object>> action = () =>
24+
client.Search<object>(s => s
25+
.Query(q => q
26+
.MatchAll()
27+
)
28+
.Index("index")
29+
.Source(sfd => null)
30+
);
31+
32+
var response = action.Should().NotThrow().Subject;
33+
34+
var json = Encoding.UTF8.GetString(response.ApiCall.RequestBodyInBytes);
35+
json.Should().Be(@"{""query"":{""match_all"":{}}}");
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)