Skip to content

Commit 6191f10

Browse files
committed
fix: OpenAPITools#21345 Java Native Client, handle Byte[] and File response types correctly
1 parent 05e672d commit 6191f10

File tree

1 file changed

+59
-1
lines changed
  • modules/openapi-generator/src/main/resources/Java/libraries/native

1 file changed

+59
-1
lines changed

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,54 @@ public class {{classname}} {
100100
return operationId + " call failed with: " + statusCode + " - " + body;
101101
}
102102

103+
/**
104+
* Download file from the given response.
105+
*
106+
* @param response Response
107+
* @return File
108+
* @throws ApiException If fail to read file content from response and write to disk
109+
*/
110+
public File downloadFileFromResponse(HttpResponse<InputStream> response) throws ApiException {
111+
try {
112+
File file = prepareDownloadFile(response);
113+
java.nio.file.Files.copy(response.body(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
114+
return file;
115+
} catch (IOException e) {
116+
throw new ApiException(e);
117+
}
118+
}
119+
120+
/**
121+
* <p>Prepare the file for download from the response.</p>
122+
*
123+
* @param response a {@link java.net.http.HttpResponse} object.
124+
* @return a {@link java.io.File} object.
125+
* @throws java.io.IOException if any.
126+
*/
127+
private File prepareDownloadFile(HttpResponse<InputStream> response) throws IOException {
128+
String filename = null;
129+
java.util.Optional<String> contentDisposition = response.headers().firstValue("Content-Disposition");
130+
if (contentDisposition.isPresent() && !"".equals(contentDisposition.get())) {
131+
// Get filename from the Content-Disposition header.
132+
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
133+
java.util.regex.Matcher matcher = pattern.matcher(contentDisposition.get());
134+
if (matcher.find())
135+
filename = matcher.group(1);
136+
}
137+
File file = null;
138+
if (filename != null) {
139+
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("swagger-gen-native");
140+
java.nio.file.Path filePath = java.nio.file.Files.createFile(tempDir.resolve(filename), null);
141+
file = filePath.toFile();
142+
tempDir.toFile().deleteOnExit(); // best effort cleanup
143+
file.deleteOnExit(); // best effort cleanup
144+
} else {
145+
file = java.nio.file.Files.createTempFile("download-", "").toFile();
146+
file.deleteOnExit(); // best effort cleanup
147+
}
148+
return file;
149+
}
150+
103151
{{#operation}}
104152
{{#vendorExtensions.x-group-parameters}}
105153
{{#hasParams}}
@@ -291,13 +339,23 @@ public class {{classname}} {
291339
);
292340
}
293341

342+
{{^isResponseFile}}{{#isResponseBinary}}
343+
Byte[] responseValue = localVarResponse.body().readAllBytes();
344+
{{/isResponseBinary}}{{/isResponseFile}}
345+
{{#isResponseFile}}
346+
// Handle file downloading.
347+
File responseValue = downloadFileFromResponse(localVarResponse);
348+
{{/isResponseFile}}
349+
{{^isResponseBinary}}{{^isResponseFile}}
294350
String responseBody = new String(localVarResponse.body().readAllBytes());
351+
{{{returnType}}} responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<{{{returnType}}}>() {});
352+
{{/isResponseFile}}{{/isResponseBinary}}
295353
localVarResponse.body().close();
296354

297355
return new ApiResponse<{{{returnType}}}>(
298356
localVarResponse.statusCode(),
299357
localVarResponse.headers().map(),
300-
responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<{{{returnType}}}>() {})
358+
responseValue
301359
);
302360
{{/returnType}}
303361
{{^returnType}}

0 commit comments

Comments
 (0)