From 6fdb632fb90cc8092099495f407f87d8eaf8bedb Mon Sep 17 00:00:00 2001 From: William Dutton Date: Fri, 4 Jul 2025 02:34:08 +1000 Subject: [PATCH] fix: #21345 Java Native Client, handle Byte[] and File response types correctly (#21346) --- .../Java/libraries/native/api.mustache | 60 ++++++++++- .../org/openapitools/client/api/AuthApi.java | 48 +++++++++ .../org/openapitools/client/api/BodyApi.java | 73 ++++++++++++- .../org/openapitools/client/api/FormApi.java | 48 +++++++++ .../openapitools/client/api/HeaderApi.java | 48 +++++++++ .../org/openapitools/client/api/PathApi.java | 48 +++++++++ .../org/openapitools/client/api/QueryApi.java | 48 +++++++++ .../client/api/AnotherFakeApi.java | 48 +++++++++ .../openapitools/client/api/DefaultApi.java | 48 +++++++++ .../org/openapitools/client/api/FakeApi.java | 48 +++++++++ .../client/api/FakeClassnameTags123Api.java | 48 +++++++++ .../org/openapitools/client/api/PetApi.java | 48 +++++++++ .../org/openapitools/client/api/StoreApi.java | 48 +++++++++ .../org/openapitools/client/api/UserApi.java | 48 +++++++++ .../org/openapitools/client/api/PetApi.java | 84 +++++++++++++-- .../org/openapitools/client/api/StoreApi.java | 66 +++++++++++- .../org/openapitools/client/api/UserApi.java | 60 ++++++++++- .../client/api/AnotherFakeApi.java | 54 +++++++++- .../openapitools/client/api/DefaultApi.java | 54 +++++++++- .../org/openapitools/client/api/FakeApi.java | 102 ++++++++++++++++-- .../client/api/FakeClassnameTags123Api.java | 54 +++++++++- .../org/openapitools/client/api/PetApi.java | 78 +++++++++++++- .../org/openapitools/client/api/StoreApi.java | 66 +++++++++++- .../org/openapitools/client/api/UserApi.java | 60 ++++++++++- 24 files changed, 1348 insertions(+), 39 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache index 75779701c40..7a8e8ec800c 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache @@ -100,6 +100,54 @@ public class {{classname}} { 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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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; + } + {{#operation}} {{#vendorExtensions.x-group-parameters}} {{#hasParams}} @@ -291,13 +339,23 @@ public class {{classname}} { ); } + {{^isResponseFile}}{{#isResponseBinary}} + Byte[] responseValue = localVarResponse.body().readAllBytes(); + {{/isResponseBinary}}{{/isResponseFile}} + {{#isResponseFile}} + // Handle file downloading. + File responseValue = downloadFileFromResponse(localVarResponse); + {{/isResponseFile}} + {{^isResponseBinary}}{{^isResponseFile}} String responseBody = new String(localVarResponse.body().readAllBytes()); + {{{returnType}}} responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<{{{returnType}}}>() {}); + {{/isResponseFile}}{{/isResponseBinary}} localVarResponse.body().close(); return new ApiResponse<{{{returnType}}}>( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference<{{{returnType}}}>() {}) + responseValue ); {{/returnType}} {{^returnType}} diff --git a/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/AuthApi.java b/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/AuthApi.java index 2c5830a3c23..5859d255084 100644 --- a/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/AuthApi.java +++ b/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/AuthApi.java @@ -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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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 diff --git a/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/BodyApi.java b/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/BodyApi.java index 23d8e651cae..d68e35e7632 100644 --- a/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/BodyApi.java +++ b/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/BodyApi.java @@ -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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + 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() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + 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() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + 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() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } diff --git a/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/FormApi.java b/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/FormApi.java index d5c55e28f84..1d58329ad9f 100644 --- a/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/FormApi.java +++ b/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/FormApi.java @@ -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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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) diff --git a/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/HeaderApi.java b/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/HeaderApi.java index b44830548e8..ef2bde8ea29 100644 --- a/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/HeaderApi.java +++ b/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/HeaderApi.java @@ -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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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) diff --git a/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/PathApi.java b/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/PathApi.java index 9757e228701..d84661bc535 100644 --- a/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/PathApi.java +++ b/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/PathApi.java @@ -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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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) diff --git a/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/QueryApi.java b/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/QueryApi.java index 05c4257eac7..dfce5c55c26 100644 --- a/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/QueryApi.java +++ b/samples/client/echo_api/java/native/src/main/java/org/openapitools/client/api/QueryApi.java @@ -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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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) diff --git a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/AnotherFakeApi.java b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/AnotherFakeApi.java index 1919d1809c2..53ee163a614 100644 --- a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/AnotherFakeApi.java +++ b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/AnotherFakeApi.java @@ -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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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 diff --git a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/DefaultApi.java b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/DefaultApi.java index 761019e6d17..fd5993cfa51 100644 --- a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/DefaultApi.java +++ b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/DefaultApi.java @@ -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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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; + } + /** * * diff --git a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeApi.java index d91496aa76d..019aadaeb0d 100644 --- a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeApi.java @@ -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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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 diff --git a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java index 50ba0a06562..b2609fec22f 100644 --- a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java +++ b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java @@ -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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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 diff --git a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/PetApi.java index 0dea62269b8..c056af30424 100644 --- a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/PetApi.java @@ -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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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 * diff --git a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/StoreApi.java b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/StoreApi.java index 9250421fa1a..742b3930778 100644 --- a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/StoreApi.java +++ b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/StoreApi.java @@ -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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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 < 1000. Anything above 1000 or nonintegers will generate API errors diff --git a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/UserApi.java index f30354c274e..cf264079ea9 100644 --- a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/UserApi.java @@ -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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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. diff --git a/samples/client/petstore/java/native-jakarta/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/native-jakarta/src/main/java/org/openapitools/client/api/PetApi.java index c1706f36801..26efc893bf8 100644 --- a/samples/client/petstore/java/native-jakarta/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/native-jakarta/src/main/java/org/openapitools/client/api/PetApi.java @@ -90,6 +90,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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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 * @@ -130,13 +178,17 @@ public class PetApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Pet responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -300,13 +352,17 @@ public class PetApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + List responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}); + localVarResponse.body().close(); return new ApiResponse>( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}) + responseValue ); } finally { } @@ -402,13 +458,17 @@ public class PetApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + List responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}); + localVarResponse.body().close(); return new ApiResponse>( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}) + responseValue ); } finally { } @@ -500,13 +560,17 @@ public class PetApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Pet responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -588,13 +652,17 @@ public class PetApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Pet responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -778,13 +846,17 @@ public class PetApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + ModelApiResponse responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } diff --git a/samples/client/petstore/java/native-jakarta/src/main/java/org/openapitools/client/api/StoreApi.java b/samples/client/petstore/java/native-jakarta/src/main/java/org/openapitools/client/api/StoreApi.java index bcf89d35d45..3773eca9c75 100644 --- a/samples/client/petstore/java/native-jakarta/src/main/java/org/openapitools/client/api/StoreApi.java +++ b/samples/client/petstore/java/native-jakarta/src/main/java/org/openapitools/client/api/StoreApi.java @@ -88,6 +88,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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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 < 1000. Anything above 1000 or nonintegers will generate API errors @@ -202,13 +250,17 @@ public class StoreApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Map responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}); + localVarResponse.body().close(); return new ApiResponse>( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}) + responseValue ); } finally { } @@ -281,13 +333,17 @@ public class StoreApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Order responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -365,13 +421,17 @@ public class StoreApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Order responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } diff --git a/samples/client/petstore/java/native-jakarta/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/native-jakarta/src/main/java/org/openapitools/client/api/UserApi.java index b147efbfb00..abbe97611c0 100644 --- a/samples/client/petstore/java/native-jakarta/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/native-jakarta/src/main/java/org/openapitools/client/api/UserApi.java @@ -89,6 +89,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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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. @@ -448,13 +496,17 @@ public class UserApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + User responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -534,13 +586,17 @@ public class UserApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + String responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } diff --git a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/AnotherFakeApi.java b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/AnotherFakeApi.java index ff0797c5a0c..3034bd208c7 100644 --- a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/AnotherFakeApi.java +++ b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/AnotherFakeApi.java @@ -82,6 +82,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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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 @@ -122,13 +170,17 @@ public class AnotherFakeApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Client responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } diff --git a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/DefaultApi.java b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/DefaultApi.java index 37eb4869077..bf84dcbeaef 100644 --- a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/DefaultApi.java +++ b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/DefaultApi.java @@ -82,6 +82,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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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; + } + /** * * @@ -120,13 +168,17 @@ public class DefaultApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + FooGetDefaultResponse responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } diff --git a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeApi.java index b5361e2fbd2..167fb9c4183 100644 --- a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeApi.java @@ -99,6 +99,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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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 @@ -137,13 +185,17 @@ public class FakeApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + FakeBigDecimalMap200Response responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -214,13 +266,17 @@ public class FakeApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + HealthCheckResult responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -293,13 +349,17 @@ public class FakeApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Boolean responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -378,13 +438,17 @@ public class FakeApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + OuterComposite responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -463,13 +527,17 @@ public class FakeApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + BigDecimal responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -548,13 +616,17 @@ public class FakeApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + String responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -626,13 +698,17 @@ public class FakeApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + List responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}); + localVarResponse.body().close(); return new ApiResponse>( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}) + responseValue ); } finally { } @@ -703,13 +779,17 @@ public class FakeApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + List responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}); + localVarResponse.body().close(); return new ApiResponse>( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}) + responseValue ); } finally { } @@ -1046,13 +1126,17 @@ public class FakeApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Client responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } diff --git a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java index 4f1e2d966f2..339a604a8db 100644 --- a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java +++ b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java @@ -88,6 +88,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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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 @@ -128,13 +176,17 @@ public class FakeClassnameTags123Api { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Client responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } diff --git a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/PetApi.java index e621ee75533..b1fd3367ebd 100644 --- a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/PetApi.java @@ -90,6 +90,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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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 * @@ -292,13 +340,17 @@ public class PetApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + List responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}); + localVarResponse.body().close(); return new ApiResponse>( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}) + responseValue ); } finally { } @@ -394,13 +446,17 @@ public class PetApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + List responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}); + localVarResponse.body().close(); return new ApiResponse>( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}) + responseValue ); } finally { } @@ -492,13 +548,17 @@ public class PetApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Pet responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -758,13 +818,17 @@ public class PetApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + ModelApiResponse responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -882,13 +946,17 @@ public class PetApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + ModelApiResponse responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } diff --git a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/StoreApi.java b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/StoreApi.java index 278213ecb8e..49664aa27f6 100644 --- a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/StoreApi.java +++ b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/StoreApi.java @@ -88,6 +88,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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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 < 1000. Anything above 1000 or nonintegers will generate API errors @@ -202,13 +250,17 @@ public class StoreApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Map responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}); + localVarResponse.body().close(); return new ApiResponse>( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference>() {}) + responseValue ); } finally { } @@ -281,13 +333,17 @@ public class StoreApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Order responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -365,13 +421,17 @@ public class StoreApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + Order responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } diff --git a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/UserApi.java index 589405a76a6..889a2e26131 100644 --- a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/UserApi.java @@ -89,6 +89,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 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); + } + } + + /** + *

Prepare the file for download from the response.

+ * + * @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 response) throws IOException { + String filename = null; + java.util.Optional 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. @@ -448,13 +496,17 @@ public class UserApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + User responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { } @@ -534,13 +586,17 @@ public class UserApi { ); } + + String responseBody = new String(localVarResponse.body().readAllBytes()); + String responseValue = responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}); + localVarResponse.body().close(); return new ApiResponse( localVarResponse.statusCode(), localVarResponse.headers().map(), - responseBody.isBlank()? null: memberVarObjectMapper.readValue(responseBody, new TypeReference() {}) + responseValue ); } finally { }