swagger-api / swagger-api/swagger-codegen

[Java] Ineffectual code to handle file downloads

Open
#12,090 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

The implementation of GenericType.equals() requires that its argument must be an instance of a GenericType, or an instance of a descendant of it, for this method to return true.

However, ApiClient.mustache:489 reads if (returnType.equals(File.class)), where returnType is of type GenericType<T> and File.class is java.lang.Class<java.io.File>.

Thus, this branch will never execute and constitutes dead code. The Eclipse Java compiler flags code containing this fragment as "Unlikely argument type for equals(): Class seems to be unrelated to GenericType".

Similarly, ApiClient.mustache:483 reads if ("byte[]".equals(returnType.toString())), but the implementation of toString shows that the string returned from this method will always have the prefix "GenericType{" regardless of what the template parameter T is.

This fragment is in the ApiClient mustache for multiple libraries.

Swagger-codegen version

At least since 3.0.35, but present in master now.

Suggest a fix/enhancement

Maybe the tests should read something like:

final Class<?> retCls = returnType.getRawType();
if (byte[].class.equals(retCls)) {
    /* ... */
} else if (File.class.equals(retCls)) {

instead. This compiles without warnings, but I haven't tested if the code would now actually do what it is supposed to.

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 with modules/swagger-codegen/src/main/resources/Java/libraries/resteasy/ApiClient.mustache around lines 483 and 489, then check the corresponding ApiClient.mustache files for the other libraries. Inspect how generated clients handle byte-array and File downloads, and verify that the revised checks compile without the reported warning and preserve the intended behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.