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 naming of root element when rendering response as XML? #398
Comments
Not totally against it, but JSONAPI top-level key is |
I've been developing and testing this whole time using JSON (not JSONAPI) and then am only just now getting to work on the XML part. I haven't actually paid any attention to what the JSONAPI results look like. Looking at it now, it seems there is no top-level key for the JSONAPI payload (same as with the JSON payload). There are two keys on equal level just like with JSON. The (Although maybe things have changed in newer versions. I am stuck on Graphti 1.3.5 right now. Sorry!) Example: {"data":[{"id":"1","type":"my_resource","attributes":{"title":"Resource number 1"},{"id":"2","type":"my_resource","attributes":{"title":"Resource number 2"}],"meta":{"stats":{"total":{"count":4}}}} When no It would appear that XML requires an additional, named, root layer, whereas the JSON and JSONAPI formats are happy enough to have an unnamed root container layer. |
The other keys are meta/included, which aren't really relevant to the shape of JSON/XML, which leaves us with just |
As far as I can tell, there is no top-level key for JSON-formatted payloads, nor for JSONAPI-formatted payloads. And by that I mean, if a XML just wants to wrap that whole situation up into an additional named top-level key. So when there is only a Am I explaining this well enough? Maybe I need to draw some Venn diagrams? :) |
Maybe if I format the example from above a bit you'll see what I mean.
|
Right, this is JSONAPI. But |
Hmmm, I'm not seeing that on my end. Here is what the same response looks like when requested as JSON (and returned as JSON not JSONAPI): { "data":[
{"id":"1","title":"Resource number 1"},
{"id":"2","title":"Resource number 2"}
]
,
"meta":{"stats":{"total":{"count":4}}}
} |
Haha, my mistake I didn't know we supported meta. Sorry! In any case, doesn't this match XML and JSONAPI? You want to rename |
JSON and JSONAPI match each other when it comes down to the relevant hierarchy. XML adds an additional layer wrapping everything. That additional wrapper just so happens to be called To be clear, I am not trying to rename the Again, check out the large code comment above for what I'm hoping to accomplish. |
Gotcha, I see now, thanks for bearing with me. Sounds like we want to remove that top-level key entirely then? |
I'm not well-versed in XML, so I've looked this up. Apparently a root (top-level key) is required. So it can not be removed. The ideal situation IMO is to default the name of the top-level key to def to_xml(root = :data)
render(self.class.hash_renderer(@proxy)).to_xml(root: root)
end How that would be handled upstream of the method is not something I've looked at, nor have I considered exactly what it would look like to send this new argument in the controller... |
Ah, I thought there was a |
Thank you so much for your quick and informative replies. I keep forgetting to mention this. It's nice to have a good discussion with quick turn-around. 😁 If you're cool with potentially breaking implementations that rely on the XML formatting done by Graphiti, your easy button is: def to_xml
render(self.class.hash_renderer(@proxy)).to_xml(root: :root)
end Or if you're okay with the default name of def to_xml
render(self.class.hash_renderer(@proxy)).to_xml
end Update your test(s) and be done. 😈 I'm not sure this is the way you'd like to go, but it is a thought. 😉 |
No problem! Thanks for bearing with me. I was thinking for backwards-compat: def to_xml
render(self.class.hash_renderer(@proxy)).to_xml(root: Graphiti.config.xml_root)
end And defaulting the option to |
Oh yah, a configuration seems like an elegant solution. For backwards-compatibility you'd want to default that option to |
Oh yes my mistake, you got it :) |
This config file is the And that file always seems to be shown as beginning with 3 hyphens. Is that right? ---
namespace: / Are those hyphens really necessary? I've always been curious about that... |
That's really only used for generators, check out https://github.com/graphiti-api/graphiti/blob/master/lib/graphiti/configuration.rb |
To be clear, the Edit: I see looking at the code that it does appear that Edit 2: But then there is mention of a |
We have a client who insists on our API providing XML, so we're fulfilling that.
The XML root element name is hard-coded to
data
in the Graphiti renderer which is not ideal since there is a child element inside also nameddata
.We have worked around this, but it would be nice to be able to specify the root element name where the Graphiti renderer could use it.
Looking at the source, here is where the magic happens, best I can tell:
graphiti/lib/graphiti/renderer.rb
Line 37 in 0389e50
Right now this is a simplified version of what our work-around looks like:
The text was updated successfully, but these errors were encountered: