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

Cleanup various bits of Google.Protobuf
pull/6990/head
Jan Tattermusch 5 years ago committed by GitHub
commit 20b7fab0a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      csharp/src/Google.Protobuf.Test/GeneratedMessageTest.Proto2.cs
  2. 2
      csharp/src/Google.Protobuf/CodedOutputStream.cs
  3. 2
      csharp/src/Google.Protobuf/JsonFormatter.cs
  4. 2
      csharp/src/Google.Protobuf/JsonParser.cs
  5. 12
      csharp/src/Google.Protobuf/MessageParser.cs
  6. 9
      csharp/src/Google.Protobuf/Reflection/ExtensionCollection.cs

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

Loading…
Cancel
Save