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

add gRPC exception advice support for factory beans #266

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

bitbrain
Copy link

@bitbrain bitbrain commented Nov 24, 2021

This pull request addresses #265

When currently using @GRpcServiceAdvice on a bean that is being created within a @Configuration like so (Spring Boot 2.5.6):

 @Bean
 public GrpcExceptionAdvice grpcExceptionAdvice() {
     return new GrpcExceptionAdvice();
 }

it will produce the following exception on startup:

Caused by: java.lang.NullPointerException
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:315)
	at org.lognet.springboot.grpc.autoconfigure.OnMissingErrorHandlerCondition.getMatchOutcome(OnMissingErrorHandlerCondition.java:35)
	at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47)
	... 95 more

The reason is that the current logic does not support beans created through factory methods:

The solution

@codecov
Copy link

codecov bot commented Nov 24, 2021

Codecov Report

Merging #266 (860950d) into master (d70346b) will decrease coverage by 0.14%.
The diff coverage is 71.42%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #266      +/-   ##
============================================
- Coverage     88.24%   88.09%   -0.15%     
- Complexity      317      318       +1     
============================================
  Files            50       50              
  Lines          1531     1537       +6     
  Branches         86       87       +1     
============================================
+ Hits           1351     1354       +3     
- Misses          143      144       +1     
- Partials         37       39       +2     
Impacted Files Coverage Δ
.../autoconfigure/OnMissingErrorHandlerCondition.java 77.77% <71.42%> (-7.94%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d70346b...860950d. Read the comment docs.

@bitbrain
Copy link
Author

I will add tests for this.

@jvmlet
Copy link
Collaborator

jvmlet commented Nov 25, 2021

what is the spring boot version you are running with?

@bitbrain
Copy link
Author

what is the spring boot version you are running with?

@jvmlet 2.5.6

Miguel Gonzalez Sanchez added 2 commits November 29, 2021 09:28
@bitbrain
Copy link
Author

@jvmlet any recommendations on how I can get the code coverage to pass? I checked https://app.codecov.io/gh/LogNet/grpc-spring-boot-starter/compare/266/changes and it seems to have no changes and I also added tests to ensure the bean loading works correctly but it complains about +0.15% coverage decrease.

@@ -40,11 +40,23 @@ sourceSets.configureEach{s->

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:${grpcSpringBoot.protocVersion.get()}"

// for apple m1, please add protoc_platform=osx-x86_64 in $HOME/.gradle/gradle.properties
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Apple M1 this project currently does not build due to missing artifacts. This is a recommended workaround. I can remove this again if you want.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the platform can't be inferred dynamically, please add it as configuration option here+ readme

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a workaround and should not be needed as a property of this Spring boot starter. In case we still want to have it I'd suggest to make it as part of another pull request.

In the meantime, I will revert these changes to the grpc-spring-boot.gradle file.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is not starter's feature, but gradle plugin's one. You can have this logic in extension class and grpc-spring-boot.gradle in separate PR.

@bitbrain
Copy link
Author

bitbrain commented Dec 2, 2021

I did some digging why that newly added code is not called during the integration tests. Here is my summary. Feedback welcome on how we could change these tests to reproduce this behaviour, as I am currently unable to determine what specifically is causing this behaviour to occur.

Scenario 1: AnnotatedGenericBeanDefinition

This is the current logic within the unit tests of this Spring Boot starter. Setting up the bean like this:

...will internally create an AnnotatedGenericBeanDefinition and set it on the internal beanFactoryMap within the beanFactory. As a result, beanDefinition.getBeanClassName() is not null.

Scenario 2: ConfigurationClassBeanDefinitionReader$ConfigurationClassBeanDefinition

Within our codebase, we use a custom Spring Boot starter to define a custom advice like so:

@Bean
public GrpcExceptionAdvice grpcExceptionAdvice() {
        return new GrpcExceptionAdvice();
 }

which itself looks like this:

@GRpcServiceAdvice
public class GrpcExceptionAdvice {
 /* exception handlers go here*/
}

This is literally the same setup, however, for some reason it is using a different bean definition where beanDefinition.getBeanClassName() returns null and instead the class name can be obtained from the internal MethodMetadata on the AnnotatedBeanDefinition.

Question time!

How can I change the tests within this Spring Boot starter to replicate Scenario 2?

@jvmlet
Copy link
Collaborator

jvmlet commented Dec 6, 2021

See behavior difference if you define the inner advice class as static/non-static

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants