fix(gemini): strip query string and fragment before extracting file e…#1666
fix(gemini): strip query string and fragment before extracting file e…#1666hangweizhang wants to merge 1 commit into
Conversation
…xtension GeminiMediaConverter.extractExtension() parsed the entire URL suffix after the last dot as the file extension, which included query strings and fragments. This caused URLs like http://localhost/cat.png?token=abc123 to produce extension "png?token=abc123" instead of "png", resulting in an IllegalArgumentException. Similarly, readFileAsBytes() failed for local file paths that contained query strings or fragments because Paths.get() treated them as part of the filename. Changes: - Add stripQueryAndFragment() utility to strip ?... and #... from URLs - Refactor extractExtension() to use stripQueryAndFragment() before extracting the extension - Refactor readFileAsBytes() to strip query/fragment from local file paths before resolving them on disk - Add test cases for URLs with query strings, fragments, and both Closes agentscope-ai#1645
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
AgentScopeJavaBot
left a comment
There was a problem hiding this comment.
🤖 AI Review
Clean, focused bug fix for Issue #1645. The new stripQueryAndFragment() utility correctly handles URLs and local paths containing query strings (?...) and fragments (#...), which previously caused extractExtension() to produce invalid extensions like png?token=abc123 and readFileAsBytes() to fail on local paths with appended query parameters. The implementation is minimal, well-documented with updated Javadoc, applied consistently in both affected methods, and includes three test cases covering query strings, fragments, and both combined. No issues found — this is a textbook bug fix.
| /** | ||
| * Strips query string ({@code ?...}) and fragment ({@code #...}) from a URL/path string. | ||
| */ | ||
| private static String stripQueryAndFragment(String url) { |
There was a problem hiding this comment.
[praise] Clean, focused utility method. The fix is applied consistently in both extractExtension() and readFileAsBytes(), the stripping order (fragment first, then query) correctly handles all URL forms including malformed ones like path#frag?query, and the error messages still reference the original URL for debugging. Good test coverage for the three main cases.
…xtension
GeminiMediaConverter.extractExtension() parsed the entire URL suffix after the last dot as the file extension, which included query strings and fragments. This caused URLs like
http://localhost/cat.png?token=abc123 to produce extension "png?token=abc123" instead of "png", resulting in an IllegalArgumentException.
Similarly, readFileAsBytes() failed for local file paths that contained query strings or fragments because Paths.get() treated them as part of the filename.
Changes:
Closes #1645
AgentScope-Java Version
[The version of AgentScope-Java you are working on, e.g. 1.0.12, check your pom.xml dependency version or run
mvn dependency:tree | grep agentscope-parent:pom(only mac/linux)]Description
[Please describe the background, purpose, changes made, and how to test this PR]
Checklist
Please check the following items before code is ready to be reviewed.
mvn spotless:applymvn test)