Skip to content

[BUG] [java native] File response being passed to Jackson instead of directly converted to File object (in temp space) #21345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
5 of 6 tasks
duttonw opened this issue May 28, 2025 · 0 comments · May be fixed by #21346
Open
5 of 6 tasks

Comments

@duttonw
Copy link
Contributor

duttonw commented May 28, 2025

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When response type File is found in Swagger Def, it generates correct ApiResponse but does not include correct handling of Response body content as non json byte stream to type File.

It will read all bytes into String not checking the header
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache#L300

        String responseBody = new String(localVarResponse.body().readAllBytes());
        localVarResponse.body().close();

        return new ApiResponse<{{{returnType}}}>(
            localVarResponse.statusCode(),
            localVarResponse.headers().map(),
            responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<{{{returnType}}}>() {})
        );

generates

        String responseBody = new String(localVarResponse.body().readAllBytes());
        localVarResponse.body().close();

        return new ApiResponse<File>(
            localVarResponse.statusCode(),
            localVarResponse.headers().map(),
            responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<File>() {})
        );
openapi-generator version

Master / 7.13.0

<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>7.13.0</version>
OpenAPI declaration file content or url
"/rest/files/{id}/content": {
      "get": {
        "summary": "Download a file",
        "description": "Download a file",
        "tags": [
          "files"
        ],
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "required": true,
            "description": "The ID of the entity",
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "File download request is successful",
            "schema": {
              "type": "file"
            }
          },
          "401": {
            "description": "ERR_AUTH_UNAUTHORIZED\n"
          },
          "403": {
            "description": "ERR_ACCESS_USER\n\nERR_ENTITY_DELETED\n\nERR_ENTITY_DLP_LOCKED\n\nERR_ENTITY_IS_SECURE_FOLDER\n\nERR_ENTITY_NOT_SCANNED\n\nERR_ENTITY_VIRUS_FOUND"
          },
          "490": {
            "description": "Request blocked by WAF"
          }
        }
      }
    },
Generation Details

https://github.com/qld-gov-au/kiteworks-integration

<plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>7.13.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <!-- please ensure its updated when this plugin is updated -->
                            <!-- https://openapi-generator.tech/docs/templating/
                            cd .openapi-generator
                            curl -L https://api.github.com/repos/OpenAPITools/openapi-generator/tarball | tar xz
                            mv `ls`/modules/openapi-generator/src/main/resources/Java ./Java
                            rm -rf OpenAPITools-openapi-generator-*
                            cd Java; ls -d libraries/* | grep -v native | xargs rm -rf -->
                            <!-- what we changed: -->
                            <!-- * api.mustache -->
                            <!-- patch for null check objects on non required params -->
                            <templateDirectory>.openapi-generator/templates/Java</templateDirectory>
                            <inputSpec>${project.basedir}/src/main/resources/kiteworks.28.swagger.json</inputSpec>
                            <generatorName>java</generatorName>
                            <configOptions>
                                <dateLibrary>java8</dateLibrary> <!--java8 - Java 8 native JSR310 (preferred for jdk 1.8+) -->
                                <useJakartaEe>true</useJakartaEe>
                                <useTags>true</useTags>
                                <serializationLibrary>jackson</serializationLibrary>
                            </configOptions>

                            <library>native</library>
                            <!--                            <output>${project.build.directory}/generated-sources/openapi</output>-->
                            <apiPackage>com.kiteworks.client.api</apiPackage>
                            <modelPackage>com.kiteworks.client.model</modelPackage>
                            <invokerPackage>com.kiteworks.client</invokerPackage>
                            <cleanupOutput>false</cleanupOutput>
                            <generateApiDocumentation>false</generateApiDocumentation>

                            <generateApiTests>false</generateApiTests>
                            <generateModelDocumentation>false</generateModelDocumentation>
                            <generateModelTests>false</generateModelTests>
                            <configHelp>false</configHelp>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Steps to reproduce

Call a server endpoint that returns File as response. It throws exception.

Related issues/PRs

#19491

Suggest a fix

What should it do;

It should check the response header for what type of payload it received as well as check returnType for 'File' and handling accordingly.

duttonw added a commit to duttonw/openapi-generator that referenced this issue May 29, 2025
duttonw added a commit to duttonw/openapi-generator that referenced this issue May 29, 2025
duttonw added a commit to duttonw/openapi-generator that referenced this issue May 29, 2025
duttonw added a commit to duttonw/openapi-generator that referenced this issue May 29, 2025
duttonw added a commit to duttonw/openapi-generator that referenced this issue May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant