Unverified Commit 61d75cb8 authored by Jan Tattermusch's avatar Jan Tattermusch Committed by GitHub

Merge pull request #6826 from jskeet/fix-custom-options

Avoid NullReferenceException when accessing CustomOptions
parents 129a7c87 7282f29c
...@@ -41,6 +41,7 @@ using static Google.Protobuf.WireFormat; ...@@ -41,6 +41,7 @@ using static Google.Protobuf.WireFormat;
using static UnitTest.Issues.TestProtos.ComplexOptionType2.Types; using static UnitTest.Issues.TestProtos.ComplexOptionType2.Types;
using static UnitTest.Issues.TestProtos.UnittestCustomOptionsProto3Extensions; using static UnitTest.Issues.TestProtos.UnittestCustomOptionsProto3Extensions;
using static UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Types; using static UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Types;
using Google.Protobuf.TestProtos;
#pragma warning disable CS0618 #pragma warning disable CS0618
...@@ -177,6 +178,21 @@ namespace Google.Protobuf.Test.Reflection ...@@ -177,6 +178,21 @@ namespace Google.Protobuf.Test.Reflection
AssertOption(new Aggregate { S = "FieldAnnotation" }, fieldOptions.TryGetMessage, Fieldopt, AggregateMessage.Descriptor.Fields["fieldname"].GetOption); AssertOption(new Aggregate { S = "FieldAnnotation" }, fieldOptions.TryGetMessage, Fieldopt, AggregateMessage.Descriptor.Fields["fieldname"].GetOption);
} }
[Test]
public void NoOptions()
{
var fileDescriptor = UnittestProto3Reflection.Descriptor;
var messageDescriptor = TestAllTypes.Descriptor;
Assert.NotNull(fileDescriptor.CustomOptions);
Assert.NotNull(messageDescriptor.CustomOptions);
Assert.NotNull(messageDescriptor.Fields[1].CustomOptions);
Assert.NotNull(fileDescriptor.Services[0].CustomOptions);
Assert.NotNull(fileDescriptor.Services[0].Methods[0].CustomOptions);
Assert.NotNull(fileDescriptor.EnumTypes[0].CustomOptions);
Assert.NotNull(fileDescriptor.EnumTypes[0].Values[0].CustomOptions);
Assert.NotNull(TestAllTypes.Descriptor.Oneofs[0].CustomOptions);
}
private void AssertOption<T, D>(T expected, OptionFetcher<T> fetcher, Extension<D, T> extension, Func<Extension<D, T>, T> descriptorOptionFetcher) where D : IExtendableMessage<D> private void AssertOption<T, D>(T expected, OptionFetcher<T> fetcher, Extension<D, T> extension, Func<Extension<D, T>, T> descriptorOptionFetcher) where D : IExtendableMessage<D>
{ {
T customOptionsValue; T customOptionsValue;
......
...@@ -129,7 +129,7 @@ namespace Google.Protobuf.Reflection ...@@ -129,7 +129,7 @@ namespace Google.Protobuf.Reflection
/// The (possibly empty) set of custom options for this enum. /// The (possibly empty) set of custom options for this enum.
/// </summary> /// </summary>
[Obsolete("CustomOptions are obsolete. Use GetOption")] [Obsolete("CustomOptions are obsolete. Use GetOption")]
public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber);
/// <summary> /// <summary>
/// Gets a single value enum option for this descriptor /// Gets a single value enum option for this descriptor
......
...@@ -74,7 +74,7 @@ namespace Google.Protobuf.Reflection ...@@ -74,7 +74,7 @@ namespace Google.Protobuf.Reflection
/// The (possibly empty) set of custom options for this enum value. /// The (possibly empty) set of custom options for this enum value.
/// </summary> /// </summary>
[Obsolete("CustomOptions are obsolete. Use GetOption")] [Obsolete("CustomOptions are obsolete. Use GetOption")]
public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber);
/// <summary> /// <summary>
/// Gets a single value enum value option for this descriptor /// Gets a single value enum value option for this descriptor
......
...@@ -299,7 +299,7 @@ namespace Google.Protobuf.Reflection ...@@ -299,7 +299,7 @@ namespace Google.Protobuf.Reflection
/// The (possibly empty) set of custom options for this field. /// The (possibly empty) set of custom options for this field.
/// </summary> /// </summary>
[Obsolete("CustomOptions are obsolete. Use GetOption")] [Obsolete("CustomOptions are obsolete. Use GetOption")]
public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber);
/// <summary> /// <summary>
/// Gets a single value field option for this descriptor /// Gets a single value field option for this descriptor
......
...@@ -542,7 +542,7 @@ namespace Google.Protobuf.Reflection ...@@ -542,7 +542,7 @@ namespace Google.Protobuf.Reflection
/// The (possibly empty) set of custom options for this file. /// The (possibly empty) set of custom options for this file.
/// </summary> /// </summary>
[Obsolete("CustomOptions are obsolete. Use GetOption")] [Obsolete("CustomOptions are obsolete. Use GetOption")]
public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber);
/// <summary> /// <summary>
/// Gets a single value file option for this descriptor /// Gets a single value file option for this descriptor
......
...@@ -261,7 +261,7 @@ namespace Google.Protobuf.Reflection ...@@ -261,7 +261,7 @@ namespace Google.Protobuf.Reflection
/// The (possibly empty) set of custom options for this message. /// The (possibly empty) set of custom options for this message.
/// </summary> /// </summary>
[Obsolete("CustomOptions are obsolete. Use GetOption")] [Obsolete("CustomOptions are obsolete. Use GetOption")]
public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber);
/// <summary> /// <summary>
/// Gets a single value message option for this descriptor /// Gets a single value message option for this descriptor
......
...@@ -74,7 +74,7 @@ namespace Google.Protobuf.Reflection ...@@ -74,7 +74,7 @@ namespace Google.Protobuf.Reflection
/// The (possibly empty) set of custom options for this method. /// The (possibly empty) set of custom options for this method.
/// </summary> /// </summary>
[Obsolete("CustomOptions are obsolete. Use GetOption")] [Obsolete("CustomOptions are obsolete. Use GetOption")]
public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber);
/// <summary> /// <summary>
/// Gets a single value method option for this descriptor /// Gets a single value method option for this descriptor
......
...@@ -106,7 +106,7 @@ namespace Google.Protobuf.Reflection ...@@ -106,7 +106,7 @@ namespace Google.Protobuf.Reflection
/// The (possibly empty) set of custom options for this oneof. /// The (possibly empty) set of custom options for this oneof.
/// </summary> /// </summary>
[Obsolete("CustomOptions are obsolete. Use GetOption")] [Obsolete("CustomOptions are obsolete. Use GetOption")]
public CustomOptions CustomOptions => new CustomOptions(proto.Options._extensions?.ValuesByNumber); public CustomOptions CustomOptions => new CustomOptions(proto.Options?._extensions?.ValuesByNumber);
/// <summary> /// <summary>
/// Gets a single value oneof option for this descriptor /// Gets a single value oneof option for this descriptor
......
...@@ -95,7 +95,7 @@ namespace Google.Protobuf.Reflection ...@@ -95,7 +95,7 @@ namespace Google.Protobuf.Reflection
/// The (possibly empty) set of custom options for this service. /// The (possibly empty) set of custom options for this service.
/// </summary> /// </summary>
[Obsolete("CustomOptions are obsolete. Use GetOption")] [Obsolete("CustomOptions are obsolete. Use GetOption")]
public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber);
/// <summary> /// <summary>
/// Gets a single value service option for this descriptor /// Gets a single value service option for this descriptor
......
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