Repository navigation
fix: Remove unnecessary MapStruct conversion #503
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| package io.a2a.grpc.mapper; | ||
|
|
||
| import org.mapstruct.BeanMapping; | ||
| import org.mapstruct.Builder; | ||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
|
|
||
| /** | ||
| * Mapper between {@link io.a2a.spec.SubscribeToTaskRequest} and {@link io.a2a.grpc.SubscribeToTaskRequest}. | ||
|
|
@@ -11,9 +14,9 @@ | |
| * <li>Proto: Simple request with name field in format "tasks/{task_id}"</li> | ||
| * </ul> | ||
| * <p> | ||
| * Note: The domain object is a complete JSONRPC request, while the proto is just the gRPC | ||
| * request parameters. The JSONRPC envelope (id, jsonrpc, method) is handled separately | ||
| * by the transport layer. | ||
| * Uses {@link ResourceNameParser} to convert between task ID and resource name format. | ||
| * The domain JSONRPC envelope fields (id, jsonrpc, method) are populated with defaults | ||
| * by the Builder when converting from proto. | ||
| */ | ||
| @Mapper(config = A2AProtoMapperConfig.class, uses = {TaskIdParamsMapper.class}) | ||
| public interface SubscribeToTaskRequestMapper { | ||
|
|
@@ -27,28 +30,21 @@ public interface SubscribeToTaskRequestMapper { | |
| * @param domain the domain SubscribeToTaskRequest | ||
| * @return the proto SubscribeToTaskRequest | ||
| */ | ||
| 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(); | ||
| } | ||
| @Mapping(target = "name", expression = "java(ResourceNameParser.defineTaskName(domain.getParams().id()))") | ||
| io.a2a.grpc.SubscribeToTaskRequest toProto(io.a2a.spec.SubscribeToTaskRequest domain); | ||
|
|
||
| /** | ||
| * 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); | ||
|
Comment on lines
36
to
+49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to /**
* 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();
} |
||
| } | ||
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.
This refactoring introduces a regression. The original
defaultmethod implementation correctly handled cases wheredomain.getParams()ordomain.getParams().id()were null by returningnull. This new implementation will throw aNullPointerExceptionifdomain.getParams()is null, or create an invalid resource name like"tasks/null"ifidis null. To prevent this, it's safer to stick with the explicit checks in the default method.