Guard gRPC beans on grpc-netty instead of grpc-api#4190
Open
seonwooj0810 wants to merge 1 commit into
Open
Conversation
JsonToGrpcGatewayFilterFactory and GrpcSslConfigurer require grpc-netty classes (io.grpc.netty.NettyChannelBuilder, io.grpc.netty.GrpcSslContexts), but their @ConditionalOnClass guard only checked io.grpc.Channel, which ships in grpc-api. When grpc-api is present on the classpath without grpc-netty, the beans were created and failed with NoClassDefFoundError: io/grpc/netty/NettyChannelBuilder. Switch the @ConditionalOnClass guards (and the matching AOT reflection hint registration) to io.grpc.netty.NettyChannelBuilder so the beans only activate when grpc-netty is actually available. Fixes spring-cloudgh-4169 Signed-off-by: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes gh-4169
Problem
JsonToGrpcGatewayFilterFactoryandGrpcSslConfigurerinGatewayAutoConfigurationare guarded with@ConditionalOnClass(name = "io.grpc.Channel"). Howeverio.grpc.Channelships in grpc-api, while both beans actually require grpc-netty classes:GrpcSslConfigurerextendsAbstractSslConfigurer<NettyChannelBuilder, ManagedChannel>and usesio.grpc.netty.GrpcSslContexts.JsonToGrpcGatewayFilterFactoryusesio.grpc.netty.NettyChannelBuilder.grpc-netty,grpc-protobufandgrpc-stubare declared<optional>in the server module. When an application hasgrpc-apion the classpath (e.g. pulled in transitively by an unrelated dependency) but notgrpc-netty, these beans are still created and fail with:The only workaround today is to add the full
grpc-nettydependency even when the gateway's gRPC support is never used (see also gh-2769).Change
Switch the
@ConditionalOnClassguards on both beans (and the matching AOT reflection-hint registration inConfigurableHintsRegistrationProcessor) fromio.grpc.Channeltoio.grpc.netty.NettyChannelBuilder— a class that is actually used by both beans and the exact class reported missing. The beans now activate only when grpc-netty is present.Scope is limited to the webflux server module; the webmvc variant has no equivalent gRPC auto-configuration.
Test evidence
Added
GatewayAutoConfigurationTests#grpcBeansNotConfiguredWhenGrpcNettyAbsent, which hidesio.grpc.netty.NettyChannelBuilderviaFilteredClassLoader(simulating grpc-api present, grpc-netty absent) and asserts the context starts and neither gRPC bean is created.io.grpc.Channelmakes the test fail, withjsonToGRPCFilterFactorywrongly present — confirming it is a genuine regression test.(
grpcBeansNotConfiguredWhenGrpcNettyAbsent,gRPCFiltersConfiguredWhenHTTP2Enabled,gRPCFiltersNotConfiguredWhenHTTP2Disabled)mvn validate(spring-javaformat + checkstyle) passes on the module. Commit is DCO signed-off.