diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 2164c283636..61c1318a079 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -23,6 +23,7 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima [[release-3-5-8]] === TinkerPop 3.5.8 (NOT OFFICIALLY RELEASED YET) +* Fixed a bug in Gremlin.Net that can lead to an `InvalidOperationException` due to modifying a collection while iterating over it in the serializers. [[release-3-5-7]] diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphBinary/TypeSerializerRegistry.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphBinary/TypeSerializerRegistry.cs index 1fc863e26d8..a6fda07ee32 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphBinary/TypeSerializerRegistry.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphBinary/TypeSerializerRegistry.cs @@ -159,11 +159,11 @@ private TypeSerializerRegistry(List customTypeEntries) /// Thrown when no serializer can be found for the type. public ITypeSerializer GetSerializerFor(Type valueType) { - if (_serializerByType.ContainsKey(valueType)) + if (_serializerByType.TryGetValue(valueType, out var serializerForType)) { - return _serializerByType[valueType]; + return serializerForType; } - + if (IsDictionaryType(valueType, out var dictKeyType, out var dictValueType)) { var serializerType = typeof(MapSerializer<,>).MakeGenericType(dictKeyType, dictValueType); @@ -199,7 +199,7 @@ public ITypeSerializer GetSerializerFor(Type valueType) return serializer; } - foreach (var supportedType in _serializerByType.Keys) + foreach (var supportedType in new List(_serializerByType.Keys)) { if (supportedType.IsAssignableFrom(valueType)) { @@ -245,7 +245,7 @@ private static bool IsListType(Type type) implementedInterface.GetGenericTypeDefinition() == typeof(IList<>)); } - + /// /// Gets a serializer for the given GraphBinary type. /// @@ -255,10 +255,10 @@ public ITypeSerializer GetSerializerFor(DataType type) { if (type == DataType.Custom) throw new ArgumentException("Custom type serializers can not be retrieved using this method"); - + return _serializerByDataType[type]; } - + /// /// Gets a serializer for the given custom type name. /// @@ -270,14 +270,14 @@ public CustomTypeSerializer GetSerializerForCustomType(string typeName) ? serializer : throw new InvalidOperationException($"No serializer found for type '{typeName}'."); } - + /// /// The builder of a . /// public class Builder { private readonly List _list = new List(); - + /// /// Creates the . /// @@ -293,7 +293,7 @@ public Builder AddCustomType(Type type, CustomTypeSerializer serializer) if (serializer == null) throw new ArgumentNullException(nameof(serializer)); if (serializer.TypeName == null) throw new ArgumentException("serializer custom type name can not be null"); - + _list.Add(new CustomTypeRegistryEntry(type, serializer)); return this; } diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs index e453efc792e..a7d4d011287 100644 --- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs +++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs @@ -134,11 +134,11 @@ public dynamic ToDict(dynamic objectData) private IGraphSONSerializer TryGetSerializerFor(Type type) { - if (Serializers.ContainsKey(type)) + if (Serializers.TryGetValue(type, out var serializer)) { - return Serializers[type]; + return serializer; } - foreach (var supportedType in Serializers.Keys) + foreach (var supportedType in new List(Serializers.Keys)) if (supportedType.IsAssignableFrom(type)) { return Serializers[supportedType];