Skip to content

Commit 04907a7

Browse files
committed
[Ruby] Fix Content-Transfer-Encoding binary unpacking
1 parent 9948ed2 commit 04907a7

File tree

3 files changed

+87
-69
lines changed

3 files changed

+87
-69
lines changed

modules/openapi-generator/src/main/resources/ruby-client/api_client_faraday_partial.mustache

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -117,35 +117,41 @@
117117
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
118118
stream << chunk
119119
end
120+
120121
stream
121122
end
122123

123124
def deserialize_file(response, stream)
124-
body = response.body
125-
if @config.return_binary_data == true
126-
# return byte stream
127-
encoding = body.encoding
128-
stream.join.force_encoding(encoding)
125+
body = response.body
126+
encoding = body.encoding
127+
128+
# reconstruct content
129+
content = stream.join
130+
content = content.unpack('m').join if response.headers['Content-Transfer-Encoding'] == 'binary'
131+
content = content.force_encoding(encoding)
132+
133+
# return byte stream
134+
return content if @config.return_binary_data == true
135+
136+
# return file instead of binary data
137+
content_disposition = response.headers['Content-Disposition']
138+
if content_disposition && content_disposition =~ /filename=/i
139+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
140+
prefix = sanitize_filename(filename)
129141
else
130-
# return file instead of binary data
131-
content_disposition = response.headers['Content-Disposition']
132-
if content_disposition && content_disposition =~ /filename=/i
133-
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
134-
prefix = sanitize_filename(filename)
135-
else
136-
prefix = 'download-'
137-
end
138-
prefix = prefix + '-' unless prefix.end_with?('-')
139-
encoding = body.encoding
140-
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
141-
tempfile.write(stream.join.force_encoding(encoding))
142-
tempfile.close
143-
config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
144-
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
145-
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
146-
"explicitly with `tempfile.delete`"
147-
tempfile
142+
prefix = 'download-'
148143
end
144+
prefix = prefix + '-' unless prefix.end_with?('-')
145+
146+
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
147+
tempfile.write(content)
148+
tempfile.close
149+
150+
config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
151+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
152+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
153+
"explicitly with `tempfile.delete`"
154+
tempfile
149155
end
150156

151157
def connection(opts)

samples/client/echo_api/ruby-faraday/lib/openapi_client/api_client.rb

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -164,35 +164,41 @@ def download_file(request)
164164
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
165165
stream << chunk
166166
end
167+
167168
stream
168169
end
169170

170171
def deserialize_file(response, stream)
171-
body = response.body
172-
if @config.return_binary_data == true
173-
# return byte stream
174-
encoding = body.encoding
175-
stream.join.force_encoding(encoding)
172+
body = response.body
173+
encoding = body.encoding
174+
175+
# reconstruct content
176+
content = stream.join
177+
content = content.unpack('m').join if response.headers['Content-Transfer-Encoding'] == 'binary'
178+
content = content.force_encoding(encoding)
179+
180+
# return byte stream
181+
return content if @config.return_binary_data == true
182+
183+
# return file instead of binary data
184+
content_disposition = response.headers['Content-Disposition']
185+
if content_disposition && content_disposition =~ /filename=/i
186+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
187+
prefix = sanitize_filename(filename)
176188
else
177-
# return file instead of binary data
178-
content_disposition = response.headers['Content-Disposition']
179-
if content_disposition && content_disposition =~ /filename=/i
180-
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
181-
prefix = sanitize_filename(filename)
182-
else
183-
prefix = 'download-'
184-
end
185-
prefix = prefix + '-' unless prefix.end_with?('-')
186-
encoding = body.encoding
187-
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
188-
tempfile.write(stream.join.force_encoding(encoding))
189-
tempfile.close
190-
config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
191-
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
192-
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
193-
"explicitly with `tempfile.delete`"
194-
tempfile
189+
prefix = 'download-'
195190
end
191+
prefix = prefix + '-' unless prefix.end_with?('-')
192+
193+
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
194+
tempfile.write(content)
195+
tempfile.close
196+
197+
config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
198+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
199+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
200+
"explicitly with `tempfile.delete`"
201+
tempfile
196202
end
197203

198204
def connection(opts)

samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -164,35 +164,41 @@ def download_file(request)
164164
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
165165
stream << chunk
166166
end
167+
167168
stream
168169
end
169170

170171
def deserialize_file(response, stream)
171-
body = response.body
172-
if @config.return_binary_data == true
173-
# return byte stream
174-
encoding = body.encoding
175-
stream.join.force_encoding(encoding)
172+
body = response.body
173+
encoding = body.encoding
174+
175+
# reconstruct content
176+
content = stream.join
177+
content = content.unpack('m').join if response.headers['Content-Transfer-Encoding'] == 'binary'
178+
content = content.force_encoding(encoding)
179+
180+
# return byte stream
181+
return content if @config.return_binary_data == true
182+
183+
# return file instead of binary data
184+
content_disposition = response.headers['Content-Disposition']
185+
if content_disposition && content_disposition =~ /filename=/i
186+
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
187+
prefix = sanitize_filename(filename)
176188
else
177-
# return file instead of binary data
178-
content_disposition = response.headers['Content-Disposition']
179-
if content_disposition && content_disposition =~ /filename=/i
180-
filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
181-
prefix = sanitize_filename(filename)
182-
else
183-
prefix = 'download-'
184-
end
185-
prefix = prefix + '-' unless prefix.end_with?('-')
186-
encoding = body.encoding
187-
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
188-
tempfile.write(stream.join.force_encoding(encoding))
189-
tempfile.close
190-
config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
191-
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
192-
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
193-
"explicitly with `tempfile.delete`"
194-
tempfile
189+
prefix = 'download-'
195190
end
191+
prefix = prefix + '-' unless prefix.end_with?('-')
192+
193+
tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
194+
tempfile.write(content)
195+
tempfile.close
196+
197+
config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
198+
"with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
199+
"will be deleted automatically with GC. It's also recommended to delete the temp file "\
200+
"explicitly with `tempfile.delete`"
201+
tempfile
196202
end
197203

198204
def connection(opts)

0 commit comments

Comments
 (0)