Skip to content
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

Allow for other content types other than application/json #134

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ def self.included(other)

def body_params
@body_params ||= begin
raw_body = request.body.read
parsed_body = raw_body.blank? ? {} : JSON.parse(raw_body)
ActionController::Parameters.new(parsed_body).permit!
rescue JSON::ParserError
raise Insights::API::Common::ApplicationControllerMixins::RequestBodyValidation::BodyParseError, "Failed to parse request body, expected JSON"
hash = params.permit!.to_h
bdunne marked this conversation as resolved.
Show resolved Hide resolved
request.path_parameters.keys.each { |key| hash.delete(key) }
ActionController::Parameters.new(hash).permit!
bdunne marked this conversation as resolved.
Show resolved Hide resolved
end
end

Expand All @@ -38,7 +36,8 @@ def validate_request
request.method,
request.path,
api_version,
body_params.as_json
body_params.to_h,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't remember the specifics, but I think there is a difference between as_json and to_h.

Copy link
Contributor Author

@mkanoor mkanoor Nov 14, 2019

Choose a reason for hiding this comment

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

@bdunne
The to_h returns a Hash similar to as_json but it changes the attribute values

For multipart/form-data we have a UploadedFile object that gets lost when we do the as_json

(byebug) j.to_h

{"content"=>#<ActionDispatch::Http::UploadedFile:0x00007fed85a2b438 @tempfile=#<Tempfile:/var/folders/kr/3m9v5qk557970qwndrlkdxyr0000gn/T/RackMultipart20191114-11952-19uzl2c.svg>, @original_filename="ocp_logo.svg", @content_type="image/svg+xml", @headers="Content-Disposition: form-data; name=\"content\"; filename=\"ocp_logo.svg\"\r\nContent-Type: image/svg+xml\r\nContent-Length: 21247\r\n">, "source_id"=>"27", "source_ref"=>"icon_ref", "portfolio_item_id"=>"2755"}

(byebug) j.as_json

{"content"=>{"tempfile"=>"#<File:0x00007fed7f53f1a0>", "original_filename"=>"ocp_logo.svg", "content_type"=>"image/svg+xml", "headers"=>"Content-Disposition: form-data; name=\"content\"; filename=\"ocp_logo.svg\"\r\nContent-Type: image/svg+xml\r\nContent-Length: 21247\r\n"}, "source_id"=>"27", "source_ref"=>"icon_ref", "portfolio_item_id"=>"2755"}

request.content_type
)
end
end
Expand Down
14 changes: 0 additions & 14 deletions spec/dummy/config/initializers/wrap_parameters.rb

This file was deleted.

10 changes: 0 additions & 10 deletions spec/requests/request_body_validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@
before { stub_const("ENV", "BYPASS_TENANCY" => true) }
let(:default_params) { { "authtype" => "openshift" } }

context "when there is an invalid body" do
let(:default_as) { :text }

it "returns a 400" do
post("/api/v1.0/authentications", :headers => {"CONTENT_TYPE" => "application/text"}, :params => "{")

expect(response.status).to eq(400)
end
end

it "unpermitted key" do
post("/api/v1.0/authentications", :headers => headers, :params => default_params.merge("garbage" => "abc"))

Expand Down