From 2d03acd41255634b0d59222e10f5d5efd6e4dc3a Mon Sep 17 00:00:00 2001 From: Sydney Acksman Date: Thu, 31 Oct 2019 15:13:26 -0500 Subject: [PATCH] Backport null checks for custom options --- .../Reflection/CustomOptionsTest.cs | 16 ++++++++++++++++ .../Google.Protobuf/Reflection/EnumDescriptor.cs | 2 +- .../Reflection/EnumValueDescriptor.cs | 2 +- .../Reflection/FieldDescriptor.cs | 2 +- .../Google.Protobuf/Reflection/FileDescriptor.cs | 2 +- .../Reflection/MessageDescriptor.cs | 2 +- .../Reflection/MethodDescriptor.cs | 2 +- .../Reflection/OneofDescriptor.cs | 2 +- .../Reflection/ServiceDescriptor.cs | 2 +- 9 files changed, 24 insertions(+), 8 deletions(-) diff --git a/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs b/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs index ff4e9a7ffd05c..d3858c3143d13 100644 --- a/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs +++ b/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs @@ -40,6 +40,7 @@ using static UnitTest.Issues.TestProtos.ComplexOptionType2.Types; using static UnitTest.Issues.TestProtos.DummyMessageContainingEnum.Types; using static Google.Protobuf.Test.Reflection.CustomOptionNumber; +using Google.Protobuf.TestProtos; namespace Google.Protobuf.Test.Reflection { @@ -202,6 +203,21 @@ public void AggregateOptions() AssertOption(new Aggregate { S = "FieldAnnotation" }, fieldOptions.TryGetMessage, AggregateFieldOpt); } + [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, CustomOptionNumber field) { T actual; diff --git a/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs index 253c2c9456fb2..2aece2f97ab00 100644 --- a/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs @@ -129,7 +129,7 @@ public EnumValueDescriptor FindValueByName(string name) /// The (possibly empty) set of custom options for this enum. /// //[Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => Proto.Options.CustomOptions; + public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; /* // uncomment this in the full proto2 support PR /// diff --git a/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs index d8c4cae8be88f..07e044b21da1d 100644 --- a/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs @@ -74,7 +74,7 @@ internal EnumValueDescriptor(EnumValueDescriptorProto proto, FileDescriptor file /// The (possibly empty) set of custom options for this enum value. /// //[Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => Proto.Options.CustomOptions; + public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; /* // uncomment this in the full proto2 support PR /// diff --git a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs index f8297f73294e9..cbda28d581aae 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs @@ -278,7 +278,7 @@ public MessageDescriptor ExtendeeType /// The (possibly empty) set of custom options for this field. /// //[Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => Proto.Options.CustomOptions; + public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; /* // uncomment this in the full proto2 support PR /// diff --git a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs index 9d449a999d53a..fee27cf2ef46b 100644 --- a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs @@ -505,7 +505,7 @@ public override string ToString() /// The (possibly empty) set of custom options for this file. /// //[Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => Proto.Options.CustomOptions; + public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; /* // uncomment this in the full proto2 support PR /// diff --git a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs index aeaafd0fcd397..68373e0e619a7 100644 --- a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs @@ -244,7 +244,7 @@ public T FindDescriptor(string name) where T : class, IDescriptor => /// The (possibly empty) set of custom options for this message. /// //[Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => Proto.Options.CustomOptions; + public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; /* // uncomment this in the full proto2 support PR /// diff --git a/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs index d18e700ac6968..7fdbc1351c21e 100644 --- a/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs @@ -74,7 +74,7 @@ public sealed class MethodDescriptor : DescriptorBase /// The (possibly empty) set of custom options for this method. /// //[Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => Proto.Options.CustomOptions; + public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; /* // uncomment this in the full proto2 support PR /// diff --git a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs index d811b5c8a508c..527732cfaf86b 100644 --- a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs @@ -106,7 +106,7 @@ public MessageDescriptor ContainingType /// The (possibly empty) set of custom options for this oneof. /// //[Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => proto.Options.CustomOptions; + public CustomOptions CustomOptions => proto.Options?.CustomOptions ?? CustomOptions.Empty; /* // uncomment this in the full proto2 support PR /// diff --git a/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs index 848fe09171394..4e6d1a14d10f5 100644 --- a/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs @@ -95,7 +95,7 @@ public MethodDescriptor FindMethodByName(String name) /// The (possibly empty) set of custom options for this service. /// //[Obsolete("CustomOptions are obsolete. Use GetOption")] - public CustomOptions CustomOptions => Proto.Options.CustomOptions; + public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty; /* // uncomment this in the full proto2 support PR ///