Skip to content

Commit 7b4cef7

Browse files
committed
Fix binary data response
zipmark#458
1 parent 81e5c56 commit 7b4cef7

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ RspecApiDocumentation.configure do |config|
250250

251251
# Change how the response body is formatted by default
252252
# Is proc that will be called with the response_content_type & response_body
253-
# by default response_content_type of `application/json` are pretty formated.
253+
# by default, a response body that is likely to be binary is replaced with the string
254+
# "[binary data]" and response_content_type of `application/json` are pretty formated.
254255
config.response_body_formatter = Proc.new { |response_content_type, response_body| response_body }
255256

256257
# Change the embedded style for HTML output. This file will not be processed by

lib/rspec_api_documentation/client_base.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,9 @@ def headers(method, path, params, request_headers)
8787

8888
def record_response_body(response_content_type, response_body)
8989
return nil if response_body.empty?
90-
if response_body.encoding == Encoding::ASCII_8BIT
91-
"[binary data]"
92-
else
93-
formatter = RspecApiDocumentation.configuration.response_body_formatter
94-
return formatter.call(response_content_type, response_body)
95-
end
90+
91+
formatter = RspecApiDocumentation.configuration.response_body_formatter
92+
formatter.call(response_content_type, response_body)
9693
end
9794

9895
def clean_out_uploaded_data(params, request_body)

lib/rspec_api_documentation/configuration.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ def self.add_setting(name, opts = {})
118118
# See RspecApiDocumentation::DSL::Endpoint#do_request
119119
add_setting :response_body_formatter, default: Proc.new { |_, _|
120120
Proc.new do |content_type, response_body|
121-
if content_type =~ /application\/.*json/
121+
if response_body.encoding == Encoding::ASCII_8BIT
122+
"[binary data]"
123+
elsif content_type =~ /application\/.*json/
122124
JSON.pretty_generate(JSON.parse(response_body))
123125
else
124126
response_body

0 commit comments

Comments
 (0)