Apply Obsolete attribute to deprecated enums and enum values in C# generated code

Fixes #10513
pull/10520/head
Jon Skeet 2 years ago
parent 687fbffd9b
commit 5036705c8d
  1. 4
      csharp/protos/unittest_issues.proto
  2. 78
      csharp/src/Google.Protobuf.Test.TestProtos/UnittestIssues.cs
  3. 21
      csharp/src/Google.Protobuf.Test/DeprecatedMemberTest.cs
  4. BIN
      csharp/src/Google.Protobuf.Test/testprotos.pb
  5. 6
      src/google/protobuf/compiler/csharp/csharp_enum.cc

@ -59,10 +59,12 @@ message NegativeEnumMessage {
// Decorate fields with [deprecated=true] as [System.Obsolete]
message DeprecatedChild {
option deprecated = true;
}
enum DeprecatedEnum {
DEPRECATED_ZERO = 0;
option deprecated = true;
DEPRECATED_ZERO = 0 [deprecated = true];
one = 1;
}

@ -30,43 +30,44 @@ namespace UnitTest.Issues.TestProtos {
"EiwKBXZhbHVlGAEgASgOMh0udW5pdHRlc3RfaXNzdWVzLk5lZ2F0aXZlRW51",
"bRIxCgZ2YWx1ZXMYAiADKA4yHS51bml0dGVzdF9pc3N1ZXMuTmVnYXRpdmVF",
"bnVtQgIQABI4Cg1wYWNrZWRfdmFsdWVzGAMgAygOMh0udW5pdHRlc3RfaXNz",
"dWVzLk5lZ2F0aXZlRW51bUICEAEiEQoPRGVwcmVjYXRlZENoaWxkIrkCChdE",
"ZXByZWNhdGVkRmllbGRzTWVzc2FnZRIaCg5QcmltaXRpdmVWYWx1ZRgBIAEo",
"BUICGAESGgoOUHJpbWl0aXZlQXJyYXkYAiADKAVCAhgBEjoKDE1lc3NhZ2VW",
"YWx1ZRgDIAEoCzIgLnVuaXR0ZXN0X2lzc3Vlcy5EZXByZWNhdGVkQ2hpbGRC",
"AhgBEjoKDE1lc3NhZ2VBcnJheRgEIAMoCzIgLnVuaXR0ZXN0X2lzc3Vlcy5E",
"ZXByZWNhdGVkQ2hpbGRCAhgBEjYKCUVudW1WYWx1ZRgFIAEoDjIfLnVuaXR0",
"ZXN0X2lzc3Vlcy5EZXByZ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5OdWxsVmFsdWVIAEIHCgV2YWx1ZSJFChNOdWxsVmFs",
"dWVOb3RJbk9uZW9mEi4KCm51bGxfdmFsdWUYAiABKA4yGi5nb29nbGUucHJv",
"dG9idWYuTnVsbFZhbHVlImAKF01peGVkUmVndWxhckFuZE9wdGlvbmFsEhUK",
"DXJlZ3VsYXJfZmllbGQYASABKAkSGwoOb3B0aW9uYWxfZmllbGQYAiABKAlI",
"AIgBAUIRCg9fb3B0aW9uYWxfZmllbGQiOQoST25lb2ZXaXRoTm9uZUZpZWxk",
"EgsKAXgYASABKAlIABIOCgRub25lGAIgASgJSABCBgoEdGVzdCI1ChFPbmVv",
"ZldpdGhOb25lTmFtZRILCgF4GAEgASgJSAASCwoBeRgCIAEoCUgAQgYKBG5v",
"bmUikwIKGURpc2FtYmlndWF0ZUNvbW1vbk1lbWJlcnMSIwobZGlzYW1iaWd1",
"YXRlX2NvbW1vbl9tZW1iZXJzGAEgASgFEg0KBXR5cGVzGAIgASgFEhIKCmRl",
"c2NyaXB0b3IYAyABKAUSDgoGZXF1YWxzGAQgASgFEhEKCXRvX3N0cmluZxgF",
"IAEoBRIVCg1nZXRfaGFzaF9jb2RlGAYgASgFEhAKCHdyaXRlX3RvGAcgASgF",
"Eg0KBWNsb25lGAggASgFEhYKDmNhbGN1bGF0ZV9zaXplGAkgASgFEhIKCm1l",
"cmdlX2Zyb20YCiABKAUSFwoPb25fY29uc3RydWN0aW9uGAsgASgFEg4KBnBh",
"cnNlchgMIAEoBSpVCgxOZWdhdGl2ZUVudW0SFgoSTkVHQVRJVkVfRU5VTV9a",
"RVJPEAASFgoJRml2ZUJlbG93EPv//////////wESFQoITWludXNPbmUQ////",
"////////ASo2Cg5EZXByZWNhdGVkRW51bRIXCg9ERVBSRUNBVEVEX1pFUk8Q",
"ABoCCAESBwoDb25lEAEaAhgBQh2qAhpVbml0VGVzdC5Jc3N1ZXMuVGVzdFBy",
"b3Rvc2IGcHJvdG8z"));
descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
new pbr::FileDescriptor[] { global::Google.Protobuf.WellKnownTypes.StructReflection.Descriptor, },
new pbr::GeneratedClrTypeInfo(new[] {typeof(global::UnitTest.Issues.TestProtos.NegativeEnum), typeof(global::UnitTest.Issues.TestProtos.DeprecatedEnum), }, null, new pbr::GeneratedClrTypeInfo[] {
@ -97,7 +98,9 @@ namespace UnitTest.Issues.TestProtos {
[pbr::OriginalName("MinusOne")] MinusOne = -1,
}
[global::System.ObsoleteAttribute]
public enum DeprecatedEnum {
[global::System.ObsoleteAttribute]
[pbr::OriginalName("DEPRECATED_ZERO")] DeprecatedZero = 0,
[pbr::OriginalName("one")] One = 1,
}
@ -826,6 +829,7 @@ namespace UnitTest.Issues.TestProtos {
}
[global::System.ObsoleteAttribute]
public sealed partial class DeprecatedChild : pb::IMessage<DeprecatedChild>
#if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
, pb::IBufferMessage

@ -34,7 +34,9 @@ using System;
using System.Reflection;
using Google.Protobuf.TestProtos;
using NUnit.Framework;
using UnitTest.Issues.TestProtos;
#pragma warning disable CS0612 // Type or member is obsolete
namespace Google.Protobuf
{
public class DeprecatedMemberTest
@ -46,9 +48,20 @@ namespace Google.Protobuf
}
[Test]
public void TestDepreatedPrimitiveValue()
{
AssertIsDeprecated(typeof(TestDeprecatedFields).GetProperty("DeprecatedInt32"));
}
public void TestDepreatedPrimitiveValue() =>
AssertIsDeprecated(typeof(TestDeprecatedFields).GetProperty(nameof(TestDeprecatedFields.DeprecatedInt32)));
[Test]
public void TestDeprecatedMessage() =>
AssertIsDeprecated(typeof(DeprecatedChild));
[Test]
public void TestDeprecatedEnum() =>
AssertIsDeprecated(typeof(DeprecatedEnum));
[Test]
public void TestDeprecatedEnumValue() =>
AssertIsDeprecated(typeof(DeprecatedEnum).GetField(nameof(DeprecatedEnum.DeprecatedZero)));
}
}
#pragma warning restore CS0612 // Type or member is obsolete

@ -61,6 +61,9 @@ EnumGenerator::~EnumGenerator() {
void EnumGenerator::Generate(io::Printer* printer) {
WriteEnumDocComment(printer, descriptor_);
if (descriptor_->options().deprecated()) {
printer->Print("[global::System.ObsoleteAttribute]\n");
}
printer->Print("$access_level$ enum $name$ {\n",
"access_level", class_access_level(),
"name", descriptor_->name());
@ -69,6 +72,9 @@ void EnumGenerator::Generate(io::Printer* printer) {
std::set<int> used_number;
for (int i = 0; i < descriptor_->value_count(); i++) {
WriteEnumValueDocComment(printer, descriptor_->value(i));
if (descriptor_->value(i)->options().deprecated()) {
printer->Print("[global::System.ObsoleteAttribute]\n");
}
std::string original_name = descriptor_->value(i)->name();
std::string name =
GetEnumValueName(descriptor_->name(), descriptor_->value(i)->name());

Loading…
Cancel
Save