question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Java/RestTemplate - Allow flexible options in generated client when a file is needed

See original GitHub issue

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: https://github.com/OpenAPITools/openapi-generator/blob/5cdc9e9e35b737b04a5a201446fb430a554ef5db/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache#L133

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" : ""
          }
        }
      }
    }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
sblumenstock-doubleSlashcommented, Nov 17, 2020

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?

1reaction
MosheElishacommented, Jul 24, 2020

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basic Authentication with the RestTemplate - Baeldung
The host and port values should be dependent on the environment, allowing the client the flexibility to define one set of values for ......
Read more >
Complete Guide to Spring RestTemplate - Reflectoring
According to the official documentation, RestTemplate is a synchronous client to perform HTTP requests. It is a higher-order API since it ...
Read more >
Spring WebClient | Java Development Journal
The WebClient provides create() method to create a default instance. This is the most basic steps to create the client. The functional API ......
Read more >
Using the OpenAPI Generator for Spring Boot - mimacom blog
xml file in order to generate the client-side code during the Maven build. For the example, a REST template based implementation is generated, ......
Read more >
Securing Applications and Services Guide - Keycloak
When securing clients and services the first thing you need to decide is ... Create a keycloak.json adapter configuration file within the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found