Skip to content

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

Closed
@MosheElisha

Description

@MosheElisha

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions