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

Conversation

mkanoor
Copy link
Contributor

@mkanoor mkanoor commented Nov 13, 2019

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

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
Copy link
Contributor Author

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.

@@ -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"}

@miq-bot
Copy link
Collaborator

miq-bot commented Nov 13, 2019

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
2 files checked, 0 offenses detected
Everything looks fine. 🍰

@miq-bot
Copy link
Collaborator

miq-bot commented Dec 18, 2019

This pull request is not mergeable. Please rebase and repush.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JSON Parser error when trying to parse multipart request body
3 participants