Skip to content

Commit 9b5fdcb

Browse files
authored
Merge pull request #310 from PavelBezpalov/master
Clean out uploaded data from arrays
2 parents 0e8da74 + c9df040 commit 9b5fdcb

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

features/upload_file.feature

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ Feature: Uploading a file
2121
[200, {}, [request.params["post"]["file"][:filename]]]
2222
end
2323
end
24-
"""
24+
"""
25+
Given a file named "nested_param_in_array.rb" with:
26+
"""
27+
require 'rack'
28+
29+
class App
30+
def self.call(env)
31+
request = Rack::Request.new(env)
32+
[200, {}, [request.params["post"]["files"][0][:filename]]]
33+
end
34+
end
35+
"""
2536

2637
Scenario: Uploading a text file with nested parameters
2738
Given a file named "file.txt" with:
@@ -40,7 +51,7 @@ Feature: Uploading a file
4051
4152
resource "FooBars" do
4253
post "/foobar" do
43-
parameter :post, "Post paramter"
54+
parameter :post, "Post parameter"
4455
4556
let(:post) do
4657
{
@@ -161,6 +172,42 @@ Feature: Uploading a file
161172

162173
When I run `rspec app_spec.rb --require ./nestedparam.rb --format RspecApiDocumentation::ApiFormatter`
163174

175+
Then the output should contain "1 example, 0 failures"
176+
And the exit status should be 0
177+
And the generated documentation should be encoded correctly
178+
179+
Scenario: Uploading an image file in params array
180+
Given I move the sample image into the workspace
181+
And a file named "app_spec.rb" with:
182+
"""
183+
require "rspec_api_documentation"
184+
require "rspec_api_documentation/dsl"
185+
require "rack/test"
186+
187+
RspecApiDocumentation.configure do |config|
188+
config.app = App
189+
end
190+
191+
resource "FooBars" do
192+
post "/foobar" do
193+
parameter :post, "Post parameter"
194+
195+
let(:post) do
196+
{
197+
id: 10,
198+
files: [ Rack::Test::UploadedFile.new("file.png", "image/png") ]
199+
}
200+
end
201+
202+
example_request "Uploading a file" do
203+
expect(response_body).to eq("file.png")
204+
end
205+
end
206+
end
207+
"""
208+
209+
When I run `rspec app_spec.rb --require ./nested_param_in_array.rb --format RspecApiDocumentation::ApiFormatter`
210+
164211
Then the output should contain "1 example, 0 failures"
165212
And the exit status should be 0
166213
And the generated documentation should be encoded correctly

lib/rspec_api_documentation/client_base.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ def record_response_body(response_content_type, response_body)
9696
end
9797

9898
def clean_out_uploaded_data(params, request_body)
99-
params.each do |_, value|
100-
if value.is_a?(Hash)
101-
if value.has_key?(:tempfile)
102-
data = value[:tempfile].read
103-
request_body = request_body.gsub(data, "[uploaded data]")
104-
else
105-
request_body = clean_out_uploaded_data(value,request_body)
106-
end
99+
params.each do |value|
100+
if [Hash, Array].member? value.class
101+
request_body = if value.respond_to?(:has_key?) && value.has_key?(:tempfile)
102+
data = value[:tempfile].read
103+
request_body.gsub(data, "[uploaded data]")
104+
else
105+
clean_out_uploaded_data(value, request_body)
106+
end
107107
end
108108
end
109109
request_body

0 commit comments

Comments
 (0)