-
Notifications
You must be signed in to change notification settings - Fork 25
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
Conversation
Rails can handle multiple content types like application/json, multipart/form-data. It parses them and builds the ActionController:: Parameters with the params that includes the path_parameters and query_parameters. During a post call where we try to get just the body parameters by reparsing the body, if we just exclude the path parameters from the params we wont have to reparse the request.body. Also the correct content-type needs to be sent to the openapi-parser.
# To enable root element in JSON for ActiveRecord objects. | ||
# ActiveSupport.on_load(:active_record) do | ||
# self.include_root_in_json = true | ||
# end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file had to be removed because it tries to wrap the JSON parameters into an object based on the controller name. In our case we are expecting the openapi-parser to do the validation for us and it doesn't like the wrapped object which is an extra parameter and it rejects it. None of our other apps catalog, approval, sources and topology are using the wrap parameters feature of rails.
lib/insights/api/common/application_controller_mixins/request_body_validation.rb
Outdated
Show resolved
Hide resolved
@@ -38,7 +36,8 @@ def validate_request | |||
request.method, | |||
request.path, | |||
api_version, | |||
body_params.as_json | |||
body_params.to_h, |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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"}
lib/insights/api/common/application_controller_mixins/request_body_validation.rb
Outdated
Show resolved
Hide resolved
Checked commits mkanoor/manageiq-api-common@65a8d76~...1cf5a0d with ruby 2.5.5, rubocop 0.69.0, haml-lint 0.20.0, and yamllint 1.10.0 |
This pull request is not mergeable. Please rebase and repush. |
Rails can handle multiple content types like application/json,
multipart/form-data. It parses them and builds the ActionController::
Parameters with the params that includes the path_parameters and
query_parameters. During a post call where we try to get just the
body parameters by reparsing the body, if we just exclude the path
parameters from the params we wont have to reparse the request.body.
Also the correct content-type needs to be sent to the openapi-parser.
Fixes #116