Skip to content

Commit 47dc31b

Browse files
committed
fix: omit optional fields with default values from client JSON serialization
The JSON-RPC client serialization used alwaysPrintFieldsWithNoPresence() which caused proto3 implicit-presence fields (taskId, contextId, tenant, etc.) to be serialized as empty strings instead of being omitted. Removed alwaysPrintFieldsWithNoPresence() from JsonFormat.printer() in JSONRPCUtils.toJsonRPCRequest() (client requests only), aligning with the REST client transport which already omitted it. This fixes #770
1 parent d1578d3 commit 47dc31b

2 files changed

Lines changed: 16 additions & 50 deletions

File tree

client/transport/jsonrpc/src/test/java/io/a2a/client/transport/jsonrpc/JsonMessages.java

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ public class JsonMessages {
9393
{
9494
"text":"tell me a joke"
9595
}
96-
],
97-
"metadata":{
98-
99-
}
96+
]
10097
}
10198
}
10299
}""";
@@ -143,18 +140,12 @@ public class JsonMessages {
143140
{
144141
"text":"tell me a joke"
145142
}
146-
],
147-
"metadata":{
148-
}
143+
]
149144
},
150145
"configuration":{
151146
"acceptedOutputModes":[
152147
"text"
153-
],
154-
"returnImmediately":false
155-
},
156-
"metadata":{
157-
148+
]
158149
}
159150
}
160151
}""";
@@ -192,19 +183,12 @@ public class JsonMessages {
192183
{
193184
"text":"tell me a joke"
194185
}
195-
],
196-
"metadata":{
197-
198-
}
186+
]
199187
},
200188
"configuration":{
201189
"acceptedOutputModes":[
202190
"text"
203-
],
204-
"returnImmediately":false
205-
},
206-
"metadata":{
207-
191+
]
208192
}
209193
}
210194
}""";
@@ -376,19 +360,12 @@ public class JsonMessages {
376360
"url":"file:///path/to/image.jpg",
377361
"mediaType":"image/jpeg"
378362
}
379-
],
380-
"metadata":{
381-
382-
}
363+
]
383364
},
384365
"configuration":{
385366
"acceptedOutputModes":[
386367
"text"
387-
],
388-
"returnImmediately":false
389-
},
390-
"metadata":{
391-
368+
]
392369
}
393370
}
394371
}""";
@@ -443,19 +420,12 @@ public class JsonMessages {
443420
"timestamp":"2024-01-15T10:30:00Z"
444421
}
445422
}
446-
],
447-
"metadata":{
448-
449-
}
423+
]
450424
},
451425
"configuration":{
452426
"acceptedOutputModes":[
453427
"text"
454-
],
455-
"returnImmediately":false
456-
},
457-
"metadata":{
458-
428+
]
459429
}
460430
}
461431
}""";
@@ -514,19 +484,12 @@ public class JsonMessages {
514484
"labels":["Q1", "Q2", "Q3", "Q4"]
515485
}
516486
}
517-
],
518-
"metadata":{
519-
520-
}
487+
]
521488
},
522489
"configuration":{
523490
"acceptedOutputModes":[
524491
"text"
525-
],
526-
"returnImmediately":false
527-
},
528-
"metadata":{
529-
492+
]
530493
}
531494
}
532495
}""";

spec-grpc/src/main/java/io/a2a/grpc/utils/JSONRPCUtils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ public class JSONRPCUtils {
171171
private static final Pattern EXTRACT_WRONG_TYPE = Pattern.compile("Expected (.*) but found \".*\"");
172172
static final String ERROR_MESSAGE = "Invalid request content: %s. Please verify the request matches the expected schema for this method.";
173173

174+
private static final JsonFormat.Printer REQUEST_PRINTER = JsonFormat.printer().omittingInsignificantWhitespace();
175+
private static final JsonFormat.Printer RESPONSE_PRINTER = JsonFormat.printer().alwaysPrintFieldsWithNoPresence().omittingInsignificantWhitespace();
176+
174177
public static A2ARequest<?> parseRequestBody(String body, @Nullable String tenant) throws JsonMappingException, JsonProcessingException {
175178
JsonElement jelement = JsonParser.parseString(body);
176179
JsonObject jsonRpc = jelement.getAsJsonObject();
@@ -556,7 +559,7 @@ public static String toJsonRPCRequest(@Nullable String requestId, String method,
556559
output.name("method").value(method);
557560
}
558561
if (payload != null) {
559-
String resultValue = JsonFormat.printer().alwaysPrintFieldsWithNoPresence().omittingInsignificantWhitespace().print(payload);
562+
String resultValue = REQUEST_PRINTER.print(payload);
560563
output.name("params").jsonValue(resultValue);
561564
}
562565
output.endObject();
@@ -579,7 +582,7 @@ public static String toJsonRPCResultResponse(Object requestId, com.google.protob
579582
output.name("id").value(number.longValue());
580583
}
581584
}
582-
String resultValue = JsonFormat.printer().alwaysPrintFieldsWithNoPresence().omittingInsignificantWhitespace().print(builder);
585+
String resultValue = RESPONSE_PRINTER.print(builder);
583586
output.name("result").jsonValue(resultValue);
584587
output.endObject();
585588
return result.toString();

0 commit comments

Comments
 (0)