Skip to content

Commit dc1b2ed

Browse files
authored
[Go] Fix generated client tests when there is no response body (#14081)
* [WIP] Isolated test case for Go api_test generator * Fix tests for API endpoints without a return type * Add the rest of the generated test fix
1 parent 09c070a commit dc1b2ed

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

modules/openapi-generator/src/main/resources/go/api_test.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ func Test_{{packageName}}_{{classname}}Service(t *testing.T) {
3939
var {{paramName}} {{{dataType}}}
4040
{{/pathParams}}
4141

42-
resp, httpRes, err := apiClient.{{classname}}.{{operationId}}(context.Background(){{#pathParams}}, {{paramName}}{{/pathParams}}).Execute()
42+
{{#returnType}}resp, {{/returnType}}httpRes, err := apiClient.{{classname}}.{{operationId}}(context.Background(){{#pathParams}}, {{paramName}}{{/pathParams}}).Execute()
4343

4444
require.Nil(t, err)
45+
{{#returnType}}
4546
require.NotNil(t, resp)
47+
{{/returnType}}
4648
assert.Equal(t, 200, httpRes.StatusCode)
4749

4850
})

modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,29 @@ public void verifyFormatErrorMessageInUse() throws IOException {
261261
TestUtils.assertFileContains(Paths.get(output + "/api_pet.go"),
262262
"newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)");
263263
}
264+
265+
@Test
266+
public void verifyApiTestWithNullResponse() throws IOException {
267+
File output = Files.createTempDirectory("test").toFile();
268+
output.deleteOnExit();
269+
270+
final CodegenConfigurator configurator = new CodegenConfigurator()
271+
.setGeneratorName("go")
272+
.setGitUserId("OpenAPITools")
273+
.setGitRepoId("openapi-generator")
274+
.setInputSpec("src/test/resources/3_0/go/petstore-with-no-response-body.yaml")
275+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
276+
277+
DefaultGenerator generator = new DefaultGenerator();
278+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
279+
files.forEach(File::deleteOnExit);
280+
281+
TestUtils.assertFileExists(Paths.get(output + "/test/api_pet_test.go"));
282+
TestUtils.assertFileNotContains(Paths.get(output + "/test/api_pet_test.go"),
283+
"require.NotNil(t, resp)");
284+
TestUtils.assertFileNotContains(Paths.get(output + "/test/api_pet_test.go"),
285+
"resp, httpRes, err := apiClient.PetApi.PetDelete(context.Background()).Execute()");
286+
TestUtils.assertFileContains(Paths.get(output + "/test/api_pet_test.go"),
287+
"httpRes, err := apiClient.PetApi.PetDelete(context.Background()).Execute()");
288+
}
264289
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
openapi: 3.0.0
2+
info:
3+
description: >-
4+
This spec is mainly for testing Petstore server and contains fake endpoints,
5+
models. Please do not use this for any other purpose. Special characters: "
6+
\
7+
version: 1.0.0
8+
title: OpenAPI Petstore
9+
license:
10+
name: Apache-2.0
11+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
12+
tags:
13+
- name: pet
14+
description: Everything about your Pets
15+
paths:
16+
/pet:
17+
delete:
18+
tags:
19+
- pet
20+
responses:
21+
'204':
22+
description: OK

0 commit comments

Comments
 (0)