Skip to content

Commit c3ca65c

Browse files
[Backport master] Support multiple expand_wildcards (#5319)
* Support multiple expand_wildcards (#5317) Since 7.10.1 the watcher expand_wildcards property has accepted and returns an array for expanded_wildcards. This broke in our existing representation of IndicesOptions during deserialisation. This change is breaking but now supports either a single string value or an array in the JSON response. * Fix up namespaces Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent 2c943a1 commit c3ca65c

File tree

4 files changed

+771
-5
lines changed

4 files changed

+771
-5
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.Collections.Generic;
6+
using System.Linq;
7+
using Elasticsearch.Net;
8+
using Nest.Utf8Json;
9+
10+
namespace Nest
11+
{
12+
internal class ExpandWildcardsFormatter : IJsonFormatter<IEnumerable<ExpandWildcards>>
13+
{
14+
public IEnumerable<ExpandWildcards> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
15+
{
16+
var token = reader.GetCurrentJsonToken();
17+
return token == JsonToken.BeginArray
18+
? formatterResolver.GetFormatter<IEnumerable<ExpandWildcards>>().Deserialize(ref reader, formatterResolver)
19+
: new[] { formatterResolver.GetFormatter<ExpandWildcards>().Deserialize(ref reader, formatterResolver) };
20+
}
21+
22+
public void Serialize(ref JsonWriter writer, IEnumerable<ExpandWildcards> value, IJsonFormatterResolver formatterResolver)
23+
{
24+
if (value == null)
25+
{
26+
writer.WriteNull();
27+
return;
28+
}
29+
30+
var wildcards = value.ToArray();
31+
32+
switch (wildcards.Length)
33+
{
34+
case 1:
35+
var singleFormatter = formatterResolver.GetFormatter<ExpandWildcards>();
36+
singleFormatter.Serialize(ref writer, wildcards.First(), formatterResolver);
37+
break;
38+
case > 1:
39+
var formatter = formatterResolver.GetFormatter<IEnumerable<ExpandWildcards>>();
40+
formatter.Serialize(ref writer, wildcards, formatterResolver);
41+
break;
42+
}
43+
}
44+
}
45+
}

src/Nest/XPack/Watcher/Input/IndicesOptions.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using System.Collections.Generic;
56
using System.Runtime.Serialization;
67
using Elasticsearch.Net;
78
using Nest.Utf8Json;
@@ -15,28 +16,40 @@ public interface IIndicesOptions
1516
[DataMember(Name = "allow_no_indices")]
1617
bool? AllowNoIndices { get; set; }
1718

19+
/// <summary>
20+
/// Determines how to expand indices wildcards.
21+
/// <para>NOTE: Elasticsearch 7.10.0 and prior supports only a single value. Elasticsearch 7.10.1 and later support multiple values.</para>
22+
/// </summary>
1823
[DataMember(Name = "expand_wildcards")]
19-
ExpandWildcards? ExpandWildcards { get; set; }
24+
[JsonFormatter(typeof(ExpandWildcardsFormatter))]
25+
IEnumerable<ExpandWildcards> ExpandWildcards { get; set; }
2026

2127
[DataMember(Name = "ignore_unavailable")]
2228
bool? IgnoreUnavailable { get; set; }
2329
}
2430

2531
[DataContract]
32+
2633
public class IndicesOptions : IIndicesOptions
2734
{
2835
public bool? AllowNoIndices { get; set; }
29-
public ExpandWildcards? ExpandWildcards { get; set; }
36+
/// <inheritdoc />
37+
public IEnumerable<ExpandWildcards> ExpandWildcards { get; set; }
3038
public bool? IgnoreUnavailable { get; set; }
3139
}
3240

3341
public class IndicesOptionsDescriptor : DescriptorBase<IndicesOptionsDescriptor, IIndicesOptions>, IIndicesOptions
3442
{
3543
bool? IIndicesOptions.AllowNoIndices { get; set; }
36-
ExpandWildcards? IIndicesOptions.ExpandWildcards { get; set; }
44+
IEnumerable<ExpandWildcards> IIndicesOptions.ExpandWildcards { get; set; }
3745
bool? IIndicesOptions.IgnoreUnavailable { get; set; }
3846

39-
public IndicesOptionsDescriptor ExpandWildcards(ExpandWildcards? expandWildcards) =>
47+
/// <inheritdoc cref="IIndicesOptions.ExpandWildcards"/>
48+
public IndicesOptionsDescriptor ExpandWildcards(IEnumerable<ExpandWildcards> expandWildcards) =>
49+
Assign(expandWildcards, (a, v) => a.ExpandWildcards = v);
50+
51+
/// <inheritdoc cref="IIndicesOptions.ExpandWildcards"/>
52+
public IndicesOptionsDescriptor ExpandWildcards(params ExpandWildcards[] expandWildcards) =>
4053
Assign(expandWildcards, (a, v) => a.ExpandWildcards = v);
4154

4255
public IndicesOptionsDescriptor IgnoreUnavailable(bool? ignoreUnavailable = true) =>

tests/Tests/XPack/Watcher/GetWatch/GetWatchApiTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public static void PutWatch(IElasticClient client, CallUniqueValues values)
118118
.Indices(typeof(Project))
119119
.SearchType(SearchType.DfsQueryThenFetch)
120120
.IndicesOptions(io => io
121+
.AllowNoIndices()
121122
.ExpandWildcards(ExpandWildcards.Open)
122123
.IgnoreUnavailable()
123124
)

0 commit comments

Comments
 (0)