fix(tool): preserve generic type info in ToolMethodInvoker#730
fix(tool): preserve generic type info in ToolMethodInvoker#730LearningGp wants to merge 2 commits intoagentscope-ai:mainfrom
Conversation
…e-ai#677) Add JsonCodec.convertValue(Object, Type) to handle parameterized types like List<T>, Map<K,V>, Set<T> during tool parameter deserialization. Change-Id: Id583a0fdfbffe4fb6da7c88e50c6f386b502ceb3 Co-developed-by: Cursor <noreply@cursor.com> Co-authored-by: Cursor <cursoragent@cursor.com>
Summary of ChangesHello @LearningGp, 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 addresses a critical issue where generic type information was not correctly preserved during the deserialization of tool parameters, leading to runtime errors. By enhancing the JSON codec to leverage Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request addresses an issue with generic type handling in the ToolMethodInvoker. The changes correctly preserve generic type information (e.g., for List<MyClass>) during parameter conversion by leveraging Parameter.getParameterizedType() and extending JsonCodec to handle java.lang.reflect.Type. The implementation in JacksonJsonCodec correctly uses Jackson's TypeFactory to construct the full generic type. The fix is well-supported by a comprehensive set of new unit tests that cover lists, maps, and nested generics. The overall change is excellent and significantly improves the robustness of tool invocation. I have one suggestion to improve an assertion in a test case to make it more robust.
| // The order of map entries is not guaranteed, so check for both possibilities | ||
| Assertions.assertTrue( | ||
| content.contains("ProductA") && content.contains("ProductB"), | ||
| "Response should contain both product names"); |
There was a problem hiding this comment.
The current assertion only verifies the presence of product names, but not their association with the correct keys. This could potentially miss bugs where the mapping is incorrect. A more robust assertion should check for the key-value pairs.
| // The order of map entries is not guaranteed, so check for both possibilities | |
| Assertions.assertTrue( | |
| content.contains("ProductA") && content.contains("ProductB"), | |
| "Response should contain both product names"); | |
| // The order of map entries is not guaranteed, so check that both key-value pairs are present. | |
| Assertions.assertTrue( | |
| content.contains("key1=ProductA") && content.contains("key2=ProductB"), | |
| "Response should contain both key-value pairs. Actual: " + content); |
There was a problem hiding this comment.
Pull request overview
This pull request fixes Issue #677 where ToolMethodInvoker was losing generic type information when deserializing parameters with generic types like List<MyClass>. Previously, this resulted in ClassCastException when trying to iterate over the list items because Jackson would deserialize to LinkedHashMap instead of the actual type.
Changes:
- Added
convertValue(Object, Type)method toJsonCodecinterface to support generic type preservation - Updated
ToolMethodInvokerto usegetParameterizedType()instead ofgetType()when converting parameters - Added comprehensive test coverage for generic type handling including List, Map, and nested generic types
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
agentscope-core/src/main/java/io/agentscope/core/util/JsonCodec.java |
Added new interface method to preserve generic type information during conversion |
agentscope-core/src/main/java/io/agentscope/core/util/JacksonJsonCodec.java |
Implemented the new method using Jackson's TypeFactory for proper generic handling |
agentscope-core/src/main/java/io/agentscope/core/tool/ToolMethodInvoker.java |
Updated parameter conversion logic to use parameterized types and the new conversion method |
agentscope-core/src/test/java/io/agentscope/core/tool/ToolMethodInvokerTest.java |
Added comprehensive tests for generic List, Map, nested generics, and edge cases |
agentscope-core/src/test/java/io/agentscope/core/util/JsonUtilsTest.java |
Updated mock implementation to include the new interface method |
Comments suppressed due to low confidence (1)
agentscope-core/src/main/java/io/agentscope/core/util/JsonCodec.java:102
- Method JsonCodec.convertValue(..) could be confused with overloaded method convertValue, since dispatch depends on static types.
<T> T convertValue(Object from, Class<T> toType);
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Change-Id: I5c6222720bb9ed62e770f1c8d884a4e97e427973 Co-developed-by: Cursor <noreply@cursor.com> Co-authored-by: Cursor <cursoragent@cursor.com>
AgentScope-Java Version
1.0.9
Description
ref(#677)
run test
Checklist
Please check the following items before code is ready to be reviewed.
mvn spotless:applymvn test)