Skip to content

Commit

Permalink
Add option to the protoc code generator to avoid generating deprecate…
Browse files Browse the repository at this point in the history
…d code (#3089)

### Motivation

The generated ServiceTalk gRPC stubs make use of deprecated calls. This causes a problem when `-Werror` is used to build the code since it will automatically fail the build. We should allow users who have already migrated their code to prevent the protoc compiler from generating and using deprecated references.

### Modifications

Add an option, `skipDeprecated`, as part of the protoc compiler configuration, to tell the generator to leave out deprecated references.

Remove
- initSerializationProvider, reason: ContentCodec is deprecated
- isSupportedMessageCodingsEmpty, reason ContentCodec is deprecated
- Deprecated ServiceFactory constructors
- ServiceFactory::Builder references to ContentCodec
- Generated client metadata and associated methods, reason: deprecated

### Result

Users can generate gRPC stubs without deprecated code.

### Testing

Manually tested:
- Add new option to ServiceTalk/examples/grpc/protoc-options and use it to generate test_service.proto which covers all combinations of streaming (or not) services.
  • Loading branch information
mgodave authored Nov 19, 2024
1 parent 38cc84b commit f8abe11
Show file tree
Hide file tree
Showing 5 changed files with 737 additions and 484 deletions.
2 changes: 2 additions & 0 deletions servicetalk-examples/grpc/protoc-options/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ protobuf {
outputSubDir = "java"
// Option to append a suffix to type names to avoid naming collisions with other code generation.
option 'typeNameSuffix=St'
// Option to tell the compiler to exclude all Deprecated fields, types, and methods from the output
option 'skipDeprecated=true'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ final Single<GrpcServerContext> bind(final ServerBinder binder, final GrpcExecut
* In case you have a use-case, let us know.
*/
@Deprecated
protected abstract void registerRoutes(Service service); // FIXME: 0.43 - remove deprecated method
protected void registerRoutes(Service service) { // FIXME: 0.43 - remove deprecated method
throw new UnsupportedOperationException("This method is not used starting from version 0.42.0");
}

/**
* Create a new {@link Service} from the passed {@link AllGrpcRoutes}.
Expand All @@ -150,7 +152,9 @@ final Single<GrpcServerContext> bind(final ServerBinder binder, final GrpcExecut
* In case you have a use-case, let us know.
*/
@Deprecated
protected abstract Service newServiceFromRoutes(AllGrpcRoutes routes); // FIXME: 0.43 - remove deprecated method
protected Service newServiceFromRoutes(AllGrpcRoutes routes) { // FIXME: 0.43 - remove deprecated method
throw new UnsupportedOperationException("This method is not used starting from version 0.42.0");
}

static GrpcRoutes<?> merge(GrpcRoutes<?>... allRoutes) {
final GrpcRouter.Builder[] builders = new GrpcRouter.Builder[allRoutes.length];
Expand Down
37 changes: 35 additions & 2 deletions servicetalk-grpc-protoc/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ link:{source-root}/servicetalk-examples/grpc/helloworld[gRPC HelloWorld Example]
This plugin supports the following
link:https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.compiler.command_line_interface[options]:

==== `javaDocs=<true|false>`
==== `javaDocs=<true|false>` (default: true)
Control if `javadoc` is generated by this plugin. Here is an example of how you can specify this option with gradle:

[source,gradle]
Expand All @@ -47,7 +47,7 @@ And with Maven:
</protocPlugin>
----

==== `typeNameSuffix=<value>`
==== `typeNameSuffix=<value>` (default: not enabled)
Appends the <value> onto service type names generated by this plugin. This helps to avoid service type name
collisions if other protoc plugins are generating code in the same gradle project, or in the same package name.

Expand Down Expand Up @@ -78,3 +78,36 @@ And with Maven:
</args>
</protocPlugin>
----

==== `skipDeprecated=<true|false>` (default: false)
Instructs the generator to remove all deprecated methods and fields from the generated code, as well as remove calls
to deprecated code either to internal ServiceTalk libraries, or outside. This will almost certainly cause breaking changes
to code that uses these deprecated methods but will be forward compatible.

If you are using the
link:https://github.com/google/protobuf-gradle-plugin#configure-what-to-generate[protobuf-gradle-plugin] this is how you
can specify an option:

[source,gradle]
----
task.plugins {
servicetalk_grpc {
option 'skipDeprecated=true'
}
}
----

And with Maven:

And with Maven:

[source, xml]
----
<protocPlugin>
<id>servicetalk-grpc-protoc</id>
<!-- more params -->
<args>
<arg>skipDeprecated=true</arg>
</args>
</protocPlugin>
----
Loading

0 comments on commit f8abe11

Please sign in to comment.