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 a136709bade5..51d958f3d583 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 5df00eeecc93..5d9163a63658 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 e36601d8f893..c4e01c93b26d 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 ApiResponse testBinaryGifWithHttpInfo() throws ApiException { ); } - 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 ApiResponse testEchoBodyAllOfPetWithHttpInfo(@javax.annotation.Nulla ); } + + 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 ApiResponse testEchoBodyPetWithHttpInfo(@javax.annotation.Nullable P ); } + + 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 ApiResponse testEchoBodyStringEnumWithHttpInfo(@javax.anno ); } + + 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 9c7c2c818c38..a400ac2fe157 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 c25c20380e28..fecc48a8f514 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 2e8463d26636..e3e6e5afd51d 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 91b865566582..225a2b23f97a 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 04f64359f776..be5a56bf3b15 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 08855ab8b247..b1c09c295957 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 de019af83829..17f2edb730d0 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 c5db4f98529e..4f5234c2a4e5 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 f181fa4064d8..1b6371d62b20 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 e23965c730f8..28aa41f63a79 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 ddb35e890f8b..3bb0707616c3 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 30d5d8831146..de369215a74b 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 ApiResponse addPetWithHttpInfo(@jakarta.annotation.Nonnull Pet pet) ); } + + 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 ApiResponse> findPetsByStatusWithHttpInfo(@jakarta.annotation.N ); } + + 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 ApiResponse> findPetsByTagsWithHttpInfo(@jakarta.annotation.Non ); } + + 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 ApiResponse getPetByIdWithHttpInfo(@jakarta.annotation.Nonnull Long ); } + + 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 ApiResponse updatePetWithHttpInfo(@jakarta.annotation.Nonnull Pet pe ); } + + 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 ApiResponse uploadFileWithHttpInfo(@jakarta.annotation. ); } + + 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 aa28060d00fb..dfa88a38b2b4 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 ApiResponse> getInventoryWithHttpInfo() throws ApiEx ); } + + 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 ApiResponse getOrderByIdWithHttpInfo(@jakarta.annotation.Nonnull L ); } + + 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 ApiResponse placeOrderWithHttpInfo(@jakarta.annotation.Nonnull Ord ); } + + 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 2b9a0b7245a0..4917aed2f47f 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 ApiResponse getUserByNameWithHttpInfo(@jakarta.annotation.Nonnull S ); } + + 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 ApiResponse loginUserWithHttpInfo(@jakarta.annotation.Nonnull Str ); } + + 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 912458c7afcc..ec5fc6519b55 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 ApiResponse call123testSpecialTagsWithHttpInfo(@javax.annotation. ); } + + 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 64e4895ab5d0..99edfec42951 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 ApiResponse fooGetWithHttpInfo() throws ApiExcepti ); } + + 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 b0938e3a12c0..deeb7ad5f4e3 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 ApiResponse fakeBigDecimalMapWithHttpInfo() ); } + + 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 ApiResponse fakeHealthGetWithHttpInfo() throws ApiExce ); } + + 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 ApiResponse fakeOuterBooleanSerializeWithHttpInfo(@javax.annotat ); } + + 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 ApiResponse fakeOuterCompositeSerializeWithHttpInfo(@java ); } + + 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 ApiResponse fakeOuterNumberSerializeWithHttpInfo(@javax.annot ); } + + 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 ApiResponse fakeOuterStringSerializeWithHttpInfo(@javax.annotatio ); } + + 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 ApiResponse> getApplicationJsonUtf8WithHttpInfo() throws ); } + + 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 ApiResponse> getArrayOfEnumsWithHttpInfo() throws ApiExce ); } + + 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 ApiResponse testClientModelWithHttpInfo(@javax.annotation.Nonnull ); } + + 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 be6dfb77cee2..e5bcfb664a7f 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 ApiResponse testClassnameWithHttpInfo(@javax.annotation.Nonnull C ); } + + 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 09a44a886f6e..428992bc7d6d 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 ApiResponse> findPetsByStatusWithHttpInfo(@javax.annotation.Non ); } + + 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 ApiResponse> findPetsByTagsWithHttpInfo(@javax.annotation.Nonnu ); } + + 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 ApiResponse getPetByIdWithHttpInfo(@javax.annotation.Nonnull Long pe ); } + + 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 ApiResponse uploadFileWithHttpInfo(@javax.annotation.No ); } + + 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 { } @@ -880,13 +944,17 @@ public ApiResponse uploadFileWithRequiredFileWithHttpInfo(@jav ); } + + 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 8b4125eac2bc..0bae081628bd 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 ApiResponse> getInventoryWithHttpInfo() throws ApiEx ); } + + 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 ApiResponse getOrderByIdWithHttpInfo(@javax.annotation.Nonnull Lon ); } + + 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 ApiResponse placeOrderWithHttpInfo(@javax.annotation.Nonnull Order ); } + + 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 bf3f11fdc0ad..d7a8fc191f68 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 @@ private String formatExceptionMessage(String operationId, int statusCode, String 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 ApiResponse getUserByNameWithHttpInfo(@javax.annotation.Nonnull Str ); } + + 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 ApiResponse loginUserWithHttpInfo(@javax.annotation.Nonnull Strin ); } + + 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 { }