OpenAPITools / OpenAPITools/openapi-generator

APITest doesn't compile if the return type is a File

Open Beginner friendly
#14,760 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

We generate with the generator version 6.1.0 a client rest api (6.0.1 works just fine):
java -jar ./openapi-generator-cli-6.1.0.jar generate -g java -o download/javaClient_6_1 -i downlaod/openapi.json -c download/configJavaClient.json -t downoad/templates/SpringClient --global-property skipFormModel=false
configJavaClient:
{
"title": "Download Service",
"groupId": "example.api.java.client",
"artifactId": "download",
"hideGenerationTimestamp": "false",
"basePackage": "example.client.download",
"apiPackage": "example.client.download.api",
"configPackage": "example.client.downoad.configuration",
"invokerPackage": "example.client.download.api",
"modelPackage": "example.client.download.model",
"useGzipFeature": false,
"booleanGetterPrefix": "is",
"dateLibrary": "java8",
"java8": true,
"interfaceOnly": true,
"library": "resttemplate",
"sourceFolder": "src/main/java",
"useOptional": false,
"withXml": false,
"reactive": false,
"serializableModel": true,
"useTags": true,
"useSpringfox": false,
"useAbstractionForFiles": true
}
Minimal API definition:
{
"openapi": "3.0.3",
"info": {
"title": "Download PDF",
"description": "Download PDF",
"contact": {
"name": "XYZ",
"email": "XYZ"
},
"version": "0.0.1"
},
"paths": {
"/download/{id}": {
"parameters": [
{
"name": "id",
"in": "path",
"description": "document ID",
"example": "1",
"required": true,
"schema": {
"type": "string"
}
}
],
"get": {
"summary": "returns a pdf document",
"description": "returns a pdf document",
"operationId": "download",
"tags": [
"pdfdownload"
],
"responses": {
"200": {
"description": "PDF document",
"content": {
"application/pdf": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
},
"default": {
"description": "error",
"content": {
"application/json": {
"schema": {
"type": "object",
"description": "Error data",
"properties": {
"errorNumber": {
"type": "integer",
"description": "error number.",
"example": 2
}
},
"required": [
"errorNumber"
]
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"ErrorData": {
"type": "object",
"description": "Error data",
"properties": {
"errorNumber": {
"type": "integer",
"description": "error number.",
"example": 2
}
},
"required": [
"errorNumber"
]
}
}
}
}

Generated ApiTest:

@Test
public void downloadTest() {
String authorization = null;
String id = null;

File response = api.download(id);

    // TODO: test validations
}

while the download method returns org.springframework.core.io.Resource. So there is a compile error.

This bug was provided within the commit version fa22ba9dd9932f0a410afb6e8c6bdb9576d2d643 at 22.08.2022. So this commit for it self is totally ok, but the mustache file modules\openapi-generator\src\main\resources\Java\libraries\resttemplate\api_test.mustache needs to be adjusted as well. The line #41:

{{#returnType}}{{{.}}} response = {{/returnType}}api.{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});

got to be replaced by

{{#returnType}}{{#isResponseFile}}{{#useAbstractionForFiles}}org.springframework.core.io.Resource{{/useAbstractionForFiles}}{{^useAbstractionForFiles}}{{{.}}}{{/useAbstractionForFiles}}{{/isResponseFile}}{{^isResponseFile}}{{{.}}}{{/isResponseFile}} response = {{/returnType}}api.{{operationId}}({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});

to do the same treatment for the return value as in the generatet api function.

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/openapi-generator/src/main/resources/Java/libraries/resttemplate/api_test.mustache and compare its return-type handling with the generated API method. Regenerate the minimal Java client using useAbstractionForFiles and verify that the generated ApiTest compiles with the Resource return type.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.