swagger-api / swagger-api/swagger-codegen

[JAVA] Cannot use a List of File or Resource in Spring RestTemplate Client

Open
#11,987 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I have a client endpoint generated by codegen with parameter a List<File> and when the HTTP try to convert it does not convert to a FileSystemResource it agregate all files path in a list and convert to a byte[] like this: "["/etc/file1.pdf","/etc/file2.pdf"]".
I already tried a typeMapping File=org.springframework.core.io.Resource in pom plugin but this thrown me the following error:
"org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class java.nio.channels.Channels$1]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.nio.channels.Channels$1 and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ImmutableCollections$List12[0]->org.springframework.core.io.FileSystemResource["outputStream"])"

Swagger-codegen version

I am using swagger codegen swagger maven plugin with version: 3.0.36 and spring boot version: 2.7.6

Swagger declaration file content or url

YAML file:
requestBody: content: multipart/form-data: schema: type: object properties: files: type: array items: type: string format: binary

Client code generated:
` public ResponseEntity addFilesWithHttpInfo(List files) throws RestClientException {
Object postBody = null;
String path = UriComponentsBuilder.fromPath("/file").build().toUriString();

    final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
    final HttpHeaders headerParams = new HttpHeaders();
    final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();
    if (files != null)
        formParams.add("files", files);

    final String[] accepts = { 
        "application/json"
     };
    final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
    final String[] contentTypes = { 
        "multipart/form-data"
     };
    final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);

    String[] authNames = new String[] {  };

    ParameterizedTypeReference<FileId> returnType = new ParameterizedTypeReference<FileId>() {};
    return apiClient.invokeAPI(path, HttpMethod.POST, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
}`

API code generated:
public ResponseEntity<FileId>> addFiles( @RequestParam(value="files") final List<Resource> files );

If I remove the List and work with only one file, codegen works well but if I add a list of files to generate I have the errors above.

Can anyone help me?
Thanks in advance!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the multipart request through addFilesWithHttpInfo(List files), then trace how apiClient.invokeAPI handles the formParams entry for files. Compare that path with the single-file case and the addFiles(List files) declaration. Done means a list is sent as multipart file parts rather than serialized paths or JSON, while preserving the working single-file behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring-boot
Domain
api, backend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.