Skip to content

Commit 010ee2b

Browse files
committed
Assert all collections on Response types are read only
Reflection unit test traverses IResponse properties to check their types. Add Fluent Assertions for IReadOnlyDictionary<TKey, TValue>. See https://github.com/dennisdoomen/FluentAssertions/issues/357 Update Watcher APIs to use readonly collections Use Actions for actions on Watch type Enable readonly collection deserialization in SimpleJson
1 parent 004ddfd commit 010ee2b

File tree

26 files changed

+1129
-72
lines changed

26 files changed

+1129
-72
lines changed

src/Elasticsearch.Net/Responses/ServerError.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class Error : IRootCause
8282
public string ResourceId { get; set; }
8383
public string ResourceType { get; set; }
8484
public string Type { get; set; }
85-
public List<RootCause> RootCause { get; set; }
85+
public IReadOnlyCollection<RootCause> RootCause { get; set; }
8686
public CausedBy CausedBy { get; set; }
8787

8888
internal static Error Create(IDictionary<string, object> dict, IJsonSerializerStrategy strategy)
@@ -99,7 +99,7 @@ internal static Error Create(IDictionary<string, object> dict, IJsonSerializerSt
9999

100100
var os = rootCause as object[];
101101
if (os == null) return error;
102-
error.RootCause = os.Select(o => (RootCause)strategy.DeserializeObject(o, typeof(RootCause))).ToList();
102+
error.RootCause = os.Select(o => (RootCause)strategy.DeserializeObject(o, typeof(RootCause))).ToList().AsReadOnly();
103103
return error;
104104
}
105105

src/Elasticsearch.Net/Serialization/SimpleJson.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
//#define SIMPLE_JSON_DATACONTRACT
3333

3434
// NOTE: uncomment the following line to enable IReadOnlyCollection<T> and IReadOnlyList<T> support.
35-
//#define SIMPLE_JSON_READONLY_COLLECTIONS
35+
#define SIMPLE_JSON_READONLY_COLLECTIONS
3636

3737
// NOTE: uncomment the following line to disable linq expressions/compiled lambda (better performance) instead of method.invoke().
3838
// define if you are using .net framework <= 3.0 or < WP7.5
@@ -81,12 +81,12 @@ namespace Elasticsearch.Net
8181
class JsonArray : List<object>
8282
{
8383
/// <summary>
84-
/// Initializes a new instance of the <see cref="JsonArray"/> class.
84+
/// Initializes a new instance of the <see cref="JsonArray"/> class.
8585
/// </summary>
8686
public JsonArray() { }
8787

8888
/// <summary>
89-
/// Initializes a new instance of the <see cref="JsonArray"/> class.
89+
/// Initializes a new instance of the <see cref="JsonArray"/> class.
9090
/// </summary>
9191
/// <param name="capacity">The capacity of the json array.</param>
9292
public JsonArray(int capacity) : base(capacity) { }
@@ -480,7 +480,7 @@ public override IEnumerable<string> GetDynamicMemberNames()
480480
/// <summary>
481481
/// This class encodes and decodes JSON strings.
482482
/// Spec. details, see http://www.json.org/
483-
///
483+
///
484484
/// JSON uses Arrays and Objects. These correspond here to the datatypes JsonArray(IList&lt;object>) and JsonObject(IDictionary&lt;string,object>).
485485
/// All numbers are parsed to doubles.
486486
/// </summary>
@@ -1176,7 +1176,7 @@ public static DataContractJsonSerializerStrategy DataContractJsonSerializerStrat
11761176

11771177
#endif
11781178
}
1179-
1179+
11801180
[GeneratedCode("simple-json", "1.0.0")]
11811181
#if SIMPLE_JSON_INTERNAL
11821182
internal
@@ -1289,7 +1289,7 @@ public virtual object DeserializeObject(object value, Type type)
12891289

12901290
if (value == null)
12911291
return null;
1292-
1292+
12931293
object obj = null;
12941294

12951295
if (str != null)
@@ -1327,7 +1327,7 @@ public virtual object DeserializeObject(object value, Type type)
13271327
}
13281328
else if (value is bool)
13291329
return value;
1330-
1330+
13311331
bool valueIsLong = value is long;
13321332
bool valueIsDouble = value is double;
13331333
if ((valueIsLong && type == typeof(long)) || (valueIsDouble && type == typeof(double)))
@@ -2080,4 +2080,4 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
20802080
// ReSharper restore LoopCanBeConvertedToQuery
20812081
// ReSharper restore RedundantExplicitArrayCreation
20822082
// ReSharper restore SuggestUseVarKeywordEvident
2083-
}
2083+
}

src/Nest/Cluster/ClusterReroute/ClusterRerouteState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ClusterRerouteState
1717

1818
[JsonProperty("nodes")]
1919
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter<string, NodeState>))]
20-
public Dictionary<string, NodeState> Nodes { get; internal set; }
20+
public IReadOnlyDictionary<string, NodeState> Nodes { get; internal set; }
2121

2222
[JsonProperty("routing_table")]
2323
public RoutingTableState RoutingTable { get; internal set; }

src/Nest/Cluster/ClusterState/IndexRoutingTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public class IndexRoutingTable
77
{
88
[JsonProperty("shards")]
99
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter<string, List<RoutingShard>>))]
10-
public Dictionary<string, List<RoutingShard>> Shards { get; internal set; }
10+
public IReadOnlyDictionary<string, List<RoutingShard>> Shards { get; internal set; }
1111
}
1212
}

src/Nest/Cluster/ClusterState/MetadataState.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ public class MetadataState
99
{
1010
[JsonProperty("templates")]
1111
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter<string, TemplateMapping>))]
12-
public IDictionary<string, TemplateMapping> Templates { get; internal set; }
12+
public IReadOnlyDictionary<string, TemplateMapping> Templates { get; internal set; }
1313

1414
[JsonProperty("cluster_uuid")]
1515
public string ClusterUUID { get; internal set; }
1616

1717
[JsonProperty("indices")]
1818
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter<string, MetadataIndexState>))]
19-
public Dictionary<string, MetadataIndexState> Indices { get; internal set; }
19+
public IReadOnlyDictionary<string, MetadataIndexState> Indices { get; internal set; }
2020
}
2121
}

src/Nest/Cluster/ClusterState/RoutingNodesState.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace Nest
88
public class RoutingNodesState
99
{
1010
[JsonProperty("unassigned")]
11-
public List<RoutingShard> Unassigned { get; internal set; }
11+
public IReadOnlyCollection<RoutingShard> Unassigned { get; internal set; }
1212

1313
[JsonProperty("nodes")]
14-
public Dictionary<string, List<RoutingShard>> Nodes { get; internal set; }
14+
public IReadOnlyDictionary<string, List<RoutingShard>> Nodes { get; internal set; }
1515
}
16-
}
16+
}

src/Nest/Cluster/ClusterState/RoutingTableState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public class RoutingTableState
99
{
1010
[JsonProperty("indices")]
1111
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter<string, IndexRoutingTable>))]
12-
public Dictionary<string, IndexRoutingTable> Indices { get; internal set; }
12+
public IReadOnlyDictionary<string, IndexRoutingTable> Indices { get; internal set; } = EmptyReadOnly<string, IndexRoutingTable>.Dictionary;
1313
}
1414
}

src/Nest/Cluster/ClusterStats/ClusterNodesStats.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class ClusterNodesStats
1010
public ClusterNodeCount Count { get; internal set; }
1111

1212
[JsonProperty("versions")]
13-
public List<string> Versions { get; internal set; }
13+
public IReadOnlyCollection<string> Versions { get; internal set; }
1414

1515
[JsonProperty("os")]
1616
public ClusterOperatingSystemStats OperatingSystem { get; internal set; }
@@ -25,7 +25,7 @@ public class ClusterNodesStats
2525
public ClusterFileSystem FileSystem { get; internal set; }
2626

2727
[JsonProperty("plugins")]
28-
public List<PluginStats> Plugins { get; internal set; }
28+
public IReadOnlyCollection<PluginStats> Plugins { get; internal set; }
2929
}
3030

3131
[JsonObject]
@@ -55,7 +55,7 @@ public class ClusterJvm
5555
public long MaxUptimeInMilliseconds { get; internal set; }
5656

5757
[JsonProperty("versions")]
58-
public List<ClusterJvmVersion> Versions { get; internal set; }
58+
public IReadOnlyCollection<ClusterJvmVersion> Versions { get; internal set; }
5959

6060
[JsonProperty("mem")]
6161
public ClusterJvmMemory Memory { get; internal set; }
@@ -139,7 +139,7 @@ public class ClusterOperatingSystemStats
139139
public int AllocatedProcessors { get; internal set; }
140140

141141
[JsonProperty("names")]
142-
public List<ClusterOperatingSystemName> Names { get; internal set; }
142+
public IReadOnlyCollection<ClusterOperatingSystemName> Names { get; internal set; }
143143
}
144144

145145
[JsonObject]

src/Nest/CommonOptions/Stats/IndexingStats.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ public class IndexingStats
4444

4545
[JsonProperty("types")]
4646
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter<string, IndexingStats>))]
47-
public Dictionary<string, IndexingStats> Types { get; set; }
47+
public IReadOnlyDictionary<string, IndexingStats> Types { get; set; }
4848
}
4949
}

src/Nest/Modules/SnapshotAndRestore/Snapshot/Snapshot.cs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,30 @@ public class Snapshot
1111
public string Name { get; internal set; }
1212

1313
[JsonProperty("indices")]
14-
public IEnumerable<IndexName> Indices { get; internal set; }
14+
public IReadOnlyCollection<IndexName> Indices { get; internal set; }
1515

1616
[JsonProperty("state")]
1717
public string State { get; internal set; }
1818

1919
[JsonProperty("start_time")]
2020
public DateTime StartTime { get; internal set; }
21-
21+
2222
[JsonProperty("start_time_in_millis")]
2323
public long StartTimeInMilliseconds { get; internal set; }
24-
24+
2525
[JsonProperty("end_time")]
2626
public DateTime EndTime { get; internal set; }
27-
27+
2828
[JsonProperty("end_time_in_millis")]
2929
public long EndTimeInMilliseconds { get; internal set; }
30-
30+
3131
[JsonProperty("duration_in_millis")]
3232
public long DurationInMilliseconds { get; internal set; }
3333

3434
[JsonProperty("shards")]
3535
public ShardsMetaData Shards { get; internal set; }
3636

3737
[JsonProperty("failures")]
38-
public IEnumerable<SnapshotShardFailure> ShardFailures { get; internal set; }
39-
40-
/// <summary>
41-
/// Contains the reason for each shard failure.
42-
/// </summary>
43-
/// For 2.0, remove this and rename ShardFailures => Failures
44-
[JsonIgnore]
45-
public IEnumerable<string> Failures
46-
{
47-
get
48-
{
49-
if (this.ShardFailures != null)
50-
return this.ShardFailures.Select(f => f.Reason);
51-
return new List<string>();
52-
}
53-
}
54-
38+
public IReadOnlyCollection<SnapshotShardFailure> Failures { get; internal set; }
5539
}
56-
}
40+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
namespace Nest
1+
using System.Collections.Generic;
2+
3+
namespace Nest
24
{
35
/// <summary>
46
/// If the license is valid but is older or has less capabilities this will list out the reasons why a resubmission with acknowledge=true is required
57
/// </summary>
68
public class LicenseAcknowledgement
79
{
810
public string Message { get; set; }
9-
public string[] License { get; set; }
11+
public IReadOnlyCollection<string> License { get; set; }
1012
}
1113
}

src/Nest/XPack/Security/Authenticate/AuthenticateResponse.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
1+
using System.Collections.Generic;
42
using Newtonsoft.Json;
53

64
namespace Nest
@@ -11,7 +9,7 @@ public interface IAuthenticateResponse : IResponse
119
string Username { get; }
1210

1311
[JsonProperty("roles")]
14-
IEnumerable<string> Roles { get; }
12+
IReadOnlyCollection<string> Roles { get; }
1513

1614
[JsonProperty("full_name")]
1715
string FullName { get; }
@@ -28,13 +26,12 @@ public class AuthenticateResponse : ResponseBase, IAuthenticateResponse
2826
{
2927
public string Username { get; internal set; }
3028

31-
public IEnumerable<string> Roles { get; internal set; }
29+
public IReadOnlyCollection<string> Roles { get; internal set; } = EmptyReadOnly<string>.Collection;
3230

3331
public string FullName { get; internal set; }
3432

3533
public string Email { get; internal set; }
3634

3735
public IReadOnlyDictionary<string, object> Metadata { get; internal set; } = EmptyReadOnly<string, object>.Dictionary;
38-
3936
}
4037
}

src/Nest/XPack/Watcher/AcknowledgeWatch/AcknowledgeWatchResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class WatchStatus
3333
public DateTimeOffset? LastMetCondition { get; set; }
3434

3535
[JsonProperty("actions")]
36-
public Dictionary<string, ActionStatus> Actions { get; set; }
36+
public IReadOnlyDictionary<string, ActionStatus> Actions { get; set; }
3737
}
3838

3939
public class ActionStatus

src/Nest/XPack/Watcher/ActivateWatch/ActivateWatchResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public class ActivationStatus
2121
public ActivationState State { get; internal set; }
2222

2323
[JsonProperty("actions")]
24-
public Dictionary<string, ActionStatus> Actions { get; set; }
24+
public IReadOnlyDictionary<string, ActionStatus> Actions { get; set; }
2525
}
2626
}

src/Nest/XPack/Watcher/ExecuteWatch/ExecuteWatchResponse.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class WatchRecord
4949
public Id WatchId { get; set; }
5050

5151
[JsonProperty("messages")]
52-
public IEnumerable<string> Messages { get; set; }
52+
public IReadOnlyCollection<string> Messages { get; set; }
5353

5454
[JsonProperty("state")]
5555
public ActionExecutionState? State { get; set; }
@@ -64,7 +64,7 @@ public class WatchRecord
6464
public InputContainer Input { get; set; }
6565

6666
[JsonProperty("metadata")]
67-
public IDictionary<string, object> Metadata { get; set; }
67+
public IReadOnlyDictionary<string, object> Metadata { get; set; }
6868

6969
[JsonProperty("result")]
7070
public ExecutionResult Result { get; set; }
@@ -98,7 +98,7 @@ public class ExecutionResult
9898
public ExecutionResultCondition Condition { get; set; }
9999

100100
[JsonProperty("actions")]
101-
public List<ExecutionResultAction> Actions { get; set; }
101+
public IReadOnlyCollection<ExecutionResultAction> Actions { get; set; }
102102
}
103103

104104
[JsonObject]
@@ -111,7 +111,7 @@ public class ExecutionResultInput
111111
public Status Status { get; set; }
112112

113113
[JsonProperty("payload")]
114-
public Dictionary<string, object> Payload { get; set; }
114+
public IReadOnlyDictionary<string, object> Payload { get; set; }
115115
}
116116

117117
[JsonObject]

src/Nest/XPack/Watcher/Watch.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Nest
77
public class Watch
88
{
99
[JsonProperty("meta")]
10-
public IDictionary<string, object> Meta { get; internal set; }
10+
public IReadOnlyDictionary<string, object> Meta { get; internal set; }
1111

1212
[JsonProperty("input")]
1313
public IInputContainer Input { get; internal set; }
@@ -23,7 +23,7 @@ public class Watch
2323

2424
[JsonProperty("actions")]
2525
[JsonConverter(typeof(ActionsJsonConverter))]
26-
public IDictionary<string, IAction> Actions { get; internal set; }
26+
public Actions Actions { get; internal set; }
2727

2828
[JsonProperty("status")]
2929
public WatchStatus Status { get; internal set; }

src/Nest/XPack/Watcher/WatcherStats/WatcherStatsResponse.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public interface IWatcherStatsResponse : IResponse
1919
ExecutionThreadPool ExecutionThreadPool { get; }
2020

2121
[JsonProperty("current_watches")]
22-
IEnumerable<WatchRecordStats> CurrentWatches { get; }
22+
IReadOnlyCollection<WatchRecordStats> CurrentWatches { get; }
2323

2424
[JsonProperty("queued_watches")]
25-
IEnumerable<WatchRecordQueuedStats> QueuedWatches { get; }
25+
IReadOnlyCollection<WatchRecordQueuedStats> QueuedWatches { get; }
2626

2727
[JsonProperty("manually_stopped")]
2828
bool ManuallyStopped { get; }
@@ -52,9 +52,9 @@ public class WatcherStatsResponse : ResponseBase, IWatcherStatsResponse
5252

5353
public ExecutionThreadPool ExecutionThreadPool { get; internal set; }
5454

55-
public IEnumerable<WatchRecordStats> CurrentWatches { get; internal set; }
55+
public IReadOnlyCollection<WatchRecordStats> CurrentWatches { get; internal set; }
5656

57-
public IEnumerable<WatchRecordQueuedStats> QueuedWatches { get; internal set; }
57+
public IReadOnlyCollection<WatchRecordQueuedStats> QueuedWatches { get; internal set; }
5858

5959
public bool ManuallyStopped { get; internal set; }
6060
}

src/Tests/Cluster/ClusterState/ClusterStatsApiTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected override void ExpectResponse(IClusterStateResponse response)
4040

4141
private void Assert(IReadOnlyDictionary<string, NodeState> nodes, string master)
4242
{
43-
nodes.Should().NotBeEmpty().And.Contain(kv=>kv.Key == master);
43+
nodes.Should().NotBeEmpty().And.ContainKey(master);
4444
var node = nodes[master];
4545
node.Name.Should().NotBeNullOrWhiteSpace();
4646
node.TransportAddress.Should().NotBeNullOrWhiteSpace();

0 commit comments

Comments
 (0)