openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[Java] InputStream is preferable to File from oas v3 with format binary

Open fuj1g0n opened this issue 8 years ago • 5 comments

Description

Currently, File type is employed in generated codes from oas v3 with format binary. I think InputStream type is more suitable for it.

I locally test with jersey, and found that jersey accepts File type as request body entity, but it internally writes the binary stream to local disk, and passes the wrote file to the resource class argument. It is not good for performance, so I think we should change type into InputStream

openapi-generator version

current master (abe741f)

OpenAPI declaration file content or url

https://gist.github.com/fujigon/dd00a99b79a2bc811761b29e63e08a39#file-openapi-yaml

Command line used for generation
# git clone, etc
mvn package
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i openapi.yaml -g jaxrs-jersey
Steps to reproduce

execute the command, and it successfully runs. I get

  • https://gist.github.com/fujigon/dd00a99b79a2bc811761b29e63e08a39#file-generated_testsapi-java
  • https://gist.github.com/fujigon/dd00a99b79a2bc811761b29e63e08a39#file-generated_testsapiservice-java and so on.
Related issues/PRs
Suggest a fix/enhancement

https://github.com/fujigon/openapi-generator/tree/feature/fix-primitive-type-resolve

fuj1g0n avatar Sep 07 '18 06:09 fuj1g0n

I would like to +1 this idea. My scenario is slightly different in that I could use an InputStream on the Java client side. The client generated method returns a File object. I want the bytes to be streamed and read in, so it's inefficient to write the bytes to disk then immediately read them back into memory.

ndjensen avatar Dec 06 '18 16:12 ndjensen

This seems to be related to #6715 File is replaced with Resource for the resttemplate

ajeans avatar Aug 17 '20 14:08 ajeans

Any update this?

Cloudmersive avatar Nov 14 '20 20:11 Cloudmersive

In my case I also have a problem with the client side (spring-mvc) that expects File for format: binary but I have a spring error because of the content-type: application/pdf "Content type 'application/pdf' not supported for bodyType=java.io.File

sebastianblesgen avatar Feb 11 '22 15:02 sebastianblesgen

Same here, seems like the webclient generation has a problem. The generated code tries to convert the response into a file object but fails:

Caused by: org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'image/png' not supported for bodyType=java.io.File
	at org.springframework.web.reactive.function.BodyExtractors.lambda$readWithMessageReaders$12(BodyExtractors.java:206)
	Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
    /**
     * Fetch tyre label image for a given id.
     * 
     * <p><b>200</b> - OK
     * @param id The id parameter
     * @return File
     * @throws WebClientResponseException if an error occurs while attempting to invoke the API
     */
    public Mono<File> fetchImage(String id) throws WebClientResponseException {
        ParameterizedTypeReference<File> localVarReturnType = new ParameterizedTypeReference<File>() {};
        return fetchImageRequestCreation(id).bodyToMono(localVarReturnType);
    }

Drevsh avatar Mar 15 '23 07:03 Drevsh