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

POC to reattach MultipartFiles to objects #3165

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

cromoteca
Copy link
Contributor

@cromoteca cromoteca commented Jan 17, 2025

This POC adds some preliminary code to reattach MultipartFiles to arbitrary objects.

It has been tested with this service:

public record Customer(String name, List<Order> orders) {}
public record Order(List<String> items, MultipartFile invoice) {}

public String uploadCustomer(Customer customer) {
    System.out.format("Uploaded customer `%s` with %d orders\n", customer.name(), customer.orders().size());
    for (Order order : customer.orders()) {
        System.out.format("Order with %d items and invoice `%s` with size %d\n",
                order.items().size(),
                order.invoice().getOriginalFilename(),
                order.invoice().getSize());
    }
    return "Uploaded customer " + customer.name();
}

and this request:

POST http://localhost:8080/connect/HelloEndpoint/uploadCustomer 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

{
  "customer": {
    "name": "John Doe",
    "orders": [
      {
        "items": [
          "apple",
          "banana"
        ]
      },
      {
        "items": [
          "grape",
          "orange",
          "pear"
        ]
      }
    ]
  }
}
------MYBOUNDARY
Content-Disposition: form-data; name="/customer/orders/0/invoice"; filename="invoice1.txt"
Content-Type: text/plain

invoice of the first order
------MYBOUNDARY
Content-Disposition: form-data; name="/customer/orders/1/invoice"; filename="invoice2.txt"
Content-Type: text/plain

invoice of the second order
------MYBOUNDARY--

The idea is that the request will contain one part which has no filename and is of type application/json. This is the ordinary object.

Then, the other parts are supposed to have a filename and a name that is used as path in the JSON, using the path syntax that Jackson uses internally.

Related to #3130

Copy link

codecov bot commented Jan 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.65%. Comparing base (b241ce6) to head (891d775).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3165   +/-   ##
=======================================
  Coverage   92.65%   92.65%           
=======================================
  Files          85       85           
  Lines        3187     3187           
  Branches      778      778           
=======================================
  Hits         2953     2953           
  Misses        182      182           
  Partials       52       52           
Flag Coverage Δ
unittests 92.65% <ø> (ø)

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

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.

1 participant