Skip to content

Commit 27a114d

Browse files
Frontriderwing328
authored andcommitted
[Java][Client] (#13968)
1 parent 9b3484c commit 27a114d

File tree

3 files changed

+140
-9
lines changed

3 files changed

+140
-9
lines changed

modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
{{! Openapi Generator }}
2+
{{! Copyright (2024) András Gábor Kis, Deutsche Telekom AG }}
3+
{{! This file is made available under the terms of the license Apache-2.0 license }}
4+
{{! SPDX-License-Identifier: Apache-2.0 }}
5+
16
{{>licenseInfo}}
27
package {{package}};
38

@@ -271,16 +276,40 @@ public class {{classname}} {
271276
}
272277
{{/vendorExtensions.x-java-text-plain-string}}
273278
{{^vendorExtensions.x-java-text-plain-string}}
274-
return new ApiResponse<{{{returnType}}}{{^returnType}}Void{{/returnType}}>(
275-
localVarResponse.statusCode(),
276-
localVarResponse.headers().map(),
277-
{{#returnType}}
278-
localVarResponse.body() == null ? null : memberVarObjectMapper.readValue(localVarResponse.body(), new TypeReference<{{{returnType}}}>() {}) // closes the InputStream
279-
{{/returnType}}
280-
{{^returnType}}
281-
null
282-
{{/returnType}}
279+
{{#returnType}}
280+
{{! Fix for https://github.com/OpenAPITools/openapi-generator/issues/13968 }}
281+
{{! This part had a bugfix for an empty response in the past, but this part of that PR was reverted because it was not doing anything. }}
282+
{{! Keep this documentation here, because the problem is not obvious. }}
283+
{{! `InputStream.available()` was used, but that only works for inputstreams that are already in memory, it will not give the right result if it is a remote stream. We only work with remote streams here. }}
284+
{{! https://github.com/OpenAPITools/openapi-generator/pull/13993/commits/3e!37411d2acef0311c82e6d941a8e40b3bc0b6da }}
285+
{{! The `available` method would work with a `PushbackInputStream`, because we could read 1 byte to check if it exists then push it back so Jackson can read it again. The issue with that is that it will also insert an ascii character for "head of input" and that will break Jackson as it does not handle special whitespace characters. }}
286+
{{! A fix for that problem is to read it into a string and remove those characters, but if we need to read it before giving it to jackson to fix the string then just reading it into a string as is to do an emptiness check is the cleaner solution. }}
287+
{{! We could also manipulate the inputstream to remove that bad character, but string manipulation is easier to read and this codepath is not asyncronus so we do not gain anything by reading the stream later. }}
288+
{{! This fix does make it unsuitable for large amounts of data because `InputStream.readAllbytes` is not meant for it, but a syncronus client is already not the right tool for that.}}
289+
if (localVarResponse.body() == null) {
290+
return new ApiResponse<{{{returnType}}}>(
291+
localVarResponse.statusCode(),
292+
localVarResponse.headers().map(),
293+
null
294+
);
295+
}
296+
297+
String responseBody = new String(localVarResponse.body().readAllBytes());
298+
localVarResponse.body().close();
299+
300+
return new ApiResponse<{{{returnType}}}>(
301+
localVarResponse.statusCode(),
302+
localVarResponse.headers().map(),
303+
responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<{{{returnType}}}>() {})
283304
);
305+
{{/returnType}}
306+
{{^returnType}}
307+
return new ApiResponse<{{{returnType}}}>(
308+
localVarResponse.statusCode(),
309+
localVarResponse.headers().map(),
310+
null
311+
);
312+
{{/returnType}}
284313
{{/vendorExtensions.x-java-text-plain-string}}
285314
} finally {
286315
{{^returnType}}

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3263,4 +3263,73 @@ public void testGenerateParameterId() {
32633263
" getCall(Integer queryParameter, final ApiCallback _callback)"
32643264
);
32653265
}
3266+
3267+
@Test
3268+
public void callNativeServiceWithEmptyResponseSync() throws IOException {
3269+
Map<String, Object> properties = new HashMap<>();
3270+
properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");
3271+
properties.put("asyncNative", "false");
3272+
3273+
File output = Files.createTempDirectory("test").toFile();
3274+
output.deleteOnExit();
3275+
3276+
final CodegenConfigurator configurator = new CodegenConfigurator()
3277+
.setGeneratorName("java")
3278+
.setLibrary(JavaClientCodegen.NATIVE)
3279+
.setAdditionalProperties(properties)
3280+
.setInputSpec("src/test/resources/3_0/java/native/issue13968.yaml")
3281+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
3282+
3283+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
3284+
DefaultGenerator generator = new DefaultGenerator();
3285+
3286+
Map<String, File> files = generator.opts(clientOptInput).generate().stream()
3287+
.collect(Collectors.toMap(File::getName, Function.identity()));
3288+
3289+
File apiFile = files.get("DefaultApi.java");
3290+
assertNotNull(apiFile);
3291+
3292+
JavaFileAssert.assertThat(apiFile).fileContains(
3293+
//reading the body into a string, then checking if it is blank.
3294+
"String responseBody = new String(localVarResponse.body().readAllBytes());",
3295+
"responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<LocationData>() {})"
3296+
);
3297+
}
3298+
3299+
3300+
/**
3301+
* This checks that the async client is not affected by this fix.
3302+
* See https://github.com/OpenAPITools/openapi-generator/issues/13968
3303+
*/
3304+
@Test
3305+
public void callNativeServiceWithEmptyResponseAsync() throws IOException {
3306+
Map<String, Object> properties = new HashMap<>();
3307+
properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");
3308+
properties.put("asyncNative", "true");
3309+
3310+
File output = Files.createTempDirectory("test").toFile();
3311+
output.deleteOnExit();
3312+
3313+
final CodegenConfigurator configurator = new CodegenConfigurator()
3314+
.setGeneratorName("java")
3315+
.setLibrary(JavaClientCodegen.NATIVE)
3316+
.setAdditionalProperties(properties)
3317+
.setInputSpec("src/test/resources/3_0/java/native/issue13968.yaml")
3318+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
3319+
3320+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
3321+
DefaultGenerator generator = new DefaultGenerator();
3322+
3323+
Map<String, File> files = generator.opts(clientOptInput).generate().stream()
3324+
.collect(Collectors.toMap(File::getName, Function.identity()));
3325+
3326+
File apiFile = files.get("DefaultApi.java");
3327+
assertNotNull(apiFile);
3328+
3329+
JavaFileAssert.assertThat(apiFile).fileDoesNotContain(
3330+
//reading the body into a string, then checking if it is blank.
3331+
"String responseBody = new String(localVarResponse.body().readAllBytes());",
3332+
"responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<LocationData>() {})"
3333+
);
3334+
}
32663335
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Example Hello API
4+
description: ''
5+
version: v1
6+
servers:
7+
- url: http://localhost
8+
description: Global Endpoint
9+
paths:
10+
/v1/emptyResponse:
11+
get:
12+
operationId: empty
13+
description: returns an empty response
14+
responses:
15+
200:
16+
description: Successful operation
17+
content:
18+
application/json:
19+
schema:
20+
$ref: '#/components/schemas/LocationData'
21+
204:
22+
description: Empty response
23+
components:
24+
schemas:
25+
LocationData:
26+
type: object
27+
properties:
28+
xPos:
29+
type: integer
30+
format: int32
31+
yPos:
32+
type: integer
33+
format: int32

0 commit comments

Comments
 (0)