Skip to content
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

feat(controller): support receiving MultipartFiles #3165

Merged
merged 15 commits into from
Jan 31, 2025

Conversation

cromoteca
Copy link
Contributor

@cromoteca cromoteca commented Jan 17, 2025

Multipart requests are supposed to contain some files (with name) and one JSON part named "hilla_body_part", which contains the ordinary data.

The part names for files must be named according to the JSON syntax for accessing a JSON object. So, for example, if a method has a parameter named uploadedFile, the corresponding path will be /uploadedFile. But paths can also point anywhere in the object, like /orders/1/invoice.

Closes #3130

Copy link

codecov bot commented Jan 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.89%. Comparing base (56ec992) to head (6bc87cc).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3165   +/-   ##
=======================================
  Coverage   92.89%   92.89%           
=======================================
  Files          85       85           
  Lines        3334     3334           
  Branches      801      801           
=======================================
  Hits         3097     3097           
  Misses        184      184           
  Partials       53       53           
Flag Coverage Δ
unittests 92.89% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cromoteca cromoteca changed the title feat: add POC to reattach MultipartFiles to objects POC to reattach MultipartFiles to objects Jan 19, 2025
@cromoteca
Copy link
Contributor Author

To be clear, this also works for top-level parameters:

public String uploadAvatar(String user, MultipartFile file) {
    return String.format("The avatar for %s is %d bytes.\n", user, file.getSize());
}

Request follows:

POST http://localhost:8080/connect/HelloEndpoint/uploadAvatar HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:134.0) Gecko/20100101 Firefox/134.0
Accept: application/json
Accept-Language: en-GB,en;q=0.8,fr-FR;q=0.6,fr;q=0.4,it;q=0.2
Accept-Encoding: gzip, deflate, br, zstd
Content-Type: multipart/form-data;boundary=----MYBOUNDARY
Referer: http://localhost:8080/
X-CSRF-Token: e44c315f-b825-4057-ba5f-2967ed167c71
Origin: http://localhost:8080
DNT: 1
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
Connection: keep-alive
Cookie: Idea-ca342cc6=93254400-1659-49ba-9b12-282fb6325c2b; JSESSIONID=04D782331CAE64FB18EED57A754B7661; csrfToken=e44c315f-b825-4057-ba5f-2967ed167c71
Priority: u=0
Pragma: no-cache
Cache-Control: no-cache

------MYBOUNDARY
Content-Disposition: form-data; name="data"
Content-Type: application/json

{
  "user": "John"
}
------MYBOUNDARY
Content-Disposition: form-data; name="/file"; filename="avatar.png"
Content-Type: image/png

iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAdnJLH8AAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAZQTFRFAAAA/wAAG/+NIgAAAAF0Uk5TAEDm2GYAAABsSURBVDjLvZNLCgAhDEOT+196GBgY2yYGXJhl+qz9KNDFV/DiJ+GXeCV+jxJYTEqCWil+ATA1yk67M4h4AptbiyHLkglaCgYgdJ4BbgfDMcc+2rEJjJTc6GSZ4c0aALY++6vOAaS4/7oNWY0H7vQCVCaU9zAAAAAASUVORK5CYII=
------MYBOUNDARY--

@cromoteca cromoteca changed the title POC to reattach MultipartFiles to objects feat(controller): support receiving MultipartFiles Jan 27, 2025
@cromoteca cromoteca marked this pull request as ready for review January 27, 2025 20:17
@taefi taefi self-requested a review January 30, 2025 12:15
Copy link
Contributor

@taefi taefi left a comment

Choose a reason for hiding this comment

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

Looks good!
Just suggested another test for verifying the possibility of handling multiple files as endpoint's params.

assertEquals(HttpStatus.OK, response.getStatusCode());
assertTrue(response.getBody().contains("Check John's file length OK"));
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest adding another test that verifies having multiple files as endpoint's params also works.

@cromoteca cromoteca requested a review from taefi January 31, 2025 10:26
Copy link
Contributor

@taefi taefi left a comment

Choose a reason for hiding this comment

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

LGTM!

@cromoteca cromoteca merged commit bc11e18 into main Jan 31, 2025
15 checks passed
@cromoteca cromoteca deleted the feat/support-multipartfile-in-controller branch January 31, 2025 13:53
mcollovati added a commit to mcollovati/quarkus-hilla that referenced this pull request Feb 1, 2025
Enhances extension compatibility with file upload changes from
vaadin/hilla#3165.
Updates EndpointController to utilize a custom HttpServletRequest,
ensuring seamless integration with Quarkus Multipart Form data handling.
mcollovati added a commit to mcollovati/quarkus-hilla that referenced this pull request Feb 3, 2025
Enhances extension compatibility with file upload changes from
vaadin/hilla#3165.
Updates EndpointController to utilize a custom HttpServletRequest,
ensuring seamless integration with Quarkus Multipart Form data handling.
mcollovati added a commit to mcollovati/quarkus-hilla that referenced this pull request Feb 3, 2025
Enhances extension compatibility with file upload changes from
vaadin/hilla#3165.
Updates EndpointController to utilize a custom HttpServletRequest,
ensuring seamless integration with Quarkus Multipart Form data handling.
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.

File upload support for Hilla endpoint controller
2 participants