Overview
Concrete MapStruct-based mapper classes converting between A2A spec Java records and protobuf generated messages, including complex polymorphic dispatch for sealed interfaces and oneof fields.
Description
The `spec-grpc/mapper` package provides bidirectional conversion between the spec module's Java records (domain model) and the spec-grpc module's protobuf generated classes (wire format). Most mappers are trivial declarative MapStruct interfaces, but several contain complex manual logic:
- A2AProtoMapperConfig: Shared `@MapperConfig` defining enum/time/binary/JSON conversion strategies with strict unmapped-target policy (build fails on missing mappings)
- A2ACommonFieldMapper: Reusable conversions: empty-string-to-null, timestamp (Instant/OffsetDateTime ↔ protobuf Timestamp), metadata (Map ↔ protobuf Struct), empty-list-to-null, enum UNSPECIFIED handling
- PartMapper: Polymorphic Part conversion using instanceof dispatch — handles TextPart, FilePart (FileWithBytes/FileWithUri), and DataPart → protobuf oneof
- SecuritySchemeMapper: Sealed interface dispatch for 5 SecurityScheme subtypes → protobuf oneof
- SecurityRequirementMapper: Complex nested security requirement mapping
- StreamResponseMapper: Complex oneof dispatch for streaming response events
- ResourceNameParser: Utility for parsing gRPC resource name patterns (e.g., `tasks/{taskId}/pushNotificationConfigs/{configId}`)
Usage
- gRPC transport conversion: Used internally by ProtoUtils to convert spec records to/from protobuf for GrpcTransport
- Polymorphic dispatch: Manual instanceof handling for sealed interfaces and protobuf oneof where MapStruct's `@SubclassMapping` is insufficient
- Resource name parsing: ResourceNameParser extracts IDs from gRPC resource name patterns used by push notification config operations
Code Reference
Source Location
- Repository: a2a-java
- Directory: `spec-grpc/src/main/java/org/a2aproject/sdk/grpc/mapper/`
Key Complex Mappers
| File |
Lines |
Role
|
| A2AProtoMapperConfig.java |
195 |
Shared config: enum/time/binary/JSON conversion
|
| A2ACommonFieldMapper.java |
463 |
Common field conversion utilities
|
| PartMapper.java |
116 |
Polymorphic Part oneof dispatch
|
| SecuritySchemeMapper.java |
104 |
Sealed interface security dispatch
|
| SecurityRequirementMapper.java |
112 |
Nested security requirement mapping
|
| StreamResponseMapper.java |
87 |
Streaming response oneof dispatch
|
| ResourceNameParser.java |
106 |
gRPC resource name parsing
|
Signature
// A2AProtoMapperConfig - shared configuration
@MapperConfig(unmappedTargetPolicy = ReportingPolicy.ERROR,
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface A2AProtoMapperConfig {
default Instant map(Timestamp value);
default Timestamp map(Instant value);
default Duration map(com.google.protobuf.Duration value);
default ByteBuffer map(ByteString value);
default ByteString map(ByteBuffer value);
default Map<String, Object> map(Struct value);
default Struct map(Map<String, Object> value);
}
// PartMapper - polymorphic oneof dispatch
public interface PartMapper {
PartMapper INSTANCE = A2AMappers.getMapper(PartMapper.class);
default org.a2aproject.sdk.grpc.Part toProto(Part<?> domain);
default Part<?> fromProto(org.a2aproject.sdk.grpc.Part proto);
}
// SecuritySchemeMapper - sealed interface dispatch
public interface SecuritySchemeMapper {
SecuritySchemeMapper INSTANCE = A2AMappers.getMapper(SecuritySchemeMapper.class);
default org.a2aproject.sdk.grpc.SecurityScheme toProto(
org.a2aproject.sdk.spec.SecurityScheme domain);
}
// ResourceNameParser
public class ResourceNameParser {
public static String extractTaskId(String resourceName);
public static String defineTaskName(String taskId);
public static String[] parseTaskPushNotificationConfigName(String resourceName);
public static String[] parseGetTaskPushNotificationConfigName(String resourceName);
}
Import
import org.a2aproject.sdk.grpc.mapper.A2AProtoMapperConfig;
import org.a2aproject.sdk.grpc.mapper.A2ACommonFieldMapper;
import org.a2aproject.sdk.grpc.mapper.PartMapper;
import org.a2aproject.sdk.grpc.mapper.SecuritySchemeMapper;
import org.a2aproject.sdk.grpc.mapper.ResourceNameParser;
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| domain |
Spec records |
Yes |
Java records (Part, SecurityScheme, etc.)
|
| proto |
Protobuf messages |
Yes |
Generated protobuf classes
|
| resourceName |
String |
Yes (parser) |
gRPC resource name pattern
|
Outputs
| Name |
Type |
Description
|
| toProto() |
Protobuf message |
Wire format for gRPC
|
| fromProto() |
Spec record |
Domain model
|
| parseTaskPushNotificationConfigName() |
String[] |
[taskId, configId] extracted
|
Usage Examples
Polymorphic Part Mapping
import org.a2aproject.sdk.grpc.mapper.PartMapper;
import org.a2aproject.sdk.spec.TextPart;
// Convert spec TextPart to protobuf
TextPart textPart = new TextPart("Hello, agent!");
org.a2aproject.sdk.grpc.Part protoPart = PartMapper.INSTANCE.toProto(textPart);
// Convert back from protobuf
Part<?> domainPart = PartMapper.INSTANCE.fromProto(protoPart);
Resource Name Parsing
import org.a2aproject.sdk.grpc.mapper.ResourceNameParser;
// Parse "tasks/task-123/pushNotificationConfigs/config-456"
String[] ids = ResourceNameParser.parseTaskPushNotificationConfigName(
"tasks/task-123/pushNotificationConfigs/config-456");
String taskId = ids[0]; // "task-123"
String configId = ids[1]; // "config-456"
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.