openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open duttonw opened this issue 7 months ago • 0 comments

Bug Report Checklist

  • [X] Have you provided a full/minimal spec to reproduce the issue?
  • [X] Have you validated the input using an OpenAPI validator?
  • [X] Have you tested with the latest master to confirm the issue still exists?
  • [X] Have you searched for related issues/PRs?
  • [X] 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<File> 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 avatar May 28 '25 21:05 duttonw