diff --git a/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs b/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs index bfee5f5d439b..d066baec9375 100644 --- a/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs +++ b/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs @@ -71,25 +71,49 @@ OptionFetcher EnumFetcher(CustomOptions options) }; } + [Test] + public void BuiltinOptionsCanBeRetrieved() + { + // non-custom options (that are not extensions but regular fields) can only be accessed via descriptor.Options + var fileOptions = UnittestProto3Reflection.Descriptor.GetOptions(); + Assert.AreEqual("Google.Protobuf.TestProtos", fileOptions.CsharpNamespace); + } + + [Test] + public void OptionPresenceCanBeDetected() + { + // case 1: the descriptor has no options at all so the options message is not present + Assert.IsNull(TestAllTypes.Descriptor.GetOptions()); + + // case 2: the descriptor has some options, but not the one we're looking for + // HasExtension will be false and GetExtension returns extension's default value + Assert.IsFalse(UnittestProto3Reflection.Descriptor.GetOptions().HasExtension(FileOpt1)); + Assert.AreEqual(0, UnittestProto3Reflection.Descriptor.GetOptions().GetExtension(FileOpt1)); + + // case 3: option is present + Assert.IsTrue(UnittestCustomOptionsProto3Reflection.Descriptor.GetOptions().HasExtension(FileOpt1)); + Assert.AreEqual(9876543210UL, UnittestCustomOptionsProto3Reflection.Descriptor.GetOptions().GetExtension(FileOpt1)); + } + [Test] public void ScalarOptions() { var d = CustomOptionOtherValues.Descriptor; - var options = d.CustomOptions; - AssertOption(-100, options.TryGetInt32, Int32Opt, d.GetOption); - AssertOption(12.3456789f, options.TryGetFloat, FloatOpt, d.GetOption); - AssertOption(1.234567890123456789d, options.TryGetDouble, DoubleOpt, d.GetOption); - AssertOption("Hello, \"World\"", options.TryGetString, StringOpt, d.GetOption); - AssertOption(ByteString.CopyFromUtf8("Hello\0World"), options.TryGetBytes, BytesOpt, d.GetOption); - AssertOption(TestEnumType.TestOptionEnumType2, EnumFetcher(options), EnumOpt, d.GetOption); + var customOptions = d.CustomOptions; + AssertOption(-100, customOptions.TryGetInt32, Int32Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(12.3456789f, customOptions.TryGetFloat, FloatOpt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(1.234567890123456789d, customOptions.TryGetDouble, DoubleOpt, d.GetOption, d.GetOptions().GetExtension); + AssertOption("Hello, \"World\"", customOptions.TryGetString, StringOpt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(ByteString.CopyFromUtf8("Hello\0World"), customOptions.TryGetBytes, BytesOpt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(TestEnumType.TestOptionEnumType2, EnumFetcher(customOptions), EnumOpt, d.GetOption, d.GetOptions().GetExtension); } [Test] public void MessageOptions() { var d = VariousComplexOptions.Descriptor; - var options = d.CustomOptions; - AssertOption(new ComplexOptionType1 { Foo = 42, Foo4 = { 99, 88 } }, options.TryGetMessage, ComplexOpt1, d.GetOption); + var customOptions = d.CustomOptions; + AssertOption(new ComplexOptionType1 { Foo = 42, Foo4 = { 99, 88 } }, customOptions.TryGetMessage, ComplexOpt1, d.GetOption, d.GetOptions().GetExtension); AssertOption(new ComplexOptionType2 { Baz = 987, @@ -97,85 +121,84 @@ public void MessageOptions() Fred = new ComplexOptionType4 { Waldo = 321 }, Barney = { new ComplexOptionType4 { Waldo = 101 }, new ComplexOptionType4 { Waldo = 212 } } }, - options.TryGetMessage, ComplexOpt2, d.GetOption); - AssertOption(new ComplexOptionType3 { Qux = 9 }, options.TryGetMessage, ComplexOpt3, d.GetOption); + customOptions.TryGetMessage, ComplexOpt2, d.GetOption, d.GetOptions().GetExtension); + AssertOption(new ComplexOptionType3 { Qux = 9 }, customOptions.TryGetMessage, ComplexOpt3, d.GetOption, d.GetOptions().GetExtension); } [Test] public void OptionLocations() { - var fileOptions = UnittestCustomOptionsProto3Reflection.Descriptor.CustomOptions; - AssertOption(9876543210UL, fileOptions.TryGetUInt64, FileOpt1, UnittestCustomOptionsProto3Reflection.Descriptor.GetOption); + var fileDescriptor = UnittestCustomOptionsProto3Reflection.Descriptor; + AssertOption(9876543210UL, fileDescriptor.CustomOptions.TryGetUInt64, FileOpt1, fileDescriptor.GetOption, fileDescriptor.GetOptions().GetExtension); - var messageOptions = TestMessageWithCustomOptions.Descriptor.CustomOptions; - AssertOption(-56, messageOptions.TryGetInt32, MessageOpt1, TestMessageWithCustomOptions.Descriptor.GetOption); + var messageDescriptor = TestMessageWithCustomOptions.Descriptor; + AssertOption(-56, messageDescriptor.CustomOptions.TryGetInt32, MessageOpt1, messageDescriptor.GetOption, messageDescriptor.GetOptions().GetExtension); - var fieldOptions = TestMessageWithCustomOptions.Descriptor.Fields["field1"].CustomOptions; - AssertOption(8765432109UL, fieldOptions.TryGetFixed64, FieldOpt1, TestMessageWithCustomOptions.Descriptor.Fields["field1"].GetOption); + var fieldDescriptor = TestMessageWithCustomOptions.Descriptor.Fields["field1"]; + AssertOption(8765432109UL, fieldDescriptor.CustomOptions.TryGetFixed64, FieldOpt1, fieldDescriptor.GetOption, fieldDescriptor.GetOptions().GetExtension); - var oneofOptions = TestMessageWithCustomOptions.Descriptor.Oneofs[0].CustomOptions; - AssertOption(-99, oneofOptions.TryGetInt32, OneofOpt1, TestMessageWithCustomOptions.Descriptor.Oneofs[0].GetOption); + var oneofDescriptor = TestMessageWithCustomOptions.Descriptor.Oneofs[0]; + AssertOption(-99, oneofDescriptor.CustomOptions.TryGetInt32, OneofOpt1, oneofDescriptor.GetOption, oneofDescriptor.GetOptions().GetExtension); - var enumOptions = TestMessageWithCustomOptions.Descriptor.EnumTypes[0].CustomOptions; - AssertOption(-789, enumOptions.TryGetSFixed32, EnumOpt1, TestMessageWithCustomOptions.Descriptor.EnumTypes[0].GetOption); + var enumDescriptor = TestMessageWithCustomOptions.Descriptor.EnumTypes[0]; + AssertOption(-789, enumDescriptor.CustomOptions.TryGetSFixed32, EnumOpt1, enumDescriptor.GetOption, enumDescriptor.GetOptions().GetExtension); - var enumValueOptions = TestMessageWithCustomOptions.Descriptor.EnumTypes[0].FindValueByNumber(2).CustomOptions; - AssertOption(123, enumValueOptions.TryGetInt32, EnumValueOpt1, TestMessageWithCustomOptions.Descriptor.EnumTypes[0].FindValueByNumber(2).GetOption); + var enumValueDescriptor = TestMessageWithCustomOptions.Descriptor.EnumTypes[0].FindValueByNumber(2); + AssertOption(123, enumValueDescriptor.CustomOptions.TryGetInt32, EnumValueOpt1, enumValueDescriptor.GetOption, enumValueDescriptor.GetOptions().GetExtension); - var service = UnittestCustomOptionsProto3Reflection.Descriptor.Services + var serviceDescriptor = UnittestCustomOptionsProto3Reflection.Descriptor.Services .Single(s => s.Name == "TestServiceWithCustomOptions"); - var serviceOptions = service.CustomOptions; - AssertOption(-9876543210, serviceOptions.TryGetSInt64, ServiceOpt1, service.GetOption); + AssertOption(-9876543210, serviceDescriptor.CustomOptions.TryGetSInt64, ServiceOpt1, serviceDescriptor.GetOption, serviceDescriptor.GetOptions().GetExtension); - var methodOptions = service.Methods[0].CustomOptions; - AssertOption(UnitTest.Issues.TestProtos.MethodOpt1.Val2, EnumFetcher(methodOptions), UnittestCustomOptionsProto3Extensions.MethodOpt1, service.Methods[0].GetOption); + var methodDescriptor = serviceDescriptor.Methods[0]; + AssertOption(UnitTest.Issues.TestProtos.MethodOpt1.Val2, EnumFetcher(methodDescriptor.CustomOptions), UnittestCustomOptionsProto3Extensions.MethodOpt1, methodDescriptor.GetOption, methodDescriptor.GetOptions().GetExtension); } [Test] public void MinValues() { var d = CustomOptionMinIntegerValues.Descriptor; - var options = d.CustomOptions; - AssertOption(false, options.TryGetBool, BoolOpt, d.GetOption); - AssertOption(int.MinValue, options.TryGetInt32, Int32Opt, d.GetOption); - AssertOption(long.MinValue, options.TryGetInt64, Int64Opt, d.GetOption); - AssertOption(uint.MinValue, options.TryGetUInt32, Uint32Opt, d.GetOption); - AssertOption(ulong.MinValue, options.TryGetUInt64, Uint64Opt, d.GetOption); - AssertOption(int.MinValue, options.TryGetSInt32, Sint32Opt, d.GetOption); - AssertOption(long.MinValue, options.TryGetSInt64, Sint64Opt, d.GetOption); - AssertOption(uint.MinValue, options.TryGetUInt32, Fixed32Opt, d.GetOption); - AssertOption(ulong.MinValue, options.TryGetUInt64, Fixed64Opt, d.GetOption); - AssertOption(int.MinValue, options.TryGetInt32, Sfixed32Opt, d.GetOption); - AssertOption(long.MinValue, options.TryGetInt64, Sfixed64Opt, d.GetOption); + var customOptions = d.CustomOptions; + AssertOption(false, customOptions.TryGetBool, BoolOpt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(int.MinValue, customOptions.TryGetInt32, Int32Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(long.MinValue, customOptions.TryGetInt64, Int64Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(uint.MinValue, customOptions.TryGetUInt32, Uint32Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(ulong.MinValue, customOptions.TryGetUInt64, Uint64Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(int.MinValue, customOptions.TryGetSInt32, Sint32Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(long.MinValue, customOptions.TryGetSInt64, Sint64Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(uint.MinValue, customOptions.TryGetUInt32, Fixed32Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(ulong.MinValue, customOptions.TryGetUInt64, Fixed64Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(int.MinValue, customOptions.TryGetInt32, Sfixed32Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(long.MinValue, customOptions.TryGetInt64, Sfixed64Opt, d.GetOption, d.GetOptions().GetExtension); } [Test] public void MaxValues() { var d = CustomOptionMaxIntegerValues.Descriptor; - var options = d.CustomOptions; - AssertOption(true, options.TryGetBool, BoolOpt, d.GetOption); - AssertOption(int.MaxValue, options.TryGetInt32, Int32Opt, d.GetOption); - AssertOption(long.MaxValue, options.TryGetInt64, Int64Opt, d.GetOption); - AssertOption(uint.MaxValue, options.TryGetUInt32, Uint32Opt, d.GetOption); - AssertOption(ulong.MaxValue, options.TryGetUInt64, Uint64Opt, d.GetOption); - AssertOption(int.MaxValue, options.TryGetSInt32, Sint32Opt, d.GetOption); - AssertOption(long.MaxValue, options.TryGetSInt64, Sint64Opt, d.GetOption); - AssertOption(uint.MaxValue, options.TryGetFixed32, Fixed32Opt, d.GetOption); - AssertOption(ulong.MaxValue, options.TryGetFixed64, Fixed64Opt, d.GetOption); - AssertOption(int.MaxValue, options.TryGetSFixed32, Sfixed32Opt, d.GetOption); - AssertOption(long.MaxValue, options.TryGetSFixed64, Sfixed64Opt, d.GetOption); + var customOptions = d.CustomOptions; + AssertOption(true, customOptions.TryGetBool, BoolOpt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(int.MaxValue, customOptions.TryGetInt32, Int32Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(long.MaxValue, customOptions.TryGetInt64, Int64Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(uint.MaxValue, customOptions.TryGetUInt32, Uint32Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(ulong.MaxValue, customOptions.TryGetUInt64, Uint64Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(int.MaxValue, customOptions.TryGetSInt32, Sint32Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(long.MaxValue, customOptions.TryGetSInt64, Sint64Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(uint.MaxValue, customOptions.TryGetFixed32, Fixed32Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(ulong.MaxValue, customOptions.TryGetFixed64, Fixed64Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(int.MaxValue, customOptions.TryGetSFixed32, Sfixed32Opt, d.GetOption, d.GetOptions().GetExtension); + AssertOption(long.MaxValue, customOptions.TryGetSFixed64, Sfixed64Opt, d.GetOption, d.GetOptions().GetExtension); } [Test] public void AggregateOptions() { // Just two examples - var messageOptions = AggregateMessage.Descriptor.CustomOptions; - AssertOption(new Aggregate { I = 101, S = "MessageAnnotation" }, messageOptions.TryGetMessage, Msgopt, AggregateMessage.Descriptor.GetOption); + var messageDescriptor = AggregateMessage.Descriptor; + AssertOption(new Aggregate { I = 101, S = "MessageAnnotation" }, messageDescriptor.CustomOptions.TryGetMessage, Msgopt, messageDescriptor.GetOption, messageDescriptor.GetOptions().GetExtension); - var fieldOptions = AggregateMessage.Descriptor.Fields["fieldname"].CustomOptions; - AssertOption(new Aggregate { S = "FieldAnnotation" }, fieldOptions.TryGetMessage, Fieldopt, AggregateMessage.Descriptor.Fields["fieldname"].GetOption); + var fieldDescriptor = messageDescriptor.Fields["fieldname"]; + AssertOption(new Aggregate { S = "FieldAnnotation" }, fieldDescriptor.CustomOptions.TryGetMessage, Fieldopt, fieldDescriptor.GetOption, fieldDescriptor.GetOptions().GetExtension); } [Test] @@ -199,16 +222,19 @@ public void MultipleImportOfSameFileWithExtension() var descriptor = UnittestIssue6936CReflection.Descriptor; var foo = Foo.Descriptor; var bar = Bar.Descriptor; - AssertOption("foo", foo.CustomOptions.TryGetString, UnittestIssue6936AExtensions.Opt, foo.GetOption); - AssertOption("bar", bar.CustomOptions.TryGetString, UnittestIssue6936AExtensions.Opt, bar.GetOption); + AssertOption("foo", foo.CustomOptions.TryGetString, UnittestIssue6936AExtensions.Opt, foo.GetOption, foo.GetOptions().GetExtension); + AssertOption("bar", bar.CustomOptions.TryGetString, UnittestIssue6936AExtensions.Opt, bar.GetOption, bar.GetOptions().GetExtension); } - private void AssertOption(T expected, OptionFetcher fetcher, Extension extension, Func, T> descriptorOptionFetcher) where D : IExtendableMessage + private void AssertOption(T expected, OptionFetcher customOptionFetcher, Extension extension, Func, T> getOptionFetcher, Func, T> extensionFetcher) where D : IExtendableMessage { - T customOptionsValue; - T extensionValue = descriptorOptionFetcher(extension); - Assert.IsTrue(fetcher(extension.FieldNumber, out customOptionsValue)); + Assert.IsTrue(customOptionFetcher(extension.FieldNumber, out T customOptionsValue)); Assert.AreEqual(expected, customOptionsValue); + + T getOptionValue = getOptionFetcher(extension); + Assert.AreEqual(expected, getOptionValue); + + T extensionValue = extensionFetcher(extension); Assert.AreEqual(expected, extensionValue); } } diff --git a/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs index 264a88a063bc..f7e8b5b5f28d 100644 --- a/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs @@ -128,12 +128,21 @@ public EnumValueDescriptor FindValueByName(string name) /// /// The (possibly empty) set of custom options for this enum. /// - [Obsolete("CustomOptions are obsolete. Use GetOption")] + [Obsolete("CustomOptions are obsolete. Use the GetOptions() method.")] public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); + /// + /// The EnumOptions, defined in descriptor.proto. + /// If the options message is not present (i.e. there are no options), null is returned. + /// Custom options can be retrieved as extensions of the returned message. + /// NOTE: A defensive copy is created each time this property is retrieved. + /// + public EnumOptions GetOptions() => Proto.Options?.Clone(); + /// /// Gets a single value enum option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public T GetOption(Extension extension) { var value = Proto.Options.GetExtension(extension); @@ -143,6 +152,7 @@ public T GetOption(Extension extension) /// /// Gets a repeated value enum option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public RepeatedField GetOption(RepeatedExtension extension) { return Proto.Options.GetExtension(extension).Clone(); diff --git a/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs index 393382010559..05097bd1da3d 100644 --- a/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/EnumValueDescriptor.cs @@ -73,12 +73,21 @@ public sealed class EnumValueDescriptor : DescriptorBase /// /// The (possibly empty) set of custom options for this enum value. /// - [Obsolete("CustomOptions are obsolete. Use GetOption")] + [Obsolete("CustomOptions are obsolete. Use the GetOptions() method.")] public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); + /// + /// The EnumValueOptions, defined in descriptor.proto. + /// If the options message is not present (i.e. there are no options), null is returned. + /// Custom options can be retrieved as extensions of the returned message. + /// NOTE: A defensive copy is created each time this property is retrieved. + /// + public EnumValueOptions GetOptions() => Proto.Options?.Clone(); + /// /// Gets a single value enum value option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public T GetOption(Extension extension) { var value = Proto.Options.GetExtension(extension); @@ -88,6 +97,7 @@ public T GetOption(Extension extension) /// /// Gets a repeated value enum value option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public RepeatedField GetOption(RepeatedExtension extension) { return Proto.Options.GetExtension(extension).Clone(); diff --git a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs index 69bab4f01045..e3068d803813 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs @@ -304,12 +304,21 @@ public MessageDescriptor ExtendeeType /// /// The (possibly empty) set of custom options for this field. /// - [Obsolete("CustomOptions are obsolete. Use GetOption")] + [Obsolete("CustomOptions are obsolete. Use the GetOptions() method.")] public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); + /// + /// The FieldOptions, defined in descriptor.proto. + /// If the options message is not present (i.e. there are no options), null is returned. + /// Custom options can be retrieved as extensions of the returned message. + /// NOTE: A defensive copy is created each time this property is retrieved. + /// + public FieldOptions GetOptions() => Proto.Options?.Clone(); + /// /// Gets a single value field option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public T GetOption(Extension extension) { var value = Proto.Options.GetExtension(extension); @@ -319,6 +328,7 @@ public T GetOption(Extension extension) /// /// Gets a repeated value field option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public RepeatedField GetOption(RepeatedExtension extension) { return Proto.Options.GetExtension(extension).Clone(); diff --git a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs index 56c0caacfde3..88e4a9de9621 100644 --- a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs @@ -547,12 +547,21 @@ public override string ToString() /// /// The (possibly empty) set of custom options for this file. /// - [Obsolete("CustomOptions are obsolete. Use GetOption")] + [Obsolete("CustomOptions are obsolete. Use the GetOptions() method.")] public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); + /// + /// The FileOptions, defined in descriptor.proto. + /// If the options message is not present (i.e. there are no options), null is returned. + /// Custom options can be retrieved as extensions of the returned message. + /// NOTE: A defensive copy is created each time this property is retrieved. + /// + public FileOptions GetOptions() => Proto.Options?.Clone(); + /// /// Gets a single value file option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public T GetOption(Extension extension) { var value = Proto.Options.GetExtension(extension); @@ -562,6 +571,7 @@ public T GetOption(Extension extension) /// /// Gets a repeated value file option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public RepeatedField GetOption(RepeatedExtension extension) { return Proto.Options.GetExtension(extension).Clone(); diff --git a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs index 6217081fbcef..7b5ab2fb48e7 100644 --- a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs @@ -287,12 +287,21 @@ internal bool IsExtensionsInitialized(IMessage message) /// /// The (possibly empty) set of custom options for this message. /// - [Obsolete("CustomOptions are obsolete. Use GetOption")] + [Obsolete("CustomOptions are obsolete. Use the GetOptions() method.")] public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); + /// + /// The MessageOptions, defined in descriptor.proto. + /// If the options message is not present (i.e. there are no options), null is returned. + /// Custom options can be retrieved as extensions of the returned message. + /// NOTE: A defensive copy is created each time this property is retrieved. + /// + public MessageOptions GetOptions() => Proto.Options?.Clone(); + /// /// Gets a single value message option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public T GetOption(Extension extension) { var value = Proto.Options.GetExtension(extension); @@ -302,6 +311,7 @@ public T GetOption(Extension extension) /// /// Gets a repeated value message option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public Collections.RepeatedField GetOption(RepeatedExtension extension) { return Proto.Options.GetExtension(extension).Clone(); diff --git a/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs index 92250ba662e0..8e1503767bd7 100644 --- a/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs @@ -73,12 +73,21 @@ public sealed class MethodDescriptor : DescriptorBase /// /// The (possibly empty) set of custom options for this method. /// - [Obsolete("CustomOptions are obsolete. Use GetOption")] + [Obsolete("CustomOptions are obsolete. Use the GetOptions() method.")] public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); + /// + /// The MethodOptions, defined in descriptor.proto. + /// If the options message is not present (i.e. there are no options), null is returned. + /// Custom options can be retrieved as extensions of the returned message. + /// NOTE: A defensive copy is created each time this property is retrieved. + /// + public MethodOptions GetOptions() => Proto.Options?.Clone(); + /// /// Gets a single value method option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public T GetOption(Extension extension) { var value = Proto.Options.GetExtension(extension); @@ -88,6 +97,7 @@ public T GetOption(Extension extension) /// /// Gets a repeated value method option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public RepeatedField GetOption(RepeatedExtension extension) { return Proto.Options.GetExtension(extension).Clone(); diff --git a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs index 7cceabd7c3e6..0df4f534b212 100644 --- a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs @@ -117,12 +117,21 @@ public MessageDescriptor ContainingType /// /// The (possibly empty) set of custom options for this oneof. /// - [Obsolete("CustomOptions are obsolete. Use GetOption")] + [Obsolete("CustomOptions are obsolete. Use the GetOptions method.")] public CustomOptions CustomOptions => new CustomOptions(proto.Options?._extensions?.ValuesByNumber); + /// + /// The OneofOptions, defined in descriptor.proto. + /// If the options message is not present (i.e. there are no options), null is returned. + /// Custom options can be retrieved as extensions of the returned message. + /// NOTE: A defensive copy is created each time this property is retrieved. + /// + public OneofOptions GetOptions() => proto.Options?.Clone(); + /// /// Gets a single value oneof option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public T GetOption(Extension extension) { var value = proto.Options.GetExtension(extension); @@ -132,6 +141,7 @@ public T GetOption(Extension extension) /// /// Gets a repeated value oneof option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public RepeatedField GetOption(RepeatedExtension extension) { return proto.Options.GetExtension(extension).Clone(); diff --git a/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs index 21417ec641e4..dab348b6f8ec 100644 --- a/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs @@ -94,12 +94,21 @@ public MethodDescriptor FindMethodByName(String name) /// /// The (possibly empty) set of custom options for this service. /// - [Obsolete("CustomOptions are obsolete. Use GetOption")] + [Obsolete("CustomOptions are obsolete. Use the GetOptions() method.")] public CustomOptions CustomOptions => new CustomOptions(Proto.Options?._extensions?.ValuesByNumber); + /// + /// The ServiceOptions, defined in descriptor.proto. + /// If the options message is not present (i.e. there are no options), null is returned. + /// Custom options can be retrieved as extensions of the returned message. + /// NOTE: A defensive copy is created each time this property is retrieved. + /// + public ServiceOptions GetOptions() => Proto.Options?.Clone(); + /// /// Gets a single value service option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public T GetOption(Extension extension) { var value = Proto.Options.GetExtension(extension); @@ -109,6 +118,7 @@ public T GetOption(Extension extension) /// /// Gets a repeated value service option for this descriptor /// + [Obsolete("GetOption is obsolete. Use the GetOptions() method.")] public RepeatedField GetOption(RepeatedExtension extension) { return Proto.Options.GetExtension(extension).Clone();