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

@@ -83,6 +83,54 @@ public class AnotherFakeApi {
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 special tags
* To test special tags and operation ID starting with number

View File

@@ -83,6 +83,54 @@ public class DefaultApi {
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;
}
/**
*
*

View File

@@ -100,6 +100,54 @@ public class FakeApi {
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;
}
/**
*
* for Java apache and Java native, test toUrlQueryString for maps with BegDecimal keys

View File

@@ -89,6 +89,54 @@ public class FakeClassnameTags123Api {
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 class name in snake case
* To test class name in snake case

View File

@@ -91,6 +91,54 @@ public class PetApi {
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;
}
/**
* Add a new pet to the store
*

View File

@@ -89,6 +89,54 @@ public class StoreApi {
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;
}
/**
* Delete purchase order by ID
* For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors

View File

@@ -90,6 +90,54 @@ public class UserApi {
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;
}
/**
* Create user
* This can only be done by the logged in user.