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/RestTemplate - Allow flexible options in generated client when a file is needed #6715

Closed
MosheElisha opened this issue Jun 19, 2020 · 14 comments · Fixed by #6912
Closed

Comments

@MosheElisha
Copy link
Contributor

Hi,

I have an API that requires a org.springframework.web.multipart.MultipartFile parameter (Swagger 2.0 API spec is below).

I am using generatorName=java and library=resttemplate to generate the client.

The generated Api client looks like:

public ResponseEntity<Template> createTemplateWithHttpInfo(File file, com.example.model.CreateTemplateRequest request) throws RestClientException {
...
    if (file != null)
            formParams.add("file", new FileSystemResource(file));
...

I would like to have the option to generate a client that takes an org.springframework.core.io.AbstractResource instead of a File.
The user will be able to pass a new FileSystemResource(file) to have same behavior as now but if the user does not have the file on disk, he/she can use an InputStreamResource or a ByteArrayResource or ... For example:

    api.createTemplateWithHttpInfo(
        new ByteArrayResource(byteArray) {
          @Override
          public String getFilename() {
            return "memoryBytes";
          }
        },
        ...
    );

From the source code it seems that there is no such option:

formParams.{{^collectionFormat}}add{{/collectionFormat}}{{#collectionFormat}}put{{/collectionFormat}}("{{baseName}}", {{#isFile}}new FileSystemResource({{paramName}}){{/isFile}}{{^isFile}}{{paramName}}{{/isFile}});{{#hasMore}}

I'm proposing a new configOption (something like "useAbstractionForFiles") and every library can try to support as possible (My first priority is to have the resttemplate library support this).

Swagger 2.0 API spec:

      "post" : {
        "operationId" : "createTemplate",
        "consumes" : [ "multipart/form-data" ],
        "produces" : [ "application/json" ],
        "parameters" : [ {
          "name" : "file",
          "in" : "formData",
          "required" : true,
          "type" : "ref"
        }, {
          "name" : "request",
          "in" : "formData",
          "required" : true,
          "type" : "ref"
        } ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "$ref" : "#/definitions/Template"
            }
          },
          "201" : {
            "description" : ""
          }
        }
      }
    }
@MosheElisha
Copy link
Contributor Author

@wing328 Can you please start the discussion or assign to / tag relevant engineers? Thanks.

@wing328
Copy link
Member

wing328 commented Jun 25, 2020

As discussed, it looks reasonable to me to add another option to meet the requirement. Please go ahead to submit a PR and we'll review the change accordingly.

MosheElisha added a commit to MosheElisha/openapi-generator that referenced this issue Jul 9, 2020
…va/RestTemplate to allow flexible options in generated client when a file is needed.
MosheElisha added a commit to MosheElisha/openapi-generator that referenced this issue Jul 12, 2020
…va/RestTemplate to allow flexible options in generated client when a file is needed.
MosheElisha added a commit to MosheElisha/openapi-generator that referenced this issue Jul 12, 2020
…llow flexible options in generated client when a file is needed (OpenAPITools#6715)
MosheElisha added a commit to MosheElisha/openapi-generator that referenced this issue Jul 12, 2020
…llow flexible options in generated client when a file is needed (OpenAPITools#6715)
MosheElisha added a commit to MosheElisha/openapi-generator that referenced this issue Jul 12, 2020
…llow flexible options in generated client when a file is needed (OpenAPITools#6715)
MosheElisha added a commit to MosheElisha/openapi-generator that referenced this issue Jul 12, 2020
…llow flexible options in generated client when a file is needed (OpenAPITools#6715)
MosheElisha added a commit to MosheElisha/openapi-generator that referenced this issue Jul 20, 2020
…va/RestTemplate to allow flexible options in generated client when a file is needed.
wing328 pushed a commit that referenced this issue Jul 20, 2020
* Fix dir path in PR request template

* Add "useAbstractionForFiles" config option for Java/RestTemplate to allow flexible options in generated client when a file is needed (#6715)

* Add "useAbstractionForFiles" config option for Java/RestTemplate to allow flexible options in generated client when a file is needed (#6715)

* Add "useAbstractionForFiles" config option for Java/RestTemplate to allow flexible options in generated client when a file is needed (#6715)

* #6715 - Add "useAbstractionForFiles" config option for Java/RestTemplate to allow flexible options in generated client when a file is needed.
@nrgraham23
Copy link

Hello,

Is there documentation on how this is used?

I am using gradle and have done the following:

Added this to my dependencies:

compile group: 'org.springframework.android', name: 'spring-android-rest-template', version: '1.0.1.RELEASE'

Updated our generator:

id "org.openapi.generator" version "5.0.0-beta"

And added this option to the configOptions:

configOptions = [ dateLibrary: "java8", serializationLibrary: "jackson", useAbstractionForFiles: "true" ]

However, the method signature still returns a File:

public File dooFoo(String foo) throws ApiException

Thanks for adding this feature!

@MosheElisha
Copy link
Contributor Author

Hi,

The feature is not included in 5.0.0-beta and will be included in 5.0.0-beta2 that is expected to be released in 30.07.2020.
If you would like to test this feature earlier, you should use the latest 5.0.0-SNAPSHOT.

@ogerardin
Copy link

ogerardin commented Jul 24, 2020

This is a good idea, but is there a reason for not using the interface Resource instead of AbstractResource ?

PS: just tried with a custom template using Resource and it works as expected. Always prefer interfaces :)

@MosheElisha
Copy link
Contributor Author

This is a good idea, but is there a reason for not using the interface Resource instead of AbstractResource ?

Good question :-). No good reason except it was just missed. I will change it to Resource soon. Thanks!

@iyzana
Copy link
Contributor

iyzana commented Sep 30, 2020

It seems like this option should be present for the webclient generator as well.
It is very similar to the resttemplate generator.

@sblumenstock-doubleSlash

How can I use this with the command line?
openapi-generator-cli generate -i petstore.yaml -g java --library=resttemplate --useAbstractionForFiles=true -o ./Desktop/OpenApi/test1

This is not working: [error] Found unexpected parameters: [--useAbstractionForFiles=true]

@MosheElisha
Copy link
Contributor Author

@sblumenstock-doubleSlash Because this is a config option, I believe you should use the additional-properties flag:

--additional-properties=useAbstractionForFiles=true

More details here: https://openapi-generator.tech/docs/usage/#additional-properties

@sblumenstock-doubleSlash

Ahh thank you! :)
I missed that.

@sblumenstock-doubleSlash

Is this only implemented when uploading a file?
When I generate a client, the downloads are still files.
Doesn't it also make sense to implement this in the downloads?

@MosheElisha
Copy link
Contributor Author

It is currently implemented for uploading files. That was my immediate need. Agree that it will be useful to get an InputStream (or similar) when downloading as well.

I don't have time to do it now but maybe in the future. If it is important for you, please feel free.

@sblumenstock-doubleSlash

I tried to solve it by myself, but I got an issue.
public {{#returnType}}{{#isFile}}{{#useAbstractionForFiles}}...
IsFile cannot be used for returnType. How do I implement this?

@sblumenstock-doubleSlash

I tried to solve it by myself, but I got an issue.
public {{#returnType}}{{#isFile}}{{#useAbstractionForFiles}}...
IsFile cannot be used for returnType. How do I implement this?

How can I check if the returnType is a file?
Is there a boolean that checks this?

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 a pull request may close this issue.

6 participants