Skip to content

Commit

Permalink
Support multiple expand_wildcards (#5317)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stevejgordon authored Feb 11, 2021
1 parent 9251068 commit 35c12cd
Show file tree
Hide file tree
Showing 4 changed files with 771 additions and 5 deletions.
45 changes: 45 additions & 0 deletions src/Nest/XPack/Watcher/Input/ExpandWildcardsFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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.Collections.Generic;
using System.Linq;
using Elasticsearch.Net;
using Elasticsearch.Net.Utf8Json;

namespace Nest
{
internal class ExpandWildcardsFormatter : IJsonFormatter<IEnumerable<ExpandWildcards>>
{
public IEnumerable<ExpandWildcards> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
{
var token = reader.GetCurrentJsonToken();
return token == JsonToken.BeginArray
? formatterResolver.GetFormatter<IEnumerable<ExpandWildcards>>().Deserialize(ref reader, formatterResolver)
: new[] { formatterResolver.GetFormatter<ExpandWildcards>().Deserialize(ref reader, formatterResolver) };
}

public void Serialize(ref JsonWriter writer, IEnumerable<ExpandWildcards> value, IJsonFormatterResolver formatterResolver)
{
if (value == null)
{
writer.WriteNull();
return;
}

var wildcards = value.ToArray();

switch (wildcards.Length)
{
case 1:
var singleFormatter = formatterResolver.GetFormatter<ExpandWildcards>();
singleFormatter.Serialize(ref writer, wildcards.First(), formatterResolver);
break;
case > 1:
var formatter = formatterResolver.GetFormatter<IEnumerable<ExpandWildcards>>();
formatter.Serialize(ref writer, wildcards, formatterResolver);
break;
}
}
}
}
21 changes: 17 additions & 4 deletions src/Nest/XPack/Watcher/Input/IndicesOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Collections.Generic;
using System.Runtime.Serialization;
using Elasticsearch.Net;
using Elasticsearch.Net.Utf8Json;
Expand All @@ -15,28 +16,40 @@ public interface IIndicesOptions
[DataMember(Name = "allow_no_indices")]
bool? AllowNoIndices { get; set; }

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

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

[DataContract]

public class IndicesOptions : IIndicesOptions
{
public bool? AllowNoIndices { get; set; }
public ExpandWildcards? ExpandWildcards { get; set; }
/// <inheritdoc />
public IEnumerable<ExpandWildcards> ExpandWildcards { get; set; }
public bool? IgnoreUnavailable { get; set; }
}

public class IndicesOptionsDescriptor : DescriptorBase<IndicesOptionsDescriptor, IIndicesOptions>, IIndicesOptions
{
bool? IIndicesOptions.AllowNoIndices { get; set; }
ExpandWildcards? IIndicesOptions.ExpandWildcards { get; set; }
IEnumerable<ExpandWildcards> IIndicesOptions.ExpandWildcards { get; set; }
bool? IIndicesOptions.IgnoreUnavailable { get; set; }

public IndicesOptionsDescriptor ExpandWildcards(ExpandWildcards? expandWildcards) =>
/// <inheritdoc cref="IIndicesOptions.ExpandWildcards"/>
public IndicesOptionsDescriptor ExpandWildcards(IEnumerable<ExpandWildcards> expandWildcards) =>
Assign(expandWildcards, (a, v) => a.ExpandWildcards = v);

/// <inheritdoc cref="IIndicesOptions.ExpandWildcards"/>
public IndicesOptionsDescriptor ExpandWildcards(params ExpandWildcards[] expandWildcards) =>
Assign(expandWildcards, (a, v) => a.ExpandWildcards = v);

public IndicesOptionsDescriptor IgnoreUnavailable(bool? ignoreUnavailable = true) =>
Expand Down
1 change: 1 addition & 0 deletions tests/Tests/XPack/Watcher/GetWatch/GetWatchApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public static void PutWatch(IElasticClient client, CallUniqueValues values)
.Indices(typeof(Project))
.SearchType(SearchType.DfsQueryThenFetch)
.IndicesOptions(io => io
.AllowNoIndices()
.ExpandWildcards(ExpandWildcards.Open)
.IgnoreUnavailable()
)
Expand Down
Loading

0 comments on commit 35c12cd

Please sign in to comment.