Skip to content

Commit 3a1d1ee

Browse files
committed
Merge pull request #244 from galiat/rename-post-body-formatter
rename config post_body_formatter to be request_body_formatter
2 parents 878ee79 + bd38fcb commit 3a1d1ee

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ RspecApiDocumentation.configure do |config|
138138

139139
# Change how the post body is formatted by default, you can still override by `raw_post`
140140
# Can be :json, :xml, or a proc that will be passed the params
141-
config.post_body_formatter = Proc.new { |params| params }
141+
config.request_body_formatter = Proc.new { |params| params }
142142

143143
# Change how the response body is formatted by default
144144
# Is proc that will be called with the response_content_type & response_body

lib/rspec_api_documentation/configuration.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,21 @@ def self.add_setting(name, opts = {})
8080
add_setting :response_headers_to_include, :default => nil
8181
add_setting :html_embedded_css_file, :default => nil
8282

83+
# renamed to request_body_formatter. here for backwards compatibility
84+
add_setting :post_body_formatter, :default => nil
85+
8386
# Change how the post body is formatted by default, you can still override by `raw_post`
8487
# Can be :json, :xml, or a proc that will be passed the params
8588
#
8689
# RspecApiDocumentation.configure do |config|
87-
# config.post_body_formatter = Proc.new do |params|
90+
# config.request_body_formatter = Proc.new do |params|
8891
# # convert to whatever you want
8992
# params.to_s
9093
# end
9194
# end
9295
#
9396
# See RspecApiDocumentation::DSL::Endpoint#do_request
94-
add_setting :post_body_formatter, :default => Proc.new { |_| Proc.new { |params| params } }
97+
add_setting :request_body_formatter, :default => Proc.new { |_| RspecApiDocumentation.configuration.post_body_formatter || Proc.new { |params| params } }
9598

9699
# Change how the response body is formatted
97100
# Can be a proc that will be passed the response body

lib/rspec_api_documentation/dsl/endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def do_request(extra_params = {})
4141
if respond_to?(:raw_post)
4242
params_or_body = raw_post
4343
else
44-
formatter = RspecApiDocumentation.configuration.post_body_formatter
44+
formatter = RspecApiDocumentation.configuration.request_body_formatter
4545
case formatter
4646
when :json
4747
params_or_body = params.empty? ? nil : params.to_json

spec/configuration_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
its(:html_embedded_css_file) { should be_nil }
6060

6161
specify "post body formatter" do
62-
expect(configuration.post_body_formatter.call({ :page => 1})).to eq({ :page => 1 })
62+
expect(configuration.request_body_formatter.call({ :page => 1})).to eq({ :page => 1 })
6363
end
6464
end
6565

spec/dsl_spec.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@
515515
get "/orders" do
516516
specify "formatting by json without parameters" do
517517
RspecApiDocumentation.configure do |config|
518-
config.post_body_formatter = :json
518+
config.request_body_formatter = :json
519519
end
520520

521521
expect(client).to receive(method).with(path, nil, nil)
@@ -530,6 +530,16 @@
530530
let(:page) { 1 }
531531

532532
specify "formatting by json" do
533+
RspecApiDocumentation.configure do |config|
534+
config.request_body_formatter = :json
535+
end
536+
537+
expect(client).to receive(method).with(path, { :page => 1 }.to_json , nil)
538+
539+
do_request
540+
end
541+
542+
specify "formatting by json via legacy config" do
533543
RspecApiDocumentation.configure do |config|
534544
config.post_body_formatter = :json
535545
end
@@ -541,7 +551,7 @@
541551

542552
specify "formatting by xml" do
543553
RspecApiDocumentation.configure do |config|
544-
config.post_body_formatter = :xml
554+
config.request_body_formatter = :xml
545555
end
546556

547557
expect(client).to receive(method).with(path, { :page => 1 }.to_xml , nil)
@@ -551,7 +561,7 @@
551561

552562
specify "formatting by proc" do
553563
RspecApiDocumentation.configure do |config|
554-
config.post_body_formatter = Proc.new do |params|
564+
config.request_body_formatter = Proc.new do |params|
555565
{ :from => "a proc" }.to_json
556566
end
557567
end

0 commit comments

Comments
 (0)