Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport null checks for custom options #6831

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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>(T expected, OptionFetcher<T> fetcher, CustomOptionNumber field)
{
T actual;
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf/Reflection/EnumDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public EnumValueDescriptor FindValueByName(string name)
/// The (possibly empty) set of custom options for this enum.
/// </summary>
//[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
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal EnumValueDescriptor(EnumValueDescriptorProto proto, FileDescriptor file
/// The (possibly empty) set of custom options for this enum value.
/// </summary>
//[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
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public MessageDescriptor ExtendeeType
/// The (possibly empty) set of custom options for this field.
/// </summary>
//[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
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public override string ToString()
/// The (possibly empty) set of custom options for this file.
/// </summary>
//[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
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public T FindDescriptor<T>(string name) where T : class, IDescriptor =>
/// The (possibly empty) set of custom options for this message.
/// </summary>
//[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
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf/Reflection/MethodDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public sealed class MethodDescriptor : DescriptorBase
/// The (possibly empty) set of custom options for this method.
/// </summary>
//[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
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public MessageDescriptor ContainingType
/// The (possibly empty) set of custom options for this oneof.
/// </summary>
//[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
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Google.Protobuf/Reflection/ServiceDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public MethodDescriptor FindMethodByName(String name)
/// The (possibly empty) set of custom options for this service.
/// </summary>
//[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
/// <summary>
Expand Down