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

Added XML support #448

Closed
wants to merge 9 commits into from
Closed

Conversation

jokklan
Copy link

@jokklan jokklan commented Nov 7, 2013

Hallo

I know that you have said before that you are not focusing on XML support at the time being, but I''ll need it for one of my own projects, so i have build it anyway. Even though it's not done, was it really simple to implement, as a hash easily can be transformed into XML. The only obstacle i came across was that XML needs a root, and json does not, but that doesn't seem to bad. Rabl eg can also render json without root with the same template as it will render xml.

I don't know if you are at all interested, but i can't see a reason not to?

Best regards
/Johan

@jokklan jokklan mentioned this pull request Nov 7, 2013
@byroot
Copy link

byroot commented Nov 19, 2013

👍

@ncr
Copy link

ncr commented Nov 28, 2013

I would love it merged.

if serializer
super(serializer, options)
else
super(resource, options)
Copy link
Contributor

Choose a reason for hiding this comment

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

There's no need to change super to this

Copy link

Choose a reason for hiding this comment

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

Methods created using define_method require explicit params in super

Copy link
Author

Choose a reason for hiding this comment

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

Exactly :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh ya, we move to define_method 👍

@spastorino
Copy link
Contributor

There are no tests for this, we need tests.

@jokklan
Copy link
Author

jokklan commented Nov 29, 2013

I have developed more on this already and written tests for it all. I will push it, hopefully this weekend. The one big (and only real) concern is still however that XML needs a root, and that elements in arrays need a wrapper tag as well, so eg:

{ posts: [
  { title: "XML",
  body: "ActiveModel::Serializer should support XML" }
]},
{ comments: [
  { user_id: 1,
  body: "Agreed!" },
  { user_id: 5,
  body "I don't..."}
]}

Would need to be something like this XML

<result>
   <posts>
     <post>
       <title>XML</title>
       <body>ActiveModel::Serializer should support XML</body>
     </post>
  </posts>
  <comments>
    <comment>
      <user_id>1</user_id>
      <body>Agreed!</body>
    </comment>
    <comment>
      <user_id>5</user_id>
      <body>I don't...</body>
    </comment>
  </comments>
</result>

@ncr
Copy link

ncr commented Nov 29, 2013

I can help if you push.

@jokklan
Copy link
Author

jokklan commented Nov 29, 2013

@ncr I have pushed what i have created so far. It's not done, and few of the specs still fails... but at least there is something to work from, and to discuss :). I will continue to work on this.

@dyanisse
Copy link

dyanisse commented Feb 3, 2014

@jokklan I am interested in this too. Can I help?

@rewritten
Copy link

I would love to help getting this integrated.

@jsborjesson
Copy link

I'm using your fork now. You might have saved me a lot of trouble - cheers!

@jsborjesson
Copy link

I'm sad to say that things like whatever_url(object) stops working in this fork. Any idea why?

@jokklan
Copy link
Author

jokklan commented Feb 15, 2014

@alcesleo I haven't have time to complete this yet, but i will come back to it soon (hopefully this weekend). The whole implementation needs some refactorings and clean up.

@rewritten Any help would be appreciated 👍 I can ping you when i have completed the last clean up, and then maybe you could look it through?

@jsborjesson
Copy link

@jokklan That sounds fantastic, I would totally buy you a beer (or three).

@dyanisse
Copy link

@jokklan thanks for merging, let me know if there is anything else to refactor.

@jokklan
Copy link
Author

jokklan commented Feb 16, 2014

@dyanisse I'll rebase with master and then let's see :)

@rewritten
Copy link

@jokklan please ping me!

@jokklan
Copy link
Author

jokklan commented Feb 19, 2014

@alcesleo @rewritten or any one else that want to help: their need to be made a decision on XML roots. I think the class name would be a good default and how the XML parser works now, but their also have to be a option to set your own root for each serializer object, and/or in a global configuration (partly implemented). Adding tests and documentation for this would be really helpful, then can I (or anyone willing) implement it soon.

If it's to unclear then please ask :)! Otherwise know that I plan getting back to this real soon. I know I keep postponing this PR I'm just a little busy, but I promise I'll finish this!

@rewritten
Copy link

@jokklan The underscored name of the serialized class could be a good default option, but also it should be configurable, yes. Do ou want us to provide (failing) tests for that?

@jokklan
Copy link
Author

jokklan commented Feb 19, 2014

Im writing some refactorings and improvements now. I'll try to add some tests, but i'll have to figure out a clever way to test it without just copy all the json tests :)

@jokklan
Copy link
Author

jokklan commented Feb 19, 2014

This should now work :). The old root option still works as before, and applies to both XML and JSON. I have also added two new options (json_root and xml_root) that takes precedence over root. Nothing is changed in how JSON is rendered. XML renders by creating a hash with root keys for every object, and then parse this to XML (as_xml in serializable).

Their still needs to be made some tests, right now is their only integration tests. Also a good documentation section needs to be added.

I will continue to work on this, but should anyone want to make some unit test or write some documentation, then please do 👍, would help a lot!

Would also be nice to get some feedback from some of the moderators. Any thoughts @spastorino ?

@jsborjesson
Copy link

@jokklan Hi! Sorry I haven't been able to look it over as much as I wanted to, I am trying it out now but I still can't get the url-helpers working. Not quite sure what has changed that made them stop functioning.

@jokklan
Copy link
Author

jokklan commented Feb 20, 2014

@alcesleo that sounds very strange, i can't figure out how this should affect url helpers.

@jsborjesson
Copy link

@jokklan Me neither, but it works in the original version.

@jsborjesson
Copy link

Some obscure inheritance or super-call that has changed maybe?

@jokklan
Copy link
Author

jokklan commented Feb 20, 2014

Properly something to do with that this branch comes from master, so it's something that's introduced in master since last stable release.

@jsborjesson
Copy link

faceplam it actually says something about it in the first paragraph of masters readme.

@jsborjesson
Copy link

This works great for me now! Thanks @jokklan! Let me know if you want to cash in on those beers.

@GeekOnCoffee
Copy link

Is there more work needed to get this merged? Hoping to use AMS but unfortunately XML generation is a requirement for us.

@espen espen mentioned this pull request May 30, 2014
@xymbol
Copy link
Contributor

xymbol commented May 30, 2014

@GeekOnCoffee Hi, there is an ongoing discussion around the future of AMS. Adding XML is not a priority and, even though pluggable formatters are somewhere in the plan, an XML variant doesn't seem to be within what we'd like to implement first. Please check the discussion here.

@steveklabnik
Copy link
Contributor

Yes. Please see here: https://groups.google.com/forum/#!topic/rails-api-core/8zu1xjIOTAM

I have some concerns about putting this in 0.9, but I wouldn't mind for 0.10 (and the eventual 1.0) to have XML support, and it should be even easier, with the new architecture.

Let me think about this some more before merging.

@steveklabnik
Copy link
Contributor

Yes. I wouldn't mind this feature in 0.10, as one of our Adapter classes. I don't plan on backporting it to older versions, however. Thanks!

@reidmorrison
Copy link

Now that v0.10 is available, is it worth re-opening the discussion around XML support?

Is it as simple as just adding a new adapter that derives from the JSON adapter and/or the JSON API adapter, and then just calls to_xml instead of to_json on the final Hash to render the output as XML?

The challenge this would address is being able to render the "equivalent" JSON or XML output on demand. Over time clients can move away from the XML interface to the better JSON layout.

@bf4
Copy link
Member

bf4 commented Jun 3, 2016

@reidmorrison I would help you write an add-on that adds an XML adapter, but, at present, it's not a good fit for AMS. Additionally, active model xml serializers have been extracted from Rails 5, so it would be problematic to make that a dependency.

Please open a new issue if you'd like to discuss more, esp with an example of what your use-case is.

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

Successfully merging this pull request may close these issues.