Java/RestTemplate - Allow flexible options in generated client when a file is needed
Created by: 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: 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" : ""
}
}
}
}