Skip to content

Commit

Permalink
fix exceptions on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Sep 9, 2024
1 parent 430e999 commit 5c89236
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/MongoDB.Bson/IO/BsonBinaryWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public override void WriteEndArray()
/// <summary>
/// Writes the end of a BSON document to the writer.
/// </summary>
public override void WriteEndDocument()
public override void WriteEndDocument(bool forceStaticSerializerRegistry = false)
{
if (Disposed) { throw new ObjectDisposedException("BsonBinaryWriter"); }
if (State != BsonWriterState.Name)
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Bson/IO/BsonDocumentWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public override void WriteEndArray()
/// <summary>
/// Writes the end of a BSON document to the writer.
/// </summary>
public override void WriteEndDocument()
public override void WriteEndDocument(bool forceStaticSerializerRegistry = false)
{
if (Disposed) { throw new ObjectDisposedException("BsonDocumentWriter"); }
if (State != BsonWriterState.Name)
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Bson/IO/BsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public virtual void WriteEndArray()
/// <summary>
/// Writes the end of a BSON document to the writer.
/// </summary>
public virtual void WriteEndDocument()
public virtual void WriteEndDocument(bool forceStaticSerializerRegistry = false)
=> ExitSerializationScope();

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/MongoDB.Bson/IO/ElementAppendingBsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ElementAppendingBsonWriter(

// public methods
/// <inheritdoc />
public override void WriteEndDocument()
public override void WriteEndDocument(bool forceStaticSerializerRegistry = false)
{
if (--_depth == 0)
{
Expand All @@ -64,7 +64,7 @@ public override void WriteEndDocument()
foreach (var element in _elements)
{
Wrapped.WriteName(element.Name);
BsonValueSerializer.Instance.Serialize(context, element.Value);
BsonValueSerializer.Instance.Serialize(context, element.Value, forceStaticSerializerRegistry);
}
}
finally
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Bson/IO/IBsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public interface IBsonWriter : IDisposable
/// <summary>
/// Writes the end of a BSON document to the writer.
/// </summary>
void WriteEndDocument();
void WriteEndDocument(bool forceStaticSerializerRegistry = false);

/// <summary>
/// Writes a BSON Int32 to the writer.
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Bson/IO/JsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public override void WriteEndArray()
/// <summary>
/// Writes the end of a BSON document to the writer.
/// </summary>
public override void WriteEndDocument()
public override void WriteEndDocument(bool forceStaticSerializerRegistry = false)
{
if (Disposed) { throw new ObjectDisposedException("JsonWriter"); }
if (State != BsonWriterState.Name)
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Bson/IO/WrappingBsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public virtual void WriteEndArray()
}

/// <inheritdoc />
public virtual void WriteEndDocument()
public virtual void WriteEndDocument(bool forceStaticSerializerRegistry = false)
{
ThrowIfDisposed();
_wrapped.WriteEndDocument();
Expand Down
14 changes: 12 additions & 2 deletions src/MongoDB.Bson/ObjectModel/BsonDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,9 +1234,19 @@ public Hashtable ToHashtable()
/// Returns a string representation of the document.
/// </summary>
/// <returns>A string representation of the document.</returns>
public override string ToString()
public override string ToString() => ToString(false);

/// <summary>
/// Returns a string representation of the document.
/// </summary>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>A string representation of the document.</returns>
public string ToString(bool forceStaticSerializerRegistry)
{
return this.ToJson();
return this.ToJson(args: new BsonSerializationArgs()
{
ForceStaticSerializerRegistry = forceStaticSerializerRegistry
});
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected override void SerializeValue(BsonSerializationContext context, BsonSer
bsonWriter.WriteStartArray();
for (int i = 0; i < value.Count; i++)
{
BsonValueSerializer.Instance.Serialize(context, value[i]);
BsonValueSerializer.Instance.Serialize(context, value[i], forceStaticSerializerRegistry: args.ForceStaticSerializerRegistry);
}
bsonWriter.WriteEndArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ protected override void SerializeValue(BsonSerializationContext context, BsonSer
BsonValueSerializer.Instance.Serialize(context, element.Value, args.ForceStaticSerializerRegistry);
}

bsonWriter.WriteEndDocument();
bsonWriter.WriteEndDocument(forceStaticSerializerRegistry: args.ForceStaticSerializerRegistry);
}

/// <summary>
Expand Down
11 changes: 9 additions & 2 deletions src/MongoDB.Driver.Core/Core/Clusters/ClusterDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,16 @@ public override int GetHashCode()
}

/// <inheritdoc/>
public override string ToString()
public override string ToString() => ToString(false);

/// <summary>
/// Returns a string representation of the document.
/// </summary>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>A string representation of the document.</returns>
public string ToString(bool forceStaticSerializerRegistry)
{
var servers = string.Join(", ", _servers.Select(n => n.ToString()).ToArray());
var servers = string.Join(", ", _servers.Select(n => n.ToString(forceStaticSerializerRegistry)).ToArray());
var value = string.Format(
"{{ ClusterId : \"{0}\", {1}Type : \"{2}\", State : \"{3}\", Servers : [{4}] }}",
_clusterId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static object[] GetParams(ClusterId clusterId, object arg1, object arg2)

public static object[] GetParams(ClusterId clusterId, object message, ClusterDescription oldDescription, ClusterDescription newDescription)
{
return new[] { clusterId.Value, message, oldDescription.ToString(), newDescription.ToString() };
return new[] { clusterId.Value, message, oldDescription.ToString(true), newDescription.ToString(true) };
}

public static object[] GetParams(ClusterId clusterId, EndPoint endPoint, object arg1)
Expand Down Expand Up @@ -247,7 +247,7 @@ private static string DocumentToString(BsonDocument document, EventLogFormatting
return null;
}

return TruncateIfNeeded(document.ToString(), eventLogFormattingOptions.MaxDocumentSize);
return TruncateIfNeeded(document.ToString(forceStaticSerializerRegistry: true), eventLogFormattingOptions.MaxDocumentSize);
}

private static string FormatException(Exception exception, EventLogFormattingOptions eventLogFormattingOptions)
Expand Down
11 changes: 9 additions & 2 deletions src/MongoDB.Driver.Core/Core/Servers/ServerDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,14 @@ public bool SdamEquals(ServerDescription other)
}

/// <inheritdoc/>
public override string ToString()
public override string ToString() => ToString(false);

/// <summary>
/// Returns a string representation of the document.
/// </summary>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>A string representation of the document.</returns>
public string ToString(bool forceStaticSerializerRegistry)
{
return new StringBuilder()
.Append("{ ")
Expand All @@ -578,7 +585,7 @@ public override string ToString()
.AppendFormat(", ReasonChanged: \"{0}\"", _reasonChanged)
.AppendFormat(", State: \"{0}\"", _state)
.Append($", ServerVersion: {_version}")
.Append($", TopologyVersion: {_topologyVersion}")
.Append($", TopologyVersion: {_topologyVersion?.ToString(forceStaticSerializerRegistry)}")
.AppendFormat(", Type: \"{0}\"", _type)
.AppendFormatIf(_tags != null && !_tags.IsEmpty, ", Tags: \"{0}\"", _tags)
.AppendFormatIf(_state == ServerState.Connected, ", WireVersionRange: \"{0}\"", _wireVersionRange)
Expand Down
11 changes: 10 additions & 1 deletion src/MongoDB.Driver.Core/Core/Servers/TopologyVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using System;
using Etherna.MongoDB.Bson;
using Etherna.MongoDB.Bson.Serialization;
using Etherna.MongoDB.Shared;

namespace Etherna.MongoDB.Driver.Core.Servers
Expand Down Expand Up @@ -215,6 +216,14 @@ public bool Equals(TopologyVersion other)
public BsonDocument ToBsonDocument() => new BsonDocument { { "processId", _processId }, { "counter", _counter } };

/// <inheritdoc/>
public override string ToString() => ToBsonDocument().ToJson();
public override string ToString() => ToString(false);

/// <summary>
/// Returns a string representation of the document.
/// </summary>
/// <param name="forceStaticSerializerRegistry">Force to use static serializer registry</param>
/// <returns>A string representation of the document.</returns>
public string ToString(bool forceStaticSerializerRegistry) => ToBsonDocument()
.ToJson(args: new BsonSerializationArgs { ForceStaticSerializerRegistry = forceStaticSerializerRegistry });
}
}

0 comments on commit 5c89236

Please sign in to comment.