From b5687ce2ce2cd674ea5e307bb25e3a37e66a415c Mon Sep 17 00:00:00 2001 From: Yohei Kitamura <3087402+yoheyk@users.noreply.github.com> Date: Wed, 27 Mar 2024 11:57:47 -0400 Subject: [PATCH] [ruby] Use Marcel to detect content-type in form-data file upload using faraday --- CI/circle_parallel.sh | 4 +--- .../src/main/resources/ruby-client/api_client.mustache | 2 +- .../ruby-client/api_client_faraday_partial.mustache | 3 +-- .../src/main/resources/ruby-client/gemspec.mustache | 2 +- .../echo_api/ruby-faraday/lib/openapi_client/api_client.rb | 5 ++--- samples/client/echo_api/ruby-faraday/openapi_client.gemspec | 2 +- .../client/petstore/ruby-faraday/lib/petstore/api_client.rb | 5 ++--- samples/client/petstore/ruby-faraday/petstore.gemspec | 2 +- 8 files changed, 10 insertions(+), 15 deletions(-) diff --git a/CI/circle_parallel.sh b/CI/circle_parallel.sh index 9be0f79cf9cf..837eaa0b5221 100755 --- a/CI/circle_parallel.sh +++ b/CI/circle_parallel.sh @@ -13,7 +13,7 @@ if [ "$NODE_INDEX" = "1" ]; then echo "Running node $NODE_INDEX ..." java -version - sudo apt-get -y install cpanminus libmagic-dev + sudo apt-get -y install cpanminus (cd samples/client/petstore/perl && /bin/bash ./test.bash) (cd samples/client/petstore/ruby && mvn integration-test) @@ -112,5 +112,3 @@ else (cd samples/client/petstore/java/apache-httpclient && mvn integration-test) fi - - 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 19eb7c5cea92..cb9205691b6d 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 @@ -13,7 +13,7 @@ require 'typhoeus' {{#isFaraday}} require 'faraday' require 'faraday/multipart' if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0') -require 'filemagic' +require 'marcel' {{/isFaraday}} {{#isHttpx}} require 'httpx' 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 78452bff0d1d..1a85fc69c3de 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 @@ -94,8 +94,7 @@ form_params.each do |key, value| case value when ::File, ::Tempfile - filemagic = FileMagic.new(FileMagic::MAGIC_MIME) - data[key] = Faraday::FilePart.new(value.path, filemagic.file(value.path, true)) + data[key] = Faraday::FilePart.new(value.path, Marcel::MimeType.for(Pathname.new(value.path))) when ::Array, nil # let Faraday handle Array and nil parameters data[key] = value diff --git a/modules/openapi-generator/src/main/resources/ruby-client/gemspec.mustache b/modules/openapi-generator/src/main/resources/ruby-client/gemspec.mustache index b17dce7dc537..ea1cf2f77b8e 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/gemspec.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/gemspec.mustache @@ -23,7 +23,7 @@ Gem::Specification.new do |s| {{#isFaraday}} s.add_runtime_dependency 'faraday', '>= 1.0.1', '< 3.0' s.add_runtime_dependency 'faraday-multipart' - s.add_runtime_dependency 'ruby-filemagic' + s.add_runtime_dependency 'marcel' {{/isFaraday}} {{#isTyphoeus}} s.add_runtime_dependency 'typhoeus', '~> 1.0', '>= 1.0.1' diff --git a/samples/client/echo_api/ruby-faraday/lib/openapi_client/api_client.rb b/samples/client/echo_api/ruby-faraday/lib/openapi_client/api_client.rb index 3161ea8ea4ab..21e7f6a6d4ea 100644 --- a/samples/client/echo_api/ruby-faraday/lib/openapi_client/api_client.rb +++ b/samples/client/echo_api/ruby-faraday/lib/openapi_client/api_client.rb @@ -17,7 +17,7 @@ require 'time' require 'faraday' require 'faraday/multipart' if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0') -require 'filemagic' +require 'marcel' module OpenapiClient @@ -141,8 +141,7 @@ def build_request_body(header_params, form_params, body) form_params.each do |key, value| case value when ::File, ::Tempfile - filemagic = FileMagic.new(FileMagic::MAGIC_MIME) - data[key] = Faraday::FilePart.new(value.path, filemagic.file(value.path, true)) + data[key] = Faraday::FilePart.new(value.path, Marcel::MimeType.for(Pathname.new(value.path))) when ::Array, nil # let Faraday handle Array and nil parameters data[key] = value diff --git a/samples/client/echo_api/ruby-faraday/openapi_client.gemspec b/samples/client/echo_api/ruby-faraday/openapi_client.gemspec index 1c506aed47f3..66453ad9de0a 100644 --- a/samples/client/echo_api/ruby-faraday/openapi_client.gemspec +++ b/samples/client/echo_api/ruby-faraday/openapi_client.gemspec @@ -30,7 +30,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'faraday', '>= 1.0.1', '< 3.0' s.add_runtime_dependency 'faraday-multipart' - s.add_runtime_dependency 'ruby-filemagic' + s.add_runtime_dependency 'marcel' s.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0' 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 773f0ec11e5b..0cadc9b43a54 100644 --- a/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb +++ b/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb @@ -17,7 +17,7 @@ require 'time' require 'faraday' require 'faraday/multipart' if Gem::Version.new(Faraday::VERSION) >= Gem::Version.new('2.0') -require 'filemagic' +require 'marcel' module Petstore @@ -141,8 +141,7 @@ def build_request_body(header_params, form_params, body) form_params.each do |key, value| case value when ::File, ::Tempfile - filemagic = FileMagic.new(FileMagic::MAGIC_MIME) - data[key] = Faraday::FilePart.new(value.path, filemagic.file(value.path, true)) + data[key] = Faraday::FilePart.new(value.path, Marcel::MimeType.for(Pathname.new(value.path))) when ::Array, nil # let Faraday handle Array and nil parameters data[key] = value diff --git a/samples/client/petstore/ruby-faraday/petstore.gemspec b/samples/client/petstore/ruby-faraday/petstore.gemspec index 83dfaff7aefb..89502c86af7e 100644 --- a/samples/client/petstore/ruby-faraday/petstore.gemspec +++ b/samples/client/petstore/ruby-faraday/petstore.gemspec @@ -30,7 +30,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'faraday', '>= 1.0.1', '< 3.0' s.add_runtime_dependency 'faraday-multipart' - s.add_runtime_dependency 'ruby-filemagic' + s.add_runtime_dependency 'marcel' s.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0'