-
Notifications
You must be signed in to change notification settings - Fork 120
chore: add Jspecify NullCheck in grpc transport (Fixes #334) #342
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
Conversation
Summary of ChangesHello @devcom33, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the null-safety of the gRPC transport client by introducing Jspecify nullness annotations. The primary goal is to make the codebase more robust and maintainable by allowing static analysis tools to catch potential null-related issues at compile time, thereby reducing runtime Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request effectively adds JSpecify nullness annotations across the gRPC transport module, improving code safety and clarity. The use of @NullMarked at the package level is a great step towards ensuring null-safety. My main feedback is a suggestion to improve the builder pattern in GrpcTransportConfigBuilder to fail-fast instead of fail-late, which will make it more robust. Overall, this is a solid improvement.
| public class GrpcTransportConfigBuilder extends ClientTransportConfigBuilder<GrpcTransportConfig, GrpcTransportConfigBuilder> { | ||
|
|
||
| private Function<String, Channel> channelFactory; | ||
| private Function<String, Channel> channelFactory = s -> { throw new IllegalStateException("channelFactory not set"); }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this initialization fixes the NullAway warning for a non-nullable field, it creates a 'fail-late' scenario where an exception is only thrown when the channelFactory is eventually used. A better practice for builders is to 'fail-fast' by validating the state in the build() method.
I recommend marking channelFactory as @Nullable and then adding a check in the build() method to ensure it has been set before creating the GrpcTransportConfig object. This prevents the creation of a partially initialized, unusable configuration object.
You'll also need to add import org.jspecify.annotations.Nullable;.
For example, you could change the build() method to something like this:
@Override
public GrpcTransportConfig build() {
if (channelFactory == null) {
throw new IllegalStateException("channelFactory must be set");
}
GrpcTransportConfig config = new GrpcTransportConfig(channelFactory);
config.setInterceptors(interceptors);
return config;
}| private Function<String, Channel> channelFactory = s -> { throw new IllegalStateException("channelFactory not set"); }; | |
| private @Nullable Function<String, Channel> channelFactory; |
|
@devcom33 may you fix gemini comments please ? |
|
Hey @ehsavoie , I’ve updated the builder to use the fail-fast pattern as suggested. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request effectively introduces JSpecify nullness annotations to the gRPC transport module, which is a great step towards improving code safety and clarity. The changes are mostly correct and follow the PR's intent. I've pointed out a couple of minor issues: one regarding Javadoc completeness for a method with newly nullable parameters, and another about using a consistent @Nullable annotation across all modified files. Addressing these will make the changes even better.
client/transport/grpc/src/main/java/io/a2a/client/transport/grpc/GrpcTransport.java
Show resolved
Hide resolved
| import io.a2a.client.transport.spi.ClientTransportConfigBuilder; | ||
| import io.a2a.util.Assert; | ||
| import io.grpc.Channel; | ||
| import jakarta.annotation.Nullable; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
devcom33
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/gemini review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request effectively introduces JSpecify nullness annotations to the gRPC transport module, enhancing code safety and clarity. The changes are consistent across the modified files, with @Nullable annotations correctly applied to parameters and fields that can be null. The added validation in GrpcTransportConfigBuilder is a good defensive measure. I have one suggestion in GrpcTransport.java to remove an unused parameter, which will further improve code quality.
client/transport/grpc/src/main/java/io/a2a/client/transport/grpc/GrpcTransport.java
Show resolved
Hide resolved
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request does a great job of introducing JSpecify nullness annotations to the gRPC transport module. The use of @NullMarked at the package level and @Nullable on parameters and fields significantly improves the code's null-safety and makes contracts clearer. The refactoring to remove unused parameters and the added validation in GrpcTransportConfigBuilder are also excellent improvements. I have one minor suggestion to enhance the robustness of the GrpcTransport constructor.
| } | ||
|
|
||
| public GrpcTransport(Channel channel, AgentCard agentCard, List<ClientCallInterceptor> interceptors) { | ||
| public GrpcTransport(Channel channel, AgentCard agentCard, @Nullable List<ClientCallInterceptor> interceptors) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For improved robustness and to align with the non-null contract of the agentCard field (as per @NullMarked on the package), it's good practice to validate the agentCard parameter for nullity in this public constructor. This ensures that the GrpcTransport is always instantiated in a valid state.
Consider adding checkNotNullParam("agentCard", agentCard); at the start of the constructor body.
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request effectively introduces JSpecify nullness annotations to the gRPC transport module, significantly improving code safety and clarity. The addition of @NullMarked at the package level establishes a solid foundation for null-safety. Key changes include correctly annotating nullable parameters, especially ClientCallContext, across the GrpcTransport class. The introduction of runtime null checks in GrpcTransport's constructor for agentCard and in GrpcTransportConfigBuilder for channelFactory are excellent additions that prevent potential NullPointerExceptions and fix issues likely flagged by NullAway. The refactoring to remove an unused parameter from createGrpcSendMessageRequest is a nice cleanup. Overall, this is a high-quality contribution that enhances the robustness of the codebase.
| checkNotNullParam("request", request); | ||
|
|
||
| SendMessageRequest sendMessageRequest = createGrpcSendMessageRequest(request, context); | ||
| SendMessageRequest sendMessageRequest = createGrpcSendMessageRequest(request); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
context should be added back here.
| checkNotNullParam("request", request); | ||
| checkNotNullParam("eventConsumer", eventConsumer); | ||
| SendMessageRequest grpcRequest = createGrpcSendMessageRequest(request, context); | ||
| SendMessageRequest grpcRequest = createGrpcSendMessageRequest(request); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, context should be added back.
| } | ||
|
|
||
| private SendMessageRequest createGrpcSendMessageRequest(MessageSendParams messageSendParams, ClientCallContext context) { | ||
| private SendMessageRequest createGrpcSendMessageRequest(MessageSendParams messageSendParams) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't remove context here.
...nt/transport/grpc/src/main/java/io/a2a/client/transport/grpc/GrpcTransportConfigBuilder.java
Show resolved
Hide resolved
...nt/transport/grpc/src/main/java/io/a2a/client/transport/grpc/GrpcTransportConfigBuilder.java
Outdated
Show resolved
Hide resolved
|
Thanks @devcom33! |
|
thank you for merging the PR. |
|
@devcom33 Sure thing, I've added the label. |
a2aproject#342) This PR adds Jspecify nullness annotations to the gRPC transport module: - Added @NullMarked in package-info.java for the package. - Annotated nullable parameters with @nullable in GrpcTransport.java and related classes. - Ensured all fields and method signatures match nullability expectations. - Fixed NullAway errors by initializing non-null fields and updating constructor annotations. - All tests pass successfully. --------- Co-authored-by: devcom33 <devcom33@users.noreply.github.com>
This PR adds Jspecify nullness annotations to the gRPC transport module: