fix: #21345 Java Native Client, handle Byte[] and File response types correctly (#21346)

This commit is contained in:
William Dutton
2025-07-04 02:34:08 +10:00
committed by GitHub
parent c7542dea3e
commit 6fdb632fb9
24 changed files with 1348 additions and 39 deletions

View File

@@ -81,6 +81,54 @@ public class AuthApi {
return operationId + " call failed with: " + statusCode + " - " + body;
}
/**
* Download file from the given response.
*
* @param response Response
* @return File
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(HttpResponse<InputStream> response) throws ApiException {
try {
File file = prepareDownloadFile(response);
java.nio.file.Files.copy(response.body(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}
/**
* <p>Prepare the file for download from the response.</p>
*
* @param response a {@link java.net.http.HttpResponse} object.
* @return a {@link java.io.File} object.
* @throws java.io.IOException if any.
*/
private File prepareDownloadFile(HttpResponse<InputStream> response) throws IOException {
String filename = null;
java.util.Optional<String> contentDisposition = response.headers().firstValue("Content-Disposition");
if (contentDisposition.isPresent() && !"".equals(contentDisposition.get())) {
// Get filename from the Content-Disposition header.
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
java.util.regex.Matcher matcher = pattern.matcher(contentDisposition.get());
if (matcher.find())
filename = matcher.group(1);
}
File file = null;
if (filename != null) {
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("swagger-gen-native");
java.nio.file.Path filePath = java.nio.file.Files.createFile(tempDir.resolve(filename));
file = filePath.toFile();
tempDir.toFile().deleteOnExit(); // best effort cleanup
file.deleteOnExit(); // best effort cleanup
} else {
file = java.nio.file.Files.createTempFile("download-", "").toFile();
file.deleteOnExit(); // best effort cleanup
}
return file;
}
/**
* To test HTTP basic authentication
* To test HTTP basic authentication

View File

@@ -91,6 +91,54 @@ public class BodyApi {
return operationId + " call failed with: " + statusCode + " - " + body;
}
/**
* Download file from the given response.
*
* @param response Response
* @return File
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(HttpResponse<InputStream> response) throws ApiException {
try {
File file = prepareDownloadFile(response);
java.nio.file.Files.copy(response.body(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}
/**
* <p>Prepare the file for download from the response.</p>
*
* @param response a {@link java.net.http.HttpResponse} object.
* @return a {@link java.io.File} object.
* @throws java.io.IOException if any.
*/
private File prepareDownloadFile(HttpResponse<InputStream> response) throws IOException {
String filename = null;
java.util.Optional<String> contentDisposition = response.headers().firstValue("Content-Disposition");
if (contentDisposition.isPresent() && !"".equals(contentDisposition.get())) {
// Get filename from the Content-Disposition header.
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
java.util.regex.Matcher matcher = pattern.matcher(contentDisposition.get());
if (matcher.find())
filename = matcher.group(1);
}
File file = null;
if (filename != null) {
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("swagger-gen-native");
java.nio.file.Path filePath = java.nio.file.Files.createFile(tempDir.resolve(filename));
file = filePath.toFile();
tempDir.toFile().deleteOnExit(); // best effort cleanup
file.deleteOnExit(); // best effort cleanup
} else {
file = java.nio.file.Files.createTempFile("download-", "").toFile();
file.deleteOnExit(); // best effort cleanup
}
return file;
}
/**
* Test binary (gif) response body
* Test binary (gif) response body
@@ -129,13 +177,16 @@ public class BodyApi {
);
}
String responseBody = new String(localVarResponse.body().readAllBytes());
// Handle file downloading.
File responseValue = downloadFileFromResponse(localVarResponse);
localVarResponse.body().close();
return new ApiResponse<File>(
localVarResponse.statusCode(),
localVarResponse.headers().map(),
responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<File>() {})
responseValue
);
} finally {
}
@@ -514,13 +565,17 @@ public class BodyApi {
);
}
String responseBody = new String(localVarResponse.body().readAllBytes());
Pet responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<Pet>() {});
localVarResponse.body().close();
return new ApiResponse<Pet>(
localVarResponse.statusCode(),
localVarResponse.headers().map(),
responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<Pet>() {})
responseValue
);
} finally {
}
@@ -681,13 +736,17 @@ public class BodyApi {
);
}
String responseBody = new String(localVarResponse.body().readAllBytes());
Pet responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<Pet>() {});
localVarResponse.body().close();
return new ApiResponse<Pet>(
localVarResponse.statusCode(),
localVarResponse.headers().map(),
responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<Pet>() {})
responseValue
);
} finally {
}
@@ -848,13 +907,17 @@ public class BodyApi {
);
}
String responseBody = new String(localVarResponse.body().readAllBytes());
StringEnumRef responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<StringEnumRef>() {});
localVarResponse.body().close();
return new ApiResponse<StringEnumRef>(
localVarResponse.statusCode(),
localVarResponse.headers().map(),
responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<StringEnumRef>() {})
responseValue
);
} finally {
}

View File

@@ -88,6 +88,54 @@ public class FormApi {
return operationId + " call failed with: " + statusCode + " - " + body;
}
/**
* Download file from the given response.
*
* @param response Response
* @return File
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(HttpResponse<InputStream> response) throws ApiException {
try {
File file = prepareDownloadFile(response);
java.nio.file.Files.copy(response.body(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}
/**
* <p>Prepare the file for download from the response.</p>
*
* @param response a {@link java.net.http.HttpResponse} object.
* @return a {@link java.io.File} object.
* @throws java.io.IOException if any.
*/
private File prepareDownloadFile(HttpResponse<InputStream> response) throws IOException {
String filename = null;
java.util.Optional<String> contentDisposition = response.headers().firstValue("Content-Disposition");
if (contentDisposition.isPresent() && !"".equals(contentDisposition.get())) {
// Get filename from the Content-Disposition header.
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
java.util.regex.Matcher matcher = pattern.matcher(contentDisposition.get());
if (matcher.find())
filename = matcher.group(1);
}
File file = null;
if (filename != null) {
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("swagger-gen-native");
java.nio.file.Path filePath = java.nio.file.Files.createFile(tempDir.resolve(filename));
file = filePath.toFile();
tempDir.toFile().deleteOnExit(); // best effort cleanup
file.deleteOnExit(); // best effort cleanup
} else {
file = java.nio.file.Files.createTempFile("download-", "").toFile();
file.deleteOnExit(); // best effort cleanup
}
return file;
}
/**
* Test form parameter(s)
* Test form parameter(s)

View File

@@ -88,6 +88,54 @@ public class HeaderApi {
return operationId + " call failed with: " + statusCode + " - " + body;
}
/**
* Download file from the given response.
*
* @param response Response
* @return File
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(HttpResponse<InputStream> response) throws ApiException {
try {
File file = prepareDownloadFile(response);
java.nio.file.Files.copy(response.body(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}
/**
* <p>Prepare the file for download from the response.</p>
*
* @param response a {@link java.net.http.HttpResponse} object.
* @return a {@link java.io.File} object.
* @throws java.io.IOException if any.
*/
private File prepareDownloadFile(HttpResponse<InputStream> response) throws IOException {
String filename = null;
java.util.Optional<String> contentDisposition = response.headers().firstValue("Content-Disposition");
if (contentDisposition.isPresent() && !"".equals(contentDisposition.get())) {
// Get filename from the Content-Disposition header.
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
java.util.regex.Matcher matcher = pattern.matcher(contentDisposition.get());
if (matcher.find())
filename = matcher.group(1);
}
File file = null;
if (filename != null) {
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("swagger-gen-native");
java.nio.file.Path filePath = java.nio.file.Files.createFile(tempDir.resolve(filename));
file = filePath.toFile();
tempDir.toFile().deleteOnExit(); // best effort cleanup
file.deleteOnExit(); // best effort cleanup
} else {
file = java.nio.file.Files.createTempFile("download-", "").toFile();
file.deleteOnExit(); // best effort cleanup
}
return file;
}
/**
* Test header parameter(s)
* Test header parameter(s)

View File

@@ -88,6 +88,54 @@ public class PathApi {
return operationId + " call failed with: " + statusCode + " - " + body;
}
/**
* Download file from the given response.
*
* @param response Response
* @return File
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(HttpResponse<InputStream> response) throws ApiException {
try {
File file = prepareDownloadFile(response);
java.nio.file.Files.copy(response.body(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}
/**
* <p>Prepare the file for download from the response.</p>
*
* @param response a {@link java.net.http.HttpResponse} object.
* @return a {@link java.io.File} object.
* @throws java.io.IOException if any.
*/
private File prepareDownloadFile(HttpResponse<InputStream> response) throws IOException {
String filename = null;
java.util.Optional<String> contentDisposition = response.headers().firstValue("Content-Disposition");
if (contentDisposition.isPresent() && !"".equals(contentDisposition.get())) {
// Get filename from the Content-Disposition header.
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
java.util.regex.Matcher matcher = pattern.matcher(contentDisposition.get());
if (matcher.find())
filename = matcher.group(1);
}
File file = null;
if (filename != null) {
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("swagger-gen-native");
java.nio.file.Path filePath = java.nio.file.Files.createFile(tempDir.resolve(filename));
file = filePath.toFile();
tempDir.toFile().deleteOnExit(); // best effort cleanup
file.deleteOnExit(); // best effort cleanup
} else {
file = java.nio.file.Files.createTempFile("download-", "").toFile();
file.deleteOnExit(); // best effort cleanup
}
return file;
}
/**
* Test path parameter(s)
* Test path parameter(s)

View File

@@ -94,6 +94,54 @@ public class QueryApi {
return operationId + " call failed with: " + statusCode + " - " + body;
}
/**
* Download file from the given response.
*
* @param response Response
* @return File
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(HttpResponse<InputStream> response) throws ApiException {
try {
File file = prepareDownloadFile(response);
java.nio.file.Files.copy(response.body(), file.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}
/**
* <p>Prepare the file for download from the response.</p>
*
* @param response a {@link java.net.http.HttpResponse} object.
* @return a {@link java.io.File} object.
* @throws java.io.IOException if any.
*/
private File prepareDownloadFile(HttpResponse<InputStream> response) throws IOException {
String filename = null;
java.util.Optional<String> contentDisposition = response.headers().firstValue("Content-Disposition");
if (contentDisposition.isPresent() && !"".equals(contentDisposition.get())) {
// Get filename from the Content-Disposition header.
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
java.util.regex.Matcher matcher = pattern.matcher(contentDisposition.get());
if (matcher.find())
filename = matcher.group(1);
}
File file = null;
if (filename != null) {
java.nio.file.Path tempDir = java.nio.file.Files.createTempDirectory("swagger-gen-native");
java.nio.file.Path filePath = java.nio.file.Files.createFile(tempDir.resolve(filename));
file = filePath.toFile();
tempDir.toFile().deleteOnExit(); // best effort cleanup
file.deleteOnExit(); // best effort cleanup
} else {
file = java.nio.file.Files.createTempFile("download-", "").toFile();
file.deleteOnExit(); // best effort cleanup
}
return file;
}
/**
* Test query parameter(s)
* Test query parameter(s)