Skip to content

feat: add convenience constructors to FileWithBytes#706

Open
ehsavoie wants to merge 1 commit intoa2aproject:mainfrom
ehsavoie:issue_703
Open

feat: add convenience constructors to FileWithBytes#706
ehsavoie wants to merge 1 commit intoa2aproject:mainfrom
ehsavoie:issue_703

Conversation

@ehsavoie
Copy link
Collaborator

@ehsavoie ehsavoie commented Mar 5, 2026

Add three constructors to FileWithBytes that accept a File, a Path, or a byte[] and handle base64 encoding internally, removing the need for callers to encode content before construction.

Fixes #702 🦕

@ehsavoie ehsavoie requested a review from jmesnil March 5, 2026 14:03
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 enhances the FileWithBytes class by introducing new constructors that streamline its instantiation. Previously, users had to manually base64 encode file content before creating a FileWithBytes object. The new constructors now accept File objects, Path objects, or raw byte[] arrays, handling the base64 encoding automatically. This change significantly improves the developer experience by reducing boilerplate code and potential errors, making the FileWithBytes API more intuitive and user-friendly.

Highlights

  • New Convenience Constructors: Added three new constructors to the FileWithBytes class, allowing direct creation from java.io.File, java.nio.file.Path, or raw byte[] inputs.
  • Automated Base64 Encoding: The new constructors internally handle the Base64 encoding of file content, removing the need for callers to pre-encode data.
  • Comprehensive Testing: Introduced a new test class, FileWithBytesTest, with extensive unit tests covering the functionality, edge cases, and consistency of the new constructors.
Changelog
  • spec/src/main/java/io/a2a/spec/FileWithBytes.java
    • Added new constructors for File, Path, and byte[] inputs.
    • Updated Javadoc formatting for list items.
    • Imported necessary Java I/O and utility classes (File, IOException, Files, Path, Base64).
  • spec/src/test/java/io/a2a/spec/FileWithBytesTest.java
    • Added a new test file to validate the functionality of the new FileWithBytes constructors.
    • Included tests for content encoding, file name derivation, MIME type propagation, empty content, and consistency across constructors.
  • spec/src/test/resources/a2a-logo-white.svg
    • Added a new SVG resource file for use in tests.
Activity
  • No activity has been recorded for this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces three new convenience constructors to the FileWithBytes record, allowing direct creation from File, Path, or byte[] inputs, with internal Base64 encoding, which significantly simplifies the API for callers. While these changes improve usability and are accompanied by comprehensive unit tests, they introduce a potential Denial of Service (DoS) vulnerability. The constructors read entire files into memory without size limits, which could lead to an OutOfMemoryError when processing large files. It is recommended to implement file size validation in these constructors to mitigate this risk. Additionally, an unnecessary throws IOException declaration in one constructor should be removed to improve API accuracy, aligning with best practices for Javadoc and API contract integrity.

Copy link
Collaborator

@jmesnil jmesnil left a comment

Choose a reason for hiding this comment

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

Gemini remark is good but its solution is not :)

@ehsavoie
Copy link
Collaborator Author

ehsavoie commented Mar 5, 2026

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces convenience constructors to the FileWithBytes class, allowing direct creation from File, Path, or byte[] inputs with internal base64 encoding, simplifying the API for callers. It also utilizes lazy loading for file content. However, the implementation has critical security and robustness concerns. The internal source field leaks server-side file paths during JSON serialization because it is not marked as transient. Furthermore, the byte[] constructor does not clone the input array, breaking the class's immutability, and there's a potential for service crashes due to broken deserialization logic in the associated JsonUtil adapters. Finally, attention is needed regarding the equals and hashCode contract and documentation consistency.

@ehsavoie
Copy link
Collaborator Author

ehsavoie commented Mar 6, 2026

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors FileWithBytes from a record to a class, introducing convenience constructors for File, Path, and byte[] sources. The implementation includes lazy loading of file content with soft-reference caching, which is a great improvement for performance and memory management. The custom Gson TypeAdapter correctly prevents leaking internal fields during serialization, and the new tests are comprehensive.

I've found one minor issue regarding inconsistent validation in one of the new constructors, which I've detailed in a specific comment.

Add three constructors to FileWithBytes that accept a File, a Path, or
a byte[] and handle base64 encoding internally, removing the need for
callers to encode content before construction.

Signed-off-by: Emmanuel Hugonnet <ehugonne@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feat]: Update FileWithBytes to be able to take a java.io.File as a parameter

2 participants