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

C# protoc plugin: deprecated option in proto definitions should add Obsolete attribute to generated c# code #28597

Closed
mrt181 opened this issue Jan 18, 2022 · 8 comments

Comments

@mrt181
Copy link

mrt181 commented Jan 18, 2022

Is your feature request related to a problem? Please describe.

Adding the Obsolete attribute to an rpc method that is overriden warns that the base method is non-obselete:
Obsolete member 'SomeService.SomeMethod(SomeRequest, ServerCallContext)' overrides non-obsolete member 'SomeService.SomeServiceBase.SomeMethod(SomeRequest, ServerCallContext)' [SomeService]csharp(CS0809)

The proto definitions look like this:

service SomeService {
  rpc SomeMethod (SomeRequest) returns (SomeReply) {
    option deprecated = true;
  }

This is ignored in the generated code.

      [global::System.CodeDom.Compiler.GeneratedCode("grpc_csharp_plugin", null)]
      public virtual global::System.Threading.Tasks.Task<global::GrpcSomeService.SomeReply> SomeMethod(global::GrpcSomeService.SomeRequest request, grpc::ServerCallContext context)

It also does not work on the enum level:

enum SomeEnum {
  option deprecated = true;
  A = 0 [deprecated = true];
}

No attribute is added:

#region Enums
  public enum SomeEnum {
    [pbr::OriginalName("A")] A = 0,
}

It works on the message level:

message SomeRequest {
  option deprecated = true;
  SomeEnum somefield = 1;
}

Which adds the Obsolete attribute:

  [global::System.ObsoleteAttribute]
  public sealed partial class SomeRequest : pb::IMessage<SomeRequest>

Describe the solution you'd like

Add the Obsolete attribute on the generated code when the deprecated option is set in the proto definition.

Describe alternatives you've considered

ignore warnings and use additional documentation and communication.

@drfloob drfloob assigned jtattermusch and unassigned drfloob Jan 18, 2022
@jtattermusch jtattermusch changed the title deprecated option in proto definitions should add Obselete attribute to generated c# code deprecated option in proto definitions should add Obsolete attribute to generated c# code Feb 14, 2022
@jtattermusch jtattermusch changed the title deprecated option in proto definitions should add Obsolete attribute to generated c# code C# protoc plugin: deprecated option in proto definitions should add Obsolete attribute to generated c# code Oct 14, 2022
@jtattermusch
Copy link
Contributor

Since the issue description mentions deprecated option for Enum values, see also protocolbuffers/protobuf#10513

@tonydnewell
Copy link
Contributor

For info: I've looked at the different places that the deprecated option can be used in proto files and compared the code generated by C# and Java. In all cases Java generates code with the correct @Deprecated attribute but the C# generator only currently supports a few cases:

@jskeet Can you think of any downside or gotchas if we add deprecated option support everywhere for C#? Anything we would break?

@jskeet
Copy link
Contributor

jskeet commented Feb 14, 2023

Not sure what the file level would look like - on the reflection class, maybe?

Other than that, as you say I think it's only a matter of adding it to service and method, then updating the version of protoc in the gRPC github repo.

@tonydnewell
Copy link
Contributor

tonydnewell commented Feb 15, 2023

FYI: the support for deprecating enums and enum values is not yet released - it is in Protocol Buffers v22.0-rc1. The gRPC repo currently has 21.12 as the protocol buffers submodule.

@jskeet
Copy link
Contributor

jskeet commented Feb 24, 2023

Note: Google.Protobuf 3.22.0 has now been released.

Additionally, I'd like to bring attention to the current state of affairs when a request/response pair are deprecated:

  • The message types are marked as obsolete in the "plain" protoc code
  • This causes warnings or errors on the gRPC generated code, on the private static marshaller/method fields, as well as the public methods that accept/return those types

Currently for Google Cloud libraries we have to work around that by inserting a suppression pragma at the start of the gRPC-generated code.

Marking the methods and fields (in the gRPC generated code) as obsolete removes this warning - but it does lead to an interesting contradiction: if the RPC is not marked as deprecated, but the request and/or response messages are, should the message be marked as obsolete? (I think it's easiest to mark it as obsolete if any of the request/response/rpc is deprecated.)

@tonydnewell
Copy link
Contributor

@jskeet Propagating the deprecated ([Obsolete]) attribute up to the service method if any of the messages are deprecated is possible. However this does push the warnings elsewhere in the generated code - to within the BindService methods.

Do we want to marked then bind methods as deprecated too? Or just disable obsolete warnings for those methods? Or we could just disable obsolete warnings for all the generated code (like you are doing by inserting the pragma yourself)?

Is there any advantage in not disabling obsolete warnings for all the generated code since the user will find out the deprecated methods and messages themselves in their own code?

@jskeet
Copy link
Contributor

jskeet commented Feb 28, 2023

Is there any advantage in not disabling obsolete warnings for all the generated code since the user will find out the deprecated methods and messages themselves in their own code?

Personally I think this is the simplest approach. You could even do it unconditionally, whether there's anything obsolete or not - with the downside that it would introduce a change to generated code which is unnecessary for most users.

apolcyn pushed a commit that referenced this issue Apr 26, 2023
… and methods in C# generated code (#32414)

Apply Obsolete attribute to deprecated services and methods in C#
generated code

Fix for #28597

- Deprecated support for enums and enum values is already fixed by
protocolbuffers/protobuf#10520 but this is not
yet released. It is fixed in Protocol Buffers v22.0-rc1 but the gRPC
repo currently has 21.12 as the protocol buffers submodule.

- Deprecated support for messages and fields already exists in the
protocol buffers compiler.

The fix in this PR adds `Obsolete` attribute to classes and methods for
deprecated services and methods within services. e.g.
```
service Greeter {
  option deprecated=true; // service level deprecated
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {
    option deprecated=true; // method level deprecated
  }
}
```

I couldn't find any protocol buffers plugin tests to update. Tested
locally.
@apolcyn
Copy link
Contributor

apolcyn commented Apr 27, 2023

fixed by #32414

@apolcyn apolcyn closed this as completed Apr 27, 2023
XuanWang-Amos pushed a commit to XuanWang-Amos/grpc that referenced this issue May 1, 2023
… and methods in C# generated code (grpc#32414)

Apply Obsolete attribute to deprecated services and methods in C#
generated code

Fix for grpc#28597

- Deprecated support for enums and enum values is already fixed by
protocolbuffers/protobuf#10520 but this is not
yet released. It is fixed in Protocol Buffers v22.0-rc1 but the gRPC
repo currently has 21.12 as the protocol buffers submodule.

- Deprecated support for messages and fields already exists in the
protocol buffers compiler.

The fix in this PR adds `Obsolete` attribute to classes and methods for
deprecated services and methods within services. e.g.
```
service Greeter {
  option deprecated=true; // service level deprecated
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {
    option deprecated=true; // method level deprecated
  }
}
```

I couldn't find any protocol buffers plugin tests to update. Tested
locally.
paulosjca pushed a commit to paulosjca/grpc that referenced this issue May 4, 2023
… and methods in C# generated code (grpc#32414)

Apply Obsolete attribute to deprecated services and methods in C#
generated code

Fix for grpc#28597

- Deprecated support for enums and enum values is already fixed by
protocolbuffers/protobuf#10520 but this is not
yet released. It is fixed in Protocol Buffers v22.0-rc1 but the gRPC
repo currently has 21.12 as the protocol buffers submodule.

- Deprecated support for messages and fields already exists in the
protocol buffers compiler.

The fix in this PR adds `Obsolete` attribute to classes and methods for
deprecated services and methods within services. e.g.
```
service Greeter {
  option deprecated=true; // service level deprecated
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {
    option deprecated=true; // method level deprecated
  }
}
```

I couldn't find any protocol buffers plugin tests to update. Tested
locally.
wanlin31 pushed a commit that referenced this issue May 18, 2023
… and methods in C# generated code (#32414)

Apply Obsolete attribute to deprecated services and methods in C#
generated code

Fix for #28597

- Deprecated support for enums and enum values is already fixed by
protocolbuffers/protobuf#10520 but this is not
yet released. It is fixed in Protocol Buffers v22.0-rc1 but the gRPC
repo currently has 21.12 as the protocol buffers submodule.

- Deprecated support for messages and fields already exists in the
protocol buffers compiler.

The fix in this PR adds `Obsolete` attribute to classes and methods for
deprecated services and methods within services. e.g.
```
service Greeter {
  option deprecated=true; // service level deprecated
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {
    option deprecated=true; // method level deprecated
  }
}
```

I couldn't find any protocol buffers plugin tests to update. Tested
locally.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants