Skip to content

Commit

Permalink
protobuf-net#1149 Support deprecated options
Browse files Browse the repository at this point in the history
- Updated `MetaType` to check if `System.ObsoleteAttribute` exists on type or field to add deprecated option while generating proto.
- Added unit test to reproduce the issue.
  • Loading branch information
mrshridhara committed Apr 23, 2024
1 parent 93b8be6 commit bfee7a4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/protobuf-net.Test/Issues/Issue1149.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using ProtoBuf.Meta;
using Xunit;

namespace ProtoBuf.Test.Issues
{
#pragma warning disable CS0612 // Type or member is obsolete
public sealed class Issue1149
{
[Fact]
public void GenerateProtoWithDeprecatedOption()
{
const string expected = @"syntax = ""proto3"";
package DefaultPackage;
message SomeClass {
option deprecated = true;
int32 SomeProperty = 1;
}
enum SomeEnum {
option deprecated = true;
One = 0;
Two = 1;
Three = 2;
}
message SomeOtherClass {
string SomeOtherProperty = 1 [deprecated = true];
}
";

var actual = Serializer.GetProto(new SchemaGenerationOptions
{
Syntax = ProtoSyntax.Proto3,
Package = "DefaultPackage",
Types = { typeof(SomeClass), typeof(SomeOtherClass), typeof(SomeEnum) },
});

Assert.Equal(expected, actual);
}

[ProtoContract]
[Obsolete]
public class SomeClass
{
[ProtoMember(tag: 1)]
public int SomeProperty { get; set; }
}

[ProtoContract]
public class SomeOtherClass
{
[ProtoMember(tag: 1)]
[Obsolete]
public string SomeOtherProperty { get; set; }
}

[ProtoContract]
[Obsolete]
public enum SomeEnum
{
One,
Two,
Three,
}
}
#pragma warning restore CS0612 // Type or member is obsolete
}
13 changes: 13 additions & 0 deletions src/protobuf-net/Meta/MetaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,7 @@ public bool IsGroup
if (!allValid) NewLine(builder, indent).Append("/* for context only");
NewLine(builder, indent).Append("enum ").Append(GetSchemaTypeName(callstack)).Append(" {");
AddNamespace(imports);
AppendMessageDeprecatedOption(builder, indent);

if (Type.IsDefined(typeof(FlagsAttribute), true))
{
Expand Down Expand Up @@ -2109,6 +2110,7 @@ public bool IsGroup

NewLine(builder, indent).Append("message ").Append(GetSchemaTypeName(callstack)).Append(" {");
AddNamespace(imports);
AppendMessageDeprecatedOption(builder, indent);
foreach (ValueMember member in fieldsArr)
{
string schemaTypeName;
Expand Down Expand Up @@ -2216,6 +2218,10 @@ void WriteValueMember(string schemaModelTypeName, bool hasGroupModifier = false)
imports.Add(RuntimeTypeModel.CommonImports.Protogen);
AddOption(builder, ref hasOption).Append("(.protobuf_net.fieldopt).dynamicType = true");
}
if (member.Member.GetCustomAttribute<ObsoleteAttribute>() != null)
{
AddOption(builder, ref hasOption).Append("deprecated = true");
}
CloseOption(builder, ref hasOption).Append(';');
if (syntax != ProtoSyntax.Proto2 && member.DefaultValue is not null && !member.IsRequired)
{
Expand Down Expand Up @@ -2345,6 +2351,13 @@ void AppendReservations()
builder.Append(" /* ").Append(reservation.Comment).Append(" */");
}
}
void AppendMessageDeprecatedOption(StringBuilder builder, int indent)
{
if (Type.GetCustomAttribute<ObsoleteAttribute>() != null)
{
NewLine(builder, indent + 1).Append("option deprecated = true;");
}
}
}

private static StringBuilder AddOption(StringBuilder builder, ref bool hasOption)
Expand Down

0 comments on commit bfee7a4

Please sign in to comment.