Skip to content

fix: Remove unnecessary MapStruct conversion - #503

Closed
kabir wants to merge 1 commit into
a2aproject:mainfrom
kabir:updating_payloads_followup
Closed

kabir wants to merge 1 commit into
a2aproject:mainfrom
kabir:updating_payloads_followup

Conversation

@kabir

@kabir kabir commented Dec 3, 2025

Copy link
Copy Markdown
Collaborator

No description provided.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @kabir, 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 streamlines the data conversion process between gRPC protocol buffers and domain objects by optimizing the SubscribeToTaskRequestMapper. It transitions from verbose, manual conversion logic to a more concise, annotation-driven approach using MapStruct, which also integrates with the SubscribeToTaskRequest builder to handle default values for JSONRPC envelope fields. This change enhances code maintainability and leverages MapStruct's capabilities more fully.

Highlights

  • MapStruct Mapper Refactoring: The SubscribeToTaskRequestMapper has been refactored to replace manual default method implementations with annotation-driven MapStruct mappings for toProto and fromProto conversions.
  • Builder Integration: A static builder() method was added to the SubscribeToTaskRequest domain object to enable MapStruct to utilize its builder for object creation, particularly for setting default JSONRPC envelope fields.
  • Simplified Conversion Logic: The fromProto mapping now explicitly ignores JSONRPC envelope fields (id, jsonrpc, method), delegating their population to the SubscribeToTaskRequest builder, reducing redundant mapping logic.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 aims to simplify a MapStruct mapper by replacing manual default method implementations with annotations. While this is a good goal, the refactoring unfortunately introduces functional regressions by removing critical null-safety checks that were present in the original code. This could lead to NullPointerExceptions at runtime. My review includes two high-severity comments that recommend reverting the mapping methods to their original, safer default implementations to prevent these issues.

Comment on lines +33 to +34
@Mapping(target = "name", expression = "java(ResourceNameParser.defineTaskName(domain.getParams().id()))")
io.a2a.grpc.SubscribeToTaskRequest toProto(io.a2a.spec.SubscribeToTaskRequest domain);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

This refactoring introduces a regression. The original default method implementation correctly handled cases where domain.getParams() or domain.getParams().id() were null by returning null. This new implementation will throw a NullPointerException if domain.getParams() is null, or create an invalid resource name like "tasks/null" if id is null. To prevent this, it's safer to stick with the explicit checks in the default method.

    default io.a2a.grpc.SubscribeToTaskRequest toProto(io.a2a.spec.SubscribeToTaskRequest domain) {
        if (domain == null || domain.getParams() == null || domain.getParams().id() == null) {
            return null;
        }
        return io.a2a.grpc.SubscribeToTaskRequest.newBuilder()
                .setName(ResourceNameParser.defineTaskName(domain.getParams().id()))
                .build();
    }

Comment on lines 36 to +49
/**
* Converts proto SubscribeToTaskRequest to domain SubscribeToTaskRequest.
* Extracts the task ID from the name field and creates a TaskIdParams.
* The JSONRPC envelope fields (id, jsonrpc, method) are populated with defaults by the Builder.
*
* @param proto the proto SubscribeToTaskRequest
* @return the domain SubscribeToTaskRequest
*/
default io.a2a.spec.SubscribeToTaskRequest fromProto(io.a2a.grpc.SubscribeToTaskRequest proto) {
if (proto == null || proto.getName() == null) {
return null;
}
return new io.a2a.spec.SubscribeToTaskRequest.Builder()
.params(new io.a2a.spec.TaskIdParams(ResourceNameParser.extractTaskId(proto.getName())))
.build();
}
@BeanMapping(builder = @Builder(buildMethod = "build"))
@Mapping(target = "params", expression = "java(new io.a2a.spec.TaskIdParams(ResourceNameParser.extractTaskId(proto.getName())))")
@Mapping(target = "id", ignore = true) // Builder sets UUID default
@Mapping(target = "jsonrpc", ignore = true) // Builder sets "2.0" default
@Mapping(target = "method", ignore = true) // Builder sets "SubscribeToTask" default
io.a2a.spec.SubscribeToTaskRequest fromProto(io.a2a.grpc.SubscribeToTaskRequest proto);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Similar to toProto, this mapping introduces a regression. The original code returned null if proto.getName() was null, preventing errors. This new implementation will throw a NullPointerException. It's best to revert to the safer default method implementation, including the original Javadoc.

    /**
     * Converts proto SubscribeToTaskRequest to domain SubscribeToTaskRequest.
     * Extracts the task ID from the name field and creates a TaskIdParams.
     *
     * @param proto the proto SubscribeToTaskRequest
     * @return the domain SubscribeToTaskRequest
     */
    default io.a2a.spec.SubscribeToTaskRequest fromProto(io.a2a.grpc.SubscribeToTaskRequest proto) {
        if (proto == null || proto.getName() == null) {
            return null;
        }
        return new io.a2a.spec.SubscribeToTaskRequest.Builder()
                .params(new io.a2a.spec.TaskIdParams(ResourceNameParser.extractTaskId(proto.getName())))
                .build();
    }

@kabir

kabir commented Dec 3, 2025

Copy link
Copy Markdown
Collaborator Author

Closing since Gemini is right, and the workarounds are ugly

@kabir kabir closed this Dec 3, 2025
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.

1 participant