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

Java RESTEasy client does not provide the "filename" directive when creating "multipart/form-data" requests #17367

Closed
miladhub opened this issue Dec 10, 2023 · 8 comments

Comments

@miladhub
Copy link
Contributor

Description

When generating a Java RESTEasy client for an API that creates "multipart/form-data" requests, the HTTP request lacks the "filename" directive. The generated code seems to simply omit the parameter - i.e., instead of calling this API, it calls this one. Note that the generated code has the file at hand, so it is entirely within its reach to provide it.

openapi-generator version

7.2.0-SNAPSHOT

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: 1.0.0
  title: Foo API
servers:
  - url: http://127.0.0.1:8080/{basePath}
    variables:
      basePath:
        default: v1
paths:
  /foo:
    post:
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                bar:
                  type: string
                  format: binary
      responses:
        '200':
          description: OK
Command line used for generation
java -jar openapi-generator-cli.jar generate -i api.yaml -g java --library resteasy
Steps to reproduce
  1. Generate the client with the command above
  2. Add a Main class with the following contents
package org.openapitools.client;
import org.openapitools.client.api.DefaultApi;
import java.io.File;
public class Main {
    public static void main(String[] args) throws ApiException {
        ApiClient defaultClient = Configuration.getDefaultApiClient();
        defaultClient.setBasePath("http://localhost:8080/v1");
        DefaultApi apiInstance = new DefaultApi(defaultClient);
        File bar = new File("pom.xml");
        apiInstance.fooPost(bar);
    }
}
  1. Build with mvn clean install
  2. On another tab, listen for incoming connections with nc localhost -l 8080 | grep "Content-Disposition"
  3. Run the client project with mvn exec:java -Dexec.mainClass="org.openapitools.client.Main"

The tab open at point 4 should display the following:

$ nc localhost -l 8080 | grep "Content-Disposition"
Content-Disposition: form-data; name="bar"

showing that the "filename" directive is missing. The output should have been

Content-Disposition: form-data; name="bar"; filename="pom.xml"

Note - The last command needs to be eventually killed.

Related issues/PRs

Found none.

Suggest a fix/enhancement

The fix should be a matter of adding , file.getName() on this line of generated code:

File file = (File) param.getValue();
try {
  multipart.addFormData(param.getKey(),new FileInputStream(file),MediaType.APPLICATION_OCTET_STREAM_TYPE); // Here
} catch (FileNotFoundException e) {
  throw new ApiException("Could not serialize multipart/form-data "+e.getMessage());
}
@wing328
Copy link
Member

wing328 commented Dec 11, 2023

@miladhub thanks for reporting the issue. Can you please file a PR with the suggested fix?

@miladhub
Copy link
Contributor Author

Yes I can look into that.

@miladhub
Copy link
Contributor Author

I tried to push the fix on a branch to then create a PR, but the push is getting refused:

$ git push --set-upstream origin pr_17367_multipart_filenames
remote: Permission to OpenAPITools/openapi-generator.git denied to miladhub.
fatal: unable to access 'https://github.com/OpenAPITools/openapi-generator.git/': The requested URL returned error: 403

@miladhub
Copy link
Contributor Author

I've also tried with SSH and signed commits, but I still get an unauthorized error. Attaching patch.
added_filename_directive_for_Java_RESTEasy_file_uploads_#17367.patch

@miladhub
Copy link
Contributor Author

For the record, I've tried this exact process on a repo of my own and it works, so it must really be a matter of permissions. Let me know if I can do anything, but feel free to use my patch, thanks.

@wing328
Copy link
Member

wing328 commented Dec 18, 2023

@miladhub
Copy link
Contributor Author

Thanks! Created PR: #17435

wing328 pushed a commit that referenced this issue Dec 21, 2023
…e uploads (#17367) (#17435)

* added filename directive for Java RESTEasy file uploads (#17367)

* regenerated samples
wing328 pushed a commit that referenced this issue Dec 26, 2023
…i client (#17367) (#17470)

* updating test to use the Java RESTEasy echo api client (#17367)

* regenerated samples

* set source and target to 1.8 to fix workflow failure, adapted parser
@miladhub
Copy link
Contributor Author

miladhub commented Jan 8, 2024

Verified end-to-end against version 7.2.0.

@miladhub miladhub closed this as completed Jan 8, 2024
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

No branches or pull requests

2 participants