diff --git a/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs b/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs index 02fe778fbc..d65a6f2023 100644 --- a/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs +++ b/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs @@ -41,6 +41,7 @@ using static Google.Protobuf.WireFormat; using static UnitTest.Issues.TestProtos.ComplexOptionType2.Types; using static UnitTest.Issues.TestProtos.UnittestCustomOptionsProto3Extensions; using static UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Types; +using Google.Protobuf.TestProtos; #pragma warning disable CS0618 @@ -177,6 +178,21 @@ namespace Google.Protobuf.Test.Reflection 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 expected, OptionFetcher fetcher, Extension extension, Func, T> descriptorOptionFetcher) where D : IExtendableMessage { T customOptionsValue; diff --git a/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs index 33be96198d..264a88a063 100644 --- a/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs @@ -129,7 +129,7 @@ namespace Google.Protobuf.Reflection /// The (possibly empty) set of custom options for this enum. /// [Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); /// /// Gets a single value enum option for this descriptor diff --git a/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs index a476ef1f91..3933820105 100644 --- a/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs @@ -74,7 +74,7 @@ namespace Google.Protobuf.Reflection /// The (possibly empty) set of custom options for this enum value. /// [Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); /// /// Gets a single value enum value option for this descriptor diff --git a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs index a81bc9ccce..ddd671aadb 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs @@ -299,7 +299,7 @@ namespace Google.Protobuf.Reflection /// The (possibly empty) set of custom options for this field. /// [Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); /// /// Gets a single value field option for this descriptor diff --git a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs index 83849a2b25..dbb6b155e9 100644 --- a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs @@ -542,7 +542,7 @@ namespace Google.Protobuf.Reflection /// The (possibly empty) set of custom options for this file. /// [Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); /// /// Gets a single value file option for this descriptor diff --git a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs index 944f6e88d2..e278514094 100644 --- a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs @@ -261,7 +261,7 @@ namespace Google.Protobuf.Reflection /// The (possibly empty) set of custom options for this message. /// [Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); /// /// Gets a single value message option for this descriptor diff --git a/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs index 13d239601a..92250ba662 100644 --- a/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs @@ -74,7 +74,7 @@ namespace Google.Protobuf.Reflection /// The (possibly empty) set of custom options for this method. /// [Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); /// /// Gets a single value method option for this descriptor diff --git a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs index 655c07e64b..1e30b92ed6 100644 --- a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs @@ -106,7 +106,7 @@ namespace Google.Protobuf.Reflection /// The (possibly empty) set of custom options for this oneof. /// [Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => new CustomOptions(proto.Options._extensions?.ValuesByNumber); + public CustomOptions CustomOptions => new CustomOptions(proto.Options?._extensions?.ValuesByNumber); /// /// Gets a single value oneof option for this descriptor diff --git a/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs index 120c6c6443..ba310ad9f9 100644 --- a/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs @@ -95,7 +95,7 @@ namespace Google.Protobuf.Reflection /// The (possibly empty) set of custom options for this service. /// [Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => new CustomOptions(Proto.Options._extensions?.ValuesByNumber); + public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); /// /// Gets a single value service option for this descriptor