Skip to content

Allow response_body_formatter config to format "binary" responses #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ RspecApiDocumentation.configure do |config|

# Change how the response body is formatted by default
# Is proc that will be called with the response_content_type & response_body
# by default response_content_type of `application/json` are pretty formated.
# by default, a response body that is likely to be binary is replaced with the string
# "[binary data]" regardless of the media type. Otherwise, a response_content_type of `application/json` is pretty formatted.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# "[binary data]" regardless of the media type. Otherwise, a response_content_type of `application/json` is pretty formatted.
# "[binary data]" regardless of the media type. Otherwise, a response_content_type identified as JSON is pretty formatted.

config.response_body_formatter = Proc.new { |response_content_type, response_body| response_body }

# Change the embedded style for HTML output. This file will not be processed by
Expand Down
9 changes: 3 additions & 6 deletions lib/rspec_api_documentation/client_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,9 @@ def headers(method, path, params, request_headers)

def record_response_body(response_content_type, response_body)
return nil if response_body.empty?
if response_body.encoding == Encoding::ASCII_8BIT
"[binary data]"
else
formatter = RspecApiDocumentation.configuration.response_body_formatter
return formatter.call(response_content_type, response_body)
end

formatter = RspecApiDocumentation.configuration.response_body_formatter
formatter.call(response_content_type, response_body)
end

def clean_out_uploaded_data(params, request_body)
Expand Down
4 changes: 3 additions & 1 deletion lib/rspec_api_documentation/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ def self.add_setting(name, opts = {})
# See RspecApiDocumentation::DSL::Endpoint#do_request
add_setting :response_body_formatter, default: Proc.new { |_, _|
Proc.new do |content_type, response_body|
if content_type =~ /application\/.*json/
if response_body.encoding == Encoding::ASCII_8BIT
"[binary data]"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so this is backwards compatible. 👍

elsif content_type =~ /application\/.*json/
JSON.pretty_generate(JSON.parse(response_body))
else
response_body
Expand Down