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

Cleanup various bits of Google.Protobuf
This commit is contained in:
Jan Tattermusch 2019-12-05 10:38:10 +01:00 committed by GitHub
commit 20b7fab0a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 19 deletions

View File

@ -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()
{

View File

@ -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

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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