Repository navigation
fix(compat-0.3): preserve explicit JSON nulls when converting Struct to Java objects #1215
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
Open
ZYZ666-RGB
wants to merge
1
commit into
a2aproject:main
Choose a base branch
from
ZYZ666-RGB:fix/compat03-explicit-json-nulls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...src/test/java/org/a2aproject/sdk/compat03/grpc/utils/FromProtoExplicitNull_v0_3_Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package org.a2aproject.sdk.compat03.grpc.utils; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import com.google.protobuf.Struct; | ||
| import com.google.protobuf.util.JsonFormat; | ||
|
|
||
| import org.a2aproject.sdk.compat03.spec.DataPart_v0_3; | ||
| import org.a2aproject.sdk.compat03.spec.Part_v0_3; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class FromProtoExplicitNull_v0_3_Test { | ||
|
|
||
| private static Struct struct(String json) throws Exception { | ||
| Struct.Builder builder = Struct.newBuilder(); | ||
| JsonFormat.parser().merge(json, builder); | ||
| return builder.build(); | ||
| } | ||
|
|
||
| @Test | ||
| void partDataPartWithExplicitNullField() throws Exception { | ||
| var dataPart = org.a2aproject.sdk.compat03.grpc.DataPart.newBuilder() | ||
| .setData(struct("{\"some_optional_field\":null,\"other_field\":\"value\"}")) | ||
| .build(); | ||
| var part = org.a2aproject.sdk.compat03.grpc.Part.newBuilder().setData(dataPart).build(); | ||
|
|
||
| Part_v0_3<?> result = ProtoUtils_v0_3.FromProto.part(part); | ||
|
|
||
| assertTrue(result instanceof DataPart_v0_3); | ||
| Map<String, Object> data = ((DataPart_v0_3) result).data(); | ||
| assertNotNull(data); | ||
| assertEquals(2, data.size()); | ||
| assertTrue(data.containsKey("some_optional_field")); | ||
| assertNull(data.get("some_optional_field")); | ||
| assertEquals("value", data.get("other_field")); | ||
| } | ||
|
|
||
| @Test | ||
| void partMetadataWithExplicitNullField() throws Exception { | ||
| var textPart = org.a2aproject.sdk.compat03.grpc.Part.newBuilder() | ||
| .setText("hello") | ||
| .setMetadata(struct("{\"nullable\":null,\"source\":\"ID\"}")) | ||
| .build(); | ||
|
|
||
| Part_v0_3<?> result = ProtoUtils_v0_3.FromProto.part(textPart); | ||
|
|
||
| Map<String, Object> metadata = ((org.a2aproject.sdk.compat03.spec.TextPart_v0_3) result).metadata(); | ||
| assertNotNull(metadata); | ||
| assertEquals(2, metadata.size()); | ||
| assertTrue(metadata.containsKey("nullable")); | ||
| assertNull(metadata.get("nullable")); | ||
| assertEquals("ID", metadata.get("source")); | ||
| } | ||
|
|
||
| @Test | ||
| void partDataPartWithNestedNullAndNullInList() throws Exception { | ||
| var dataPart = org.a2aproject.sdk.compat03.grpc.DataPart.newBuilder() | ||
| .setData(struct("{\"nested\":{\"optional\":null},\"items\":[null,1]}")) | ||
| .build(); | ||
| var part = org.a2aproject.sdk.compat03.grpc.Part.newBuilder().setData(dataPart).build(); | ||
|
|
||
| Part_v0_3<?> result = ProtoUtils_v0_3.FromProto.part(part); | ||
|
|
||
| Map<String, Object> data = ((DataPart_v0_3) result).data(); | ||
| assertNotNull(data.get("nested")); | ||
| assertNull(((Map<?, ?>) data.get("nested")).get("optional")); | ||
| } | ||
|
|
||
| @Test | ||
| void taskWithDataPartThatHasExplicitNullField() throws Exception { | ||
| var message = org.a2aproject.sdk.compat03.grpc.Message.newBuilder() | ||
| .setRole(org.a2aproject.sdk.compat03.grpc.Role.ROLE_AGENT) | ||
| .setMessageId("message-1") | ||
| .addContent(org.a2aproject.sdk.compat03.grpc.Part.newBuilder() | ||
| .setData(org.a2aproject.sdk.compat03.grpc.DataPart.newBuilder() | ||
| .setData(struct("{\"some_optional_field\":null,\"other_field\":\"value\"}")))) | ||
| .build(); | ||
| var task = org.a2aproject.sdk.compat03.grpc.Task.newBuilder() | ||
| .setId("task-1") | ||
| .setContextId("context-1") | ||
| .setStatus(org.a2aproject.sdk.compat03.grpc.TaskStatus.newBuilder() | ||
| .setState(org.a2aproject.sdk.compat03.grpc.TaskState.TASK_STATE_COMPLETED)) | ||
| .addHistory(message) | ||
| .build(); | ||
|
|
||
| var result = ProtoUtils_v0_3.FromProto.task(task); | ||
|
|
||
| assertEquals("task-1", result.id()); | ||
| assertEquals(1, result.history().size()); | ||
| var dataPart = (DataPart_v0_3) result.history().get(0).parts().get(0); | ||
| assertTrue(dataPart.data().containsKey("some_optional_field")); | ||
| assertNull(dataPart.data().get("some_optional_field")); | ||
| assertEquals("value", dataPart.data().get("other_field")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could we update ToProto.value() to handle nulls too? Now that these maps accept null values, converting them back to protobuf produces KIND_NOT_SET rather than NULL_VALUE. #1174 added an explicit setNullValue(NullValue.NULL_VALUE) for the 1.0 mapper. It would be good to do the same here and add a round-trip test.