From f1df6acdee67eff72833e7cbacab5b624b0092e7 Mon Sep 17 00:00:00 2001 From: Tomohiro Suwa Date: Fri, 30 Oct 2020 21:36:14 +0900 Subject: [PATCH 1/4] Fix download_file --- .../resources/ruby-client/api_client.mustache | 3 +- .../api_client_faraday_partial.mustache | 29 ------------------- 2 files changed, 2 insertions(+), 30 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/ruby-client/api_client.mustache b/modules/openapi-generator/src/main/resources/ruby-client/api_client.mustache index fd043cd31f90..f4f0d1599036 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/api_client.mustache @@ -71,7 +71,8 @@ module {{moduleName}} {{/isFaraday}} {{#isFaraday}} if return_type == 'File' - @tempfile.write(@stream) + @tempfile = Tempfile.open('download-', @config.temp_folder_path, encoding: body.encoding) + @tempfile.write(@stream.join.force_encoding(body.encoding)) @tempfile.close @config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\ diff --git a/modules/openapi-generator/src/main/resources/ruby-client/api_client_faraday_partial.mustache b/modules/openapi-generator/src/main/resources/ruby-client/api_client_faraday_partial.mustache index e4fce575857b..96e1e1142ad7 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/api_client_faraday_partial.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/api_client_faraday_partial.mustache @@ -128,40 +128,11 @@ data end - # Save response body into a file in (the defined) temporary folder, using the filename - # from the "Content-Disposition" header if provided, otherwise a random filename. - # The response body is written to the file in chunks in order to handle files which - # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby - # process can use. - # - # @see Configuration#temp_folder_path def download_file(request) - tempfile = nil - encoding = nil - request.headers do |response| - content_disposition = response.headers['Content-Disposition'] - if content_disposition && content_disposition =~ /filename=/i - filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1] - prefix = sanitize_filename(filename) - else - prefix = 'download-' - end - prefix = prefix + '-' unless prefix.end_with?('-') - encoding = response.body.encoding - tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) - @tempfile = tempfile - end - - if tempfile.nil? - tempfile = Tempfile.open('download-', @config.temp_folder_path) - @tempfile = tempfile - end - @stream = [] # handle streaming Responses request.options.on_data = Proc.new do |chunk, overall_received_bytes| @stream << chunk end - end From d5c7e150323a68aee8e7cd72c8f5ccb9a20d8ed4 Mon Sep 17 00:00:00 2001 From: Tomohiro Suwa Date: Fri, 30 Oct 2020 21:38:14 +0900 Subject: [PATCH 2/4] Generate samples with f1df6acdee6 --- .../ruby-faraday/lib/petstore/api_client.rb | 32 ++----------------- 1 file changed, 2 insertions(+), 30 deletions(-) diff --git a/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb b/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb index 621712264389..5e5f694e3668 100644 --- a/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb +++ b/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb @@ -172,42 +172,13 @@ def build_request_body(header_params, form_params, body) data end - # Save response body into a file in (the defined) temporary folder, using the filename - # from the "Content-Disposition" header if provided, otherwise a random filename. - # The response body is written to the file in chunks in order to handle files which - # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby - # process can use. - # - # @see Configuration#temp_folder_path def download_file(request) - tempfile = nil - encoding = nil - request.headers do |response| - content_disposition = response.headers['Content-Disposition'] - if content_disposition && content_disposition =~ /filename=/i - filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1] - prefix = sanitize_filename(filename) - else - prefix = 'download-' - end - prefix = prefix + '-' unless prefix.end_with?('-') - encoding = response.body.encoding - tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) - @tempfile = tempfile - end - - if tempfile.nil? - tempfile = Tempfile.open('download-', @config.temp_folder_path) - @tempfile = tempfile - end - @stream = [] # handle streaming Responses request.options.on_data = Proc.new do |chunk, overall_received_bytes| @stream << chunk end - end # Check if the given MIME is a JSON MIME. @@ -232,7 +203,8 @@ def deserialize(response, return_type) # handle file downloading - return the File instance processed in request callbacks # note that response body is empty when the file is written in chunks in request on_body callback if return_type == 'File' - @tempfile.write(@stream) + @tempfile = Tempfile.open('download-', @config.temp_folder_path, encoding: body.encoding) + @tempfile.write(@stream.join.force_encoding(body.encoding)) @tempfile.close @config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\ From 3e3ea88b0dec7763c861e9623f88a9d1fa9034c8 Mon Sep 17 00:00:00 2001 From: Tomohiro Suwa Date: Fri, 30 Oct 2020 21:47:58 +0900 Subject: [PATCH 3/4] fixup! Fix download_file --- .../main/resources/ruby-client/api_client.mustache | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/ruby-client/api_client.mustache b/modules/openapi-generator/src/main/resources/ruby-client/api_client.mustache index f4f0d1599036..3400b97da4ec 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/api_client.mustache @@ -71,8 +71,17 @@ module {{moduleName}} {{/isFaraday}} {{#isFaraday}} if return_type == 'File' - @tempfile = Tempfile.open('download-', @config.temp_folder_path, encoding: body.encoding) - @tempfile.write(@stream.join.force_encoding(body.encoding)) + content_disposition = response.headers['Content-Disposition'] + if content_disposition && content_disposition =~ /filename=/i + filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1] + prefix = sanitize_filename(filename) + else + prefix = 'download-' + end + prefix = prefix + '-' unless prefix.end_with?('-') + encoding = body.encoding + @tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) + @tempfile.write(@stream.join.force_encoding(encoding)) @tempfile.close @config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\ From 1b4c9df567c71bda7fb8f5359533166a5ccc9e68 Mon Sep 17 00:00:00 2001 From: Tomohiro Suwa Date: Fri, 30 Oct 2020 21:57:55 +0900 Subject: [PATCH 4/4] Generate samples with 3e3ea88b0dec7763c8 --- .../ruby-faraday/lib/petstore/api_client.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb b/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb index 5e5f694e3668..fee10b38d89f 100644 --- a/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb +++ b/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb @@ -203,8 +203,17 @@ def deserialize(response, return_type) # handle file downloading - return the File instance processed in request callbacks # note that response body is empty when the file is written in chunks in request on_body callback if return_type == 'File' - @tempfile = Tempfile.open('download-', @config.temp_folder_path, encoding: body.encoding) - @tempfile.write(@stream.join.force_encoding(body.encoding)) + content_disposition = response.headers['Content-Disposition'] + if content_disposition && content_disposition =~ /filename=/i + filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1] + prefix = sanitize_filename(filename) + else + prefix = 'download-' + end + prefix = prefix + '-' unless prefix.end_with?('-') + encoding = body.encoding + @tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) + @tempfile.write(@stream.join.force_encoding(encoding)) @tempfile.close @config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\