Skip to content

fix(gemini): strip query string and fragment before extracting file e…#1666

Open
hangweizhang wants to merge 1 commit into
agentscope-ai:mainfrom
hangweizhang:fix/issue-1645-gemini-media-query-string
Open

fix(gemini): strip query string and fragment before extracting file e…#1666
hangweizhang wants to merge 1 commit into
agentscope-ai:mainfrom
hangweizhang:fix/issue-1645-gemini-media-query-string

Conversation

@hangweizhang

Copy link
Copy Markdown
Contributor

…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 #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.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

…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
@hangweizhang hangweizhang requested a review from a team June 7, 2026 02:58
@codecov

codecov Bot commented Jun 7, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...pe/core/formatter/gemini/GeminiMediaConverter.java 92.85% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/core/model Model providers and formatters labels Jun 7, 2026

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core/model Model providers and formatters bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:GeminiMediaConverter rejects media URLs with query strings

2 participants