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
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]
public void RequiredFieldsInExtensions()
{
......
......@@ -89,7 +89,7 @@ namespace Google.Protobuf
private CodedOutputStream(byte[] buffer, int offset, int length)
{
this.output = null;
this.buffer = buffer;
this.buffer = ProtoPreconditions.CheckNotNull(buffer, nameof(buffer));
this.position = offset;
this.limit = offset + length;
leaveOpen = true; // Simple way of avoiding trying to dispose of a null reference
......
......@@ -133,7 +133,7 @@ namespace Google.Protobuf
/// <param name="settings">The settings.</param>
public JsonFormatter(Settings settings)
{
this.settings = settings;
this.settings = ProtoPreconditions.CheckNotNull(settings, nameof(settings));
}
/// <summary>
......
......@@ -110,7 +110,7 @@ namespace Google.Protobuf
/// <param name="settings">The settings.</param>
public JsonParser(Settings settings)
{
this.settings = settings;
this.settings = ProtoPreconditions.CheckNotNull(settings, nameof(settings));
}
/// <summary>
......
......@@ -72,7 +72,6 @@ namespace Google.Protobuf
{
IMessage message = factory();
message.MergeFrom(data, DiscardUnknownFields, Extensions);
CheckMergedRequiredFields(message);
return message;
}
......@@ -87,7 +86,6 @@ namespace Google.Protobuf
{
IMessage message = factory();
message.MergeFrom(data, offset, length, DiscardUnknownFields, Extensions);
CheckMergedRequiredFields(message);
return message;
}
......@@ -100,7 +98,6 @@ namespace Google.Protobuf
{
IMessage message = factory();
message.MergeFrom(data, DiscardUnknownFields, Extensions);
CheckMergedRequiredFields(message);
return message;
}
......@@ -113,7 +110,6 @@ namespace Google.Protobuf
{
IMessage message = factory();
message.MergeFrom(input, DiscardUnknownFields, Extensions);
CheckMergedRequiredFields(message);
return message;
}
......@@ -130,7 +126,6 @@ namespace Google.Protobuf
{
IMessage message = factory();
message.MergeDelimitedFrom(input, DiscardUnknownFields, Extensions);
CheckMergedRequiredFields(message);
return message;
}
......@@ -143,7 +138,6 @@ namespace Google.Protobuf
{
IMessage message = factory();
MergeFrom(message, input);
CheckMergedRequiredFields(message);
return message;
}
......@@ -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>
/// Creates a new message parser which optionally discards unknown fields when parsing.
/// </summary>
......
......@@ -107,13 +107,14 @@ namespace Google.Protobuf.Reflection
{
descriptor.CrossLink();
IList<FieldDescriptor> _;
if (!declarationOrder.TryGetValue(descriptor.ExtendeeType, out _))
IList<FieldDescriptor> list;
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
......
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