Skip to content

Commit 30ac823

Browse files
authored
Fix/response ❤️ (#2383)
* Give all the responses some ❤️ As per #2299 be more consistent responses with returning empty collections vs null on responses. Now that we dropped .NET 4.0 we can make the response IEnumerable and Dictionaries IReadOnly* Made sure all the interfaces only define getters and not setters * another pass at all response objects * Make sure EmptyReadOnly returns actual ReadOnly* instances for the interfaces and not rely on implicit conversion. Fix sorts on covariant hits having trouble going from JArray to ReadOnlyCollection<object> * fix shard store response because Stores now needs an explicit JsonProperty
1 parent 26aaae0 commit 30ac823

File tree

116 files changed

+528
-537
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+528
-537
lines changed

src/Nest/Aggregations/Aggregate.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Nest
1111
[ExactContractJsonConverter(typeof(AggregateJsonConverter))]
1212
public interface IAggregate
1313
{
14-
IDictionary<string, object> Meta { get; set; }
14+
//TODO this public set is problematic
15+
IReadOnlyDictionary<string, object> Meta { get; set; }
1516
}
1617
}

src/Nest/Aggregations/AggregateJsonConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private IAggregate ReadAggregate(JsonReader reader, JsonSerializer serializer)
6161
case "buckets":
6262
case "doc_count_error_upper_bound":
6363
aggregate = GetMultiBucketAggregate(reader, serializer);
64-
break;;
64+
break;
6565
case "count":
6666
aggregate = GetStatsAggregate(reader, serializer);
6767
break;
@@ -342,7 +342,7 @@ private IAggregate GetExtendedStatsAggregate(StatsAggregate statsMetric, JsonRea
342342
return extendedStatsMetric;
343343
}
344344

345-
private IDictionary<string, IAggregate> GetSubAggregates(JsonReader reader, JsonSerializer serializer)
345+
private IReadOnlyDictionary<string, IAggregate> GetSubAggregates(JsonReader reader, JsonSerializer serializer)
346346
{
347347
if (reader.TokenType != JsonToken.PropertyName)
348348
return null;
@@ -405,7 +405,7 @@ private IAggregate GetMultiBucketAggregate(JsonReader reader, JsonSerializer ser
405405
if (reader.TokenType == JsonToken.EndArray)
406406
{
407407
reader.Read();
408-
bucket.Items = Enumerable.Empty<IBucket>();
408+
bucket.Items = EmptyReadOnly<IBucket>.Collection;
409409
return bucket;
410410
}
411411
do
@@ -553,7 +553,7 @@ private IBucket GetKeyedBucket(JsonReader reader, JsonSerializer serializer)
553553
if (property == "from" || property == "to")
554554
return GetRangeBucket(reader, serializer, key);
555555

556-
var keyItem = new KeyedBucket {Key = key};
556+
var keyItem = new KeyedBucket { Key = key };
557557

558558
if (property == "key_as_string")
559559
{

src/Nest/Aggregations/AggregationsHelper.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ namespace Nest
66
{
77
public class AggregationsHelper
88
{
9-
public IDictionary<string, IAggregate> Aggregations { get; protected internal set; }
9+
public IReadOnlyDictionary<string, IAggregate> Aggregations { get; protected internal set; } = EmptyReadOnly<string, IAggregate>.Dictionary;
1010

1111
public AggregationsHelper() { }
1212

13-
public AggregationsHelper(IDictionary<string, IAggregate> aggregations)
13+
protected AggregationsHelper(IDictionary<string, IAggregate> aggregations)
14+
{
15+
this.Aggregations = aggregations != null ?
16+
new Dictionary<string, IAggregate>(aggregations)
17+
: EmptyReadOnly<string, IAggregate>.Dictionary;
18+
}
19+
public AggregationsHelper(IReadOnlyDictionary<string, IAggregate> aggregations)
1420
{
15-
this.Aggregations = aggregations;
21+
this.Aggregations = aggregations ?? EmptyReadOnly<string, IAggregate>.Dictionary;
1622
}
1723

1824
public ValueAggregate Min(string key) => this.TryGet<ValueAggregate>(key);
@@ -164,11 +170,11 @@ public MultiBucketAggregate<HistogramBucket> Histogram(string key)
164170
public MatrixStatsAggregate MatrixStats(string key) => this.TryGet<MatrixStatsAggregate>(key);
165171

166172

167-
private TAggregation TryGet<TAggregation>(string key)
168-
where TAggregation : class, IAggregate
173+
private TAggregate TryGet<TAggregate>(string key)
174+
where TAggregate : class, IAggregate
169175
{
170176
IAggregate agg;
171-
return this.Aggregations.TryGetValue(key, out agg) ? agg as TAggregation : null;
177+
return this.Aggregations.TryGetValue(key, out agg) ? agg as TAggregate : null;
172178
}
173179

174180
private MultiBucketAggregate<TBucket> GetBucket<TBucket>(string key)

src/Nest/Aggregations/Bucket/BucketAggregate.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public abstract class BucketAggregateBase : AggregationsHelper, IAggregate
88
protected BucketAggregateBase() { }
99
protected BucketAggregateBase(IDictionary<string, IAggregate> aggregations) : base(aggregations) { }
1010

11-
public IDictionary<string, object> Meta { get; set; }
11+
public IReadOnlyDictionary<string, object> Meta { get; set; } = EmptyReadOnly<string, object>.Dictionary;
1212
}
1313

1414
public class MultiBucketAggregate<TBucket> : BucketAggregateBase
@@ -17,7 +17,7 @@ public class MultiBucketAggregate<TBucket> : BucketAggregateBase
1717
public MultiBucketAggregate() { }
1818
public MultiBucketAggregate(IDictionary<string, IAggregate> aggregations) : base(aggregations) { }
1919

20-
public IList<TBucket> Buckets { get; set; }
20+
public IReadOnlyCollection<TBucket> Buckets { get; set; } = EmptyReadOnly<TBucket>.Collection;
2121
}
2222

2323
public class SingleBucketAggregate : BucketAggregateBase
@@ -31,10 +31,10 @@ public SingleBucketAggregate(IDictionary<string, IAggregate> aggregations) : bas
3131
// Intermediate object used for deserialization
3232
public class BucketAggregate : IAggregate
3333
{
34-
public IEnumerable<IBucket> Items { get; set; }
34+
public IReadOnlyCollection<IBucket> Items { get; set; } = EmptyReadOnly<IBucket>.Collection;
3535
public long? DocCountErrorUpperBound { get; set; }
3636
public long? SumOtherDocCount { get; set; }
37-
public IDictionary<string, object> Meta { get; set; }
37+
public IReadOnlyDictionary<string, object> Meta { get; set; } = EmptyReadOnly<string, object>.Dictionary;
3838
public long DocCount { get; set; }
3939
}
4040
}

src/Nest/Aggregations/Matrix/MatrixAggregate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ namespace Nest
88
{
99
public abstract class MatrixAggregateBase : IAggregate
1010
{
11-
public IDictionary<string, object> Meta { get; set; }
11+
public IReadOnlyDictionary<string, object> Meta { get; set; } = EmptyReadOnly<string, object>.Dictionary;
1212
}
1313
}

src/Nest/Aggregations/Metric/MetricAggregate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace Nest
55
{
66
public abstract class MetricAggregateBase : IAggregate
77
{
8-
public IDictionary<string, object> Meta { get; set; }
8+
public IReadOnlyDictionary<string, object> Meta { get; set; } = EmptyReadOnly<string, object>.Dictionary;
99
}
1010
}

src/Nest/Aggregations/Metric/TopHits/TopHitsAggregate.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,33 @@ public class TopHitsAggregate : MetricAggregateBase
1616
public double? MaxScore { get; set; }
1717

1818
public TopHitsAggregate() { }
19-
19+
2020
internal TopHitsAggregate(IEnumerable<JObject> hits)
2121
{
22-
_hits = hits;
22+
_hits = hits ?? Enumerable.Empty<JObject>();
2323
}
2424

2525
internal TopHitsAggregate(IEnumerable<JObject> hits, JsonSerializer serializer)
2626
{
27-
_hits = hits;
27+
_hits = hits ?? Enumerable.Empty<JObject>();;
2828
_defaultSerializer = serializer;
2929
}
3030

31-
public IEnumerable<Hit<T>> Hits<T>(JsonSerializer serializer = null)
31+
private IEnumerable<Hit<T>> ConvertHits<T>(JsonSerializer serializer = null)
3232
where T : class
3333
{
3434
var s = serializer ?? _defaultSerializer;
3535

36-
return s != null
37-
? _hits.Select(h => h.ToObject<Hit<T>>(s))
36+
return s != null
37+
? _hits.Select(h => h.ToObject<Hit<T>>(s))
3838
: _hits.Select(h => h.ToObject<Hit<T>>());
3939
}
4040

41-
public IEnumerable<T> Documents<T>(JsonSerializer serializer = null) where T : class =>
42-
this.Hits<T>(serializer).Select(h => h.Source);
41+
public IReadOnlyCollection<Hit<T>> Hits<T>(JsonSerializer serializer = null)
42+
where T : class =>
43+
this.ConvertHits<T>(serializer).ToList().AsReadOnly();
44+
45+
public IReadOnlyCollection<T> Documents<T>(JsonSerializer serializer = null) where T : class =>
46+
this.ConvertHits<T>(serializer).Select(h => h.Source).ToList().AsReadOnly();
4347
}
4448
}

src/Nest/Cat/CatResponse.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using Newtonsoft.Json;
34

45
namespace Nest
56
{
67
public interface ICatResponse<out TCatRecord> : IResponse
78
where TCatRecord : ICatRecord
89
{
9-
IEnumerable<TCatRecord> Records { get; }
10+
IReadOnlyCollection<TCatRecord> Records { get; }
1011
}
1112

1213
[JsonObject]
1314
public class CatResponse<TCatRecord> : ResponseBase, ICatResponse<TCatRecord>
1415
where TCatRecord : ICatRecord
1516
{
16-
public IEnumerable<TCatRecord> Records { get; internal set; }
17+
public IReadOnlyCollection<TCatRecord> Records { get; internal set; } = EmptyReadOnly<TCatRecord>.Collection;
1718
}
1819
}

src/Nest/Cat/ElasticClient-Cat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public partial class ElasticClient
1313
private CatResponse<TCatRecord> DeserializeCatResponse<TCatRecord>(IApiCallDetails response, Stream stream)
1414
where TCatRecord : ICatRecord
1515
{
16-
var records = this.Serializer.Deserialize<IEnumerable<TCatRecord>>(stream);
16+
var records = this.Serializer.Deserialize<IReadOnlyCollection<TCatRecord>>(stream);
1717
return new CatResponse<TCatRecord> { Records = records };
1818
}
1919

src/Nest/Cluster/ClusterAllocationExplain/ClusterAllocationExplainResponse.cs

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,58 @@ namespace Nest
99
public interface IClusterAllocationExplainResponse : IResponse
1010
{
1111
[JsonProperty("shard")]
12-
ShardAllocationExplanation Shard { get; set; }
12+
ShardAllocationExplanation Shard { get; }
1313

1414
[JsonProperty("assigned")]
15-
bool Assigned { get; set; }
15+
bool Assigned { get; }
1616

1717
[JsonProperty("assigned_node_id")]
18-
string AssignedNodeId { get; set; }
18+
string AssignedNodeId { get; }
1919

2020
[JsonProperty("shard_state_fetch_pending")]
21-
bool ShardStateFetchPending { get; set; }
21+
bool ShardStateFetchPending { get; }
2222

2323
[JsonProperty("unassigned_info")]
24-
UnassignedInformation UnassignedInformation { get; set; }
24+
UnassignedInformation UnassignedInformation { get; }
2525

2626
[JsonProperty("allocation_delay")]
27-
string AllocationDelay { get; set; }
27+
string AllocationDelay { get; }
2828

2929
[JsonProperty("allocation_delay_ms")]
30-
long AllocationDelayInMilliseconds { get; set; }
30+
long AllocationDelayInMilliseconds { get; }
3131

3232
[JsonProperty("remaining_delay")]
33-
string RemainingDelay { get; set; }
33+
string RemainingDelay { get; }
3434

3535
[JsonProperty("remaining_delay_ms")]
36-
long RemainingDelayInMilliseconds { get; set; }
36+
long RemainingDelayInMilliseconds { get; }
3737

3838
[JsonProperty("nodes")]
3939
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter))]
40-
Dictionary<string, NodeAllocationExplanation> Nodes { get; set; }
40+
IReadOnlyDictionary<string, NodeAllocationExplanation> Nodes { get; }
4141
}
4242

4343
public class ClusterAllocationExplainResponse : ResponseBase, IClusterAllocationExplainResponse
4444
{
45-
public ShardAllocationExplanation Shard { get; set; }
45+
public ShardAllocationExplanation Shard { get; internal set; }
4646

47-
public bool Assigned { get; set; }
47+
public bool Assigned { get; internal set; }
4848

49-
public string AssignedNodeId { get; set; }
49+
public string AssignedNodeId { get; internal set; }
5050

51-
public bool ShardStateFetchPending { get; set; }
51+
public bool ShardStateFetchPending { get; internal set; }
5252

53-
public UnassignedInformation UnassignedInformation { get; set; }
53+
public UnassignedInformation UnassignedInformation { get; internal set; }
5454

55-
public string AllocationDelay { get; set; }
55+
public string AllocationDelay { get; internal set; }
5656

57-
public long AllocationDelayInMilliseconds { get; set; }
57+
public long AllocationDelayInMilliseconds { get; internal set; }
5858

59-
public string RemainingDelay { get; set; }
59+
public string RemainingDelay { get; internal set; }
6060

61-
public long RemainingDelayInMilliseconds { get; set; }
61+
public long RemainingDelayInMilliseconds { get; internal set; }
6262

63-
public Dictionary<string, NodeAllocationExplanation> Nodes { get; set; }
63+
public IReadOnlyDictionary<string, NodeAllocationExplanation> Nodes { get; internal set; } = EmptyReadOnly<string, NodeAllocationExplanation>.Dictionary;
6464
}
6565

6666
[JsonConverter(typeof(StringEnumConverter))]
@@ -86,7 +86,7 @@ public class NodeAllocationExplanation
8686
public string NodeName { get; set; }
8787

8888
[JsonProperty("node_attributes")]
89-
public Dictionary<string,string> NodeAttributes { get; set; }
89+
public IReadOnlyDictionary<string, string> NodeAttributes { get; set; } = EmptyReadOnly<string, string>.Dictionary;
9090

9191
[JsonProperty("store")]
9292
public AllocationStore Store { get; set; }
@@ -102,7 +102,7 @@ public class NodeAllocationExplanation
102102
public float Weight { get; set; }
103103

104104
[JsonProperty("decisions")]
105-
public IEnumerable<AllocationDecision> Decisions { get; set; }
105+
public IReadOnlyCollection<AllocationDecision> Decisions { get; set; } = EmptyReadOnly<AllocationDecision>.Collection;
106106
}
107107

108108
[JsonObject]
@@ -160,68 +160,68 @@ public enum UnassignedInformationReason
160160
IndexCreated,
161161

162162
///<summary>
163-
/// Unassigned as a result of a full cluster recovery.
164-
///</summary>
163+
/// Unassigned as a result of a full cluster recovery.
164+
///</summary>
165165
[EnumMember(Value = "CLUSTER_RECOVERED")]
166166
ClusterRecovered,
167167

168168
///<summary>
169-
/// Unassigned as a result of opening a closed index.
170-
///</summary>
169+
/// Unassigned as a result of opening a closed index.
170+
///</summary>
171171
[EnumMember(Value = "INDEX_REOPENED")]
172172
IndexReopened,
173173

174174
///<summary>
175-
/// Unassigned as a result of importing a dangling index.
176-
///</summary>
175+
/// Unassigned as a result of importing a dangling index.
176+
///</summary>
177177
[EnumMember(Value = "DANGLING_INDEX_IMPORTED")]
178178
DanglingIndexImported,
179179

180180
///<summary>
181-
/// Unassigned as a result of restoring into a new index.
182-
///</summary>
181+
/// Unassigned as a result of restoring into a new index.
182+
///</summary>
183183
[EnumMember(Value = "NEW_INDEX_RESTORED")]
184184
NewIndexRestored,
185185

186186
///<summary>
187-
/// Unassigned as a result of restoring into a closed index.
188-
///</summary>
187+
/// Unassigned as a result of restoring into a closed index.
188+
///</summary>
189189
[EnumMember(Value = "EXISTING_INDEX_RESTORED")]
190190
ExistingIndexRestored,
191191

192192
///<summary>
193-
/// Unassigned as a result of explicit addition of a replica.
194-
///</summary>
193+
/// Unassigned as a result of explicit addition of a replica.
194+
///</summary>
195195
[EnumMember(Value = "REPLICA_ADDED")]
196196
ReplicaAdded,
197197

198198
///<summary>
199-
/// Unassigned as a result of a failed allocation of the shard.
200-
///</summary>
199+
/// Unassigned as a result of a failed allocation of the shard.
200+
///</summary>
201201
[EnumMember(Value = "ALLOCATION_FAILED")]
202202
AllocationFailed,
203203

204204
///<summary>
205-
/// Unassigned as a result of the node hosting it leaving the cluster.
206-
///</summary>
205+
/// Unassigned as a result of the node hosting it leaving the cluster.
206+
///</summary>
207207
[EnumMember(Value = "NODE_LEFT")]
208208
NodeLeft,
209209

210210
///<summary>
211-
/// Unassigned as a result of explicit cancel reroute command.
212-
///</summary>
211+
/// Unassigned as a result of explicit cancel reroute command.
212+
///</summary>
213213
[EnumMember(Value = "REROUTE_CANCELLED")]
214214
RerouteCancelled,
215215

216216
///<summary>
217-
/// When a shard moves from started back to initializing, for example, during shadow replica
218-
///</summary>
217+
/// When a shard moves from started back to initializing, for example, during shadow replica
218+
///</summary>
219219
[EnumMember(Value = "REINITIALIZED")]
220220
Reinitialized,
221221

222222
///<summary>
223-
/// A better replica location is identified and causes the existing replica allocation to be cancelled.
224-
///</summary>
223+
/// A better replica location is identified and causes the existing replica allocation to be cancelled.
224+
///</summary>
225225
[EnumMember(Value = "REALLOCATED_REPLICA")]
226226
ReallocatedReplica
227227
}

0 commit comments

Comments
 (0)