Skip to content

Commit 0d4f598

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 dennisdoomen/FluentAssertions#357 Update Watcher APIs to use readonly collections Use Actions for actions on Watch type Enable readonly collection deserialization in SimpleJson (cherry-pick from commit 73a77a2)
1 parent 1fb762e commit 0d4f598

File tree

26 files changed

+1128
-71
lines changed

26 files changed

+1128
-71
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+
}

0 commit comments

Comments
 (0)