-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fixed xml formatter to work with entity presenter #309
Closed
+42
−2
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think this is incorrect. If the object responds to
to_xml
, why would you first convert it to JSON?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.
So i'm not really sure how to go about it but basically if you look at this file https://github.com/intridea/grape/blob/master/lib/grape/entity.rb you will see this at
line 305 # The serializable hash is the Entity's primary output. It is the transformed
# hash for the given data model and is used as the basis for serialization to
# JSON and other formats.
#
# @param runtime_options [Hash] Any options you pass in here will be known to the entity
# representation, this is where you can trigger things from conditional options
# etc.
def serializable_hash(runtime_options = {})
So what i'm trying to do is convert the object coming into def call to be a serialized hash and then generate the xml from that. The reason I converted to json is that when looking at the other formatters I didn't see a clear way to get directly to a serializable hash. Perhaps it would be better to create some other object for dealing with this conversion process but as a first stab at it I put it directly in the formatter.
At the very least I think I added a test in the correct area so if you have a better idea of how to solve this then i'd love to see what you come up with.
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 think that the strategy should be similar to how we do JSON: if the object can do
to_json
, call that. Otherwise use a well known serializer, like MultiXml, to transform the object to XML.In the end, we need tests that make sure that all these types transform to valid xml: String, Array, a class that supports to_xml, a class that doesn't support to_xml, etc.
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.
Right but isn't the entity.rb responsible for making the object respond to to_xml? That's sort of the point as I understand it. My object may not support to_xml but once I expose some properties on it then those methods will be able to be represented via xml. If that's not the case then i'm not really sure what the point of the Entity class is.
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.
Yes, you're correct. I didn't think I contradicted myself though - Entity should define to_xml in this case, it's not the job of the formatter to know it's dealing with something called Entity.
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.
Here is the problem I encountered. Perhaps i'm missing some key piece of evidence. If you go into the Entity class in entity.rb and override to_xml you will see that you get xml that looks like this.
<?xml version="1.0" encoding="UTF-8"?>
<hash></hash>
And basically everything inside to_xml is ignored. So even if to_xml returns a simple string it doesn't print out. That being the case it lead me to what you see.