Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
dev-support: fix bug where multipart json body isn't automatically pa…
Browse files Browse the repository at this point in the history
…rsed

Explicitly parse the metadata parameter of the multipart form body to JSON as the rack middleware doesn't automatically parse this as it does with "Content-type: application/json" requests.
  • Loading branch information
shaunmuscat committed May 23, 2016
1 parent 55b3ef2 commit 0501351
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
19 changes: 12 additions & 7 deletions app/controllers/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def add_document_to_item
begin
collection = validate_collection(params[:collectionId], params[:api_key])
item = validate_item_exists(collection, params[:itemId])
doc_metadata = params[:metadata]
doc_metadata = parse_str_to_json(params[:metadata], 'JSON document metadata is ill-formatted')
doc_content = params[:document_content]
uploaded_file = params[:file]
uploaded_file = uploaded_file.first if uploaded_file.is_a? Array
Expand Down Expand Up @@ -543,16 +543,21 @@ def update_doc_source(doc_metadata, doc_identifier, doc_source)

# Returns a cleansed copy of params for the add item api
def cleanse_params(request_params)
if request_params[:items].is_a? String
request_params[:items] = parse_str_to_json(request_params[:items], 'JSON item metadata is ill-formatted')
request_params[:file] = [] if request_params[:file].nil?
request_params[:file] = [request_params[:file]] unless request_params[:file].is_a? Array
request_params
end

def parse_str_to_json(var, parser_error_msg)
if var.is_a? String
begin
request_params[:items] = JSON.parse(request_params[:items])
var = JSON.parse(var)
rescue JSON::ParserError
raise ResponseError.new(400), "JSON item metadata is ill-formatted"
raise ResponseError.new(400), parser_error_msg
end
end
request_params[:file] = [] if request_params[:file].nil?
request_params[:file] = [request_params[:file]] unless request_params[:file].is_a? Array
request_params
var
end

# Processes files uploaded as part of a multipart request
Expand Down
6 changes: 3 additions & 3 deletions features/api.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ Feature: Browsing via API
{"error":"The identifier document1.txt is used for multiple documents"}
"""

@api_add_item @test
@api_add_item
Scenario: Add an item with a single document file as a multipart http request
Given I make a JSON post request for the collections page with the API token for "[email protected]" with JSON params
| name | collection_metadata |
Expand All @@ -1824,7 +1824,7 @@ Feature: Browsing via API
"""

# ToDo: fix rpec post request merging hashes (document hashes) in array, see: http://stackoverflow.com/questions/18337609/rspec-request-test-merges-hashes-in-array-in-post-json-params
# @api_add_item @test
# @api_add_item
# Scenario: Add an item with many document files as a multipart http request
# Given I make a JSON post request for the collections page with the API token for "[email protected]" with JSON params
# | name | collection_metadata |
Expand Down Expand Up @@ -1852,7 +1852,7 @@ Feature: Browsing via API
# """

# ToDo: remove following workaround test once previous api_steps are fixed so that the previous test passes
@api_add_item @test
@api_add_item
Scenario: Add an item with many document files as a multipart http request
Given I make a JSON post request for the collections page with the API token for "[email protected]" with JSON params
| name | collection_metadata |
Expand Down

0 comments on commit 0501351

Please sign in to comment.