Unverified Commit 20b7fab0 authored by Jan Tattermusch's avatar Jan Tattermusch Committed by GitHub

Merge pull request #6674 from ObsidianMinor/csharp/general-cleanup

Cleanup various bits of Google.Protobuf
parents c8a56342 d5e964c8
...@@ -261,6 +261,18 @@ namespace Google.Protobuf ...@@ -261,6 +261,18 @@ namespace Google.Protobuf
Assert.True(message.IsInitialized()); Assert.True(message.IsInitialized());
} }
/// <summary>
/// Code was accidentally left in message parser that threw exceptions when missing required fields after parsing.
/// We've decided to not throw exceptions on missing fields, instead leaving it up to the consumer how they
/// want to check and handle missing fields.
/// </summary>
[Test]
public void RequiredFieldsNoThrow()
{
Assert.DoesNotThrow(() => TestRequired.Parser.ParseFrom(new byte[0]));
Assert.DoesNotThrow(() => (TestRequired.Parser as MessageParser).ParseFrom(new byte[0]));
}
[Test] [Test]
public void RequiredFieldsInExtensions() public void RequiredFieldsInExtensions()
{ {
......
...@@ -89,7 +89,7 @@ namespace Google.Protobuf ...@@ -89,7 +89,7 @@ namespace Google.Protobuf
private CodedOutputStream(byte[] buffer, int offset, int length) private CodedOutputStream(byte[] buffer, int offset, int length)
{ {
this.output = null; this.output = null;
this.buffer = buffer; this.buffer = ProtoPreconditions.CheckNotNull(buffer, nameof(buffer));
this.position = offset; this.position = offset;
this.limit = offset + length; this.limit = offset + length;
leaveOpen = true; // Simple way of avoiding trying to dispose of a null reference leaveOpen = true; // Simple way of avoiding trying to dispose of a null reference
......
...@@ -133,7 +133,7 @@ namespace Google.Protobuf ...@@ -133,7 +133,7 @@ namespace Google.Protobuf
/// <param name="settings">The settings.</param> /// <param name="settings">The settings.</param>
public JsonFormatter(Settings settings) public JsonFormatter(Settings settings)
{ {
this.settings = settings; this.settings = ProtoPreconditions.CheckNotNull(settings, nameof(settings));
} }
/// <summary> /// <summary>
......
...@@ -110,7 +110,7 @@ namespace Google.Protobuf ...@@ -110,7 +110,7 @@ namespace Google.Protobuf
/// <param name="settings">The settings.</param> /// <param name="settings">The settings.</param>
public JsonParser(Settings settings) public JsonParser(Settings settings)
{ {
this.settings = settings; this.settings = ProtoPreconditions.CheckNotNull(settings, nameof(settings));
} }
/// <summary> /// <summary>
......
...@@ -72,7 +72,6 @@ namespace Google.Protobuf ...@@ -72,7 +72,6 @@ namespace Google.Protobuf
{ {
IMessage message = factory(); IMessage message = factory();
message.MergeFrom(data, DiscardUnknownFields, Extensions); message.MergeFrom(data, DiscardUnknownFields, Extensions);
CheckMergedRequiredFields(message);
return message; return message;
} }
...@@ -87,7 +86,6 @@ namespace Google.Protobuf ...@@ -87,7 +86,6 @@ namespace Google.Protobuf
{ {
IMessage message = factory(); IMessage message = factory();
message.MergeFrom(data, offset, length, DiscardUnknownFields, Extensions); message.MergeFrom(data, offset, length, DiscardUnknownFields, Extensions);
CheckMergedRequiredFields(message);
return message; return message;
} }
...@@ -100,7 +98,6 @@ namespace Google.Protobuf ...@@ -100,7 +98,6 @@ namespace Google.Protobuf
{ {
IMessage message = factory(); IMessage message = factory();
message.MergeFrom(data, DiscardUnknownFields, Extensions); message.MergeFrom(data, DiscardUnknownFields, Extensions);
CheckMergedRequiredFields(message);
return message; return message;
} }
...@@ -113,7 +110,6 @@ namespace Google.Protobuf ...@@ -113,7 +110,6 @@ namespace Google.Protobuf
{ {
IMessage message = factory(); IMessage message = factory();
message.MergeFrom(input, DiscardUnknownFields, Extensions); message.MergeFrom(input, DiscardUnknownFields, Extensions);
CheckMergedRequiredFields(message);
return message; return message;
} }
...@@ -130,7 +126,6 @@ namespace Google.Protobuf ...@@ -130,7 +126,6 @@ namespace Google.Protobuf
{ {
IMessage message = factory(); IMessage message = factory();
message.MergeDelimitedFrom(input, DiscardUnknownFields, Extensions); message.MergeDelimitedFrom(input, DiscardUnknownFields, Extensions);
CheckMergedRequiredFields(message);
return message; return message;
} }
...@@ -143,7 +138,6 @@ namespace Google.Protobuf ...@@ -143,7 +138,6 @@ namespace Google.Protobuf
{ {
IMessage message = factory(); IMessage message = factory();
MergeFrom(message, input); MergeFrom(message, input);
CheckMergedRequiredFields(message);
return message; return message;
} }
...@@ -176,12 +170,6 @@ namespace Google.Protobuf ...@@ -176,12 +170,6 @@ namespace Google.Protobuf
} }
} }
internal static void CheckMergedRequiredFields(IMessage message)
{
if (!message.IsInitialized())
throw new InvalidOperationException("Parsed message does not contain all required fields");
}
/// <summary> /// <summary>
/// Creates a new message parser which optionally discards unknown fields when parsing. /// Creates a new message parser which optionally discards unknown fields when parsing.
/// </summary> /// </summary>
......
...@@ -107,13 +107,14 @@ namespace Google.Protobuf.Reflection ...@@ -107,13 +107,14 @@ namespace Google.Protobuf.Reflection
{ {
descriptor.CrossLink(); descriptor.CrossLink();
IList<FieldDescriptor> _; IList<FieldDescriptor> list;
if (!declarationOrder.TryGetValue(descriptor.ExtendeeType, out _)) if (!declarationOrder.TryGetValue(descriptor.ExtendeeType, out list))
{ {
declarationOrder.Add(descriptor.ExtendeeType, new List<FieldDescriptor>()); list = new List<FieldDescriptor>();
declarationOrder.Add(descriptor.ExtendeeType, list);
} }
declarationOrder[descriptor.ExtendeeType].Add(descriptor); list.Add(descriptor);
} }
extensionsByTypeInDeclarationOrder = declarationOrder extensionsByTypeInDeclarationOrder = declarationOrder
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment