doedje / jquery.soap

This script uses $.ajax to send a SOAP:Envelope. It can take XML DOM, XML string or JSON as input and the response can be returned as either XML DOM, XML string or JSON too.
352 stars 148 forks source link

Adding support for WSDL extended types (fix for issue #14) #95

Closed JordanStopford closed 8 years ago

JordanStopford commented 8 years ago

Example:

var brewTeaRequest = { Size: 'ShortAndStout', TeaType: 'EarlGrey' };

var attrs = { "Pot": { "xsi:type": 'TeaPot' } };

jQuery.soap({ attrs: attrs, url: http://localhost/BrewService, method: 'Brew', namespaceURL: http://localhost/BrewService, data: data });

or ...

var brewCoffeeRequest = { Size: 'Tall', CoffeeType: 'Columbian' };

var attrs = { "Pot": { "xsi:type": 'CoffeePot' } };

jQuery.soap({ attrs: attrs, url: http://localhost/BrewService, method: 'Brew', namespaceURL: http://localhost/BrewService, data: data });

JordanStopford commented 8 years ago

Just to expand on the commit, I found issue #14 whilst trying to support this in a project I am working on currently. I saw your comment that this is supported when passing the request as XML and not with JSON, and I understand that JSON was not meant to provide this capability, but the overhead of adding this in was so low it seemed worth it.

Also: thanks for the great library!

All comments welcome.

doedje commented 8 years ago

Hi Jordan,

First I like to thank you for the pull request. I just think this is the coolest thing about github and open source, we build this stuff together!

It is within that spirit that I am feeling a bit awkward that I have to disappoint you and won't merge your code. I hope you understand that I don't want to be disrespectful towards you effort, I really appreciate that and I hope you will continue to contribute to the open source community in general.

I have a fundamental problem with supporting attributes in the json2xml conversion, because it is not a 1-on-1 conversion, but a certain convention has to be agreed upon to make it work. And with convention comes personal taste. So instead of forcing my own personal taste upon all $.soap users it is much nicer to allow every individual to use their favorite json2xml conversion tool and feed that to the data parameter in $.soap.

example:

var jsonData = {
  "node": {
    "#atrributes": {
      "xsi:type": "foo"
    },
    "#text": "bar"
  }
}
$.soap({
  data: json2xml(jsonData);
});

Here json2xml() could be any json2xml library available on the internet that uses a conversion convention of my liking... You might like another one and it would change your whole syntax of the jsonData declaration, whatever makes you happy! And I like that approach much better than having $.soap (or me) make that decision for you.

The same goes for the xml2json conversion that is done with the response data of the soapcall. I implemented this in the earlier versions of $.soap, but as anthony-redFox correctly pointed out in issue #14, this whole conversion business should have never be part of $.soap in the first place. I was not willing to let go of that, back in the days, because of backwards compatibility. But a future 2.x.x branch might just get rid of that all together.

You are still allowed of course to use your own version of $.soap. You would still be able to update your code with any of the commits that are done against my branches, the change of running into a merge conflict are quite limited, since not that much code has been altered. Although I have some recommendations for you that would make your solution a bit more elegant:

+                        if (attrs) {
+                          for (var key in attrs) {
+                            if (soapObject.name === key) {
+                              var childAttrs = attrs[key];
+                              for (var childKey in childAttrs) {
+                                soapObject.attr(childKey, childAttrs[childKey]);
+                              }
+                            }
+                          }
+                        }

to:

+                        var key = soapObject.name;
+                        if (attrs && attrs[key]) {
+                          var childAttrs = attrs[key];
+                          for (var childKey in childAttrs) {
+                            soapObject.attr(childKey, childAttrs[childKey]);
+                          }
+                        }

Bottom line, I really think json2xml and xml2json conversion are beyond the scope of $.soap. Both might go out of the window in a future 2.x.x branch (which is currenly NOT on my timeline to be happening any time soon....) I encourage everyone to use the conversion tool of their liking to accomplish these conversion, especially when they get more complex than what is supported by $.soap.

Hope you understand!

JordanStopford commented 8 years ago

Hi Remy,

Thanks for the lengthy reply!

I honestly thought it was missing functionality, as it seems to be present in dom2soap. Our typical use of $.soap is to pass in the data as raw JSON instead of converting it ourselves, so the conversion is done in json2soap.

I think it would be helpful to merge this functionality into the library, as I haven't seen many people using XML (or even willing to use XML!) as JSON is the preferred format for this kind of thing, and the library allows you to provide raw JSON without any pre-conversion.

I'll merge in your changes; I haven't got a lot of experience with javascript (my background is C++/Java) so I imagine my code looks rather rudimentary!

Regards,

Jordan

doedje commented 8 years ago

Hi Jordan,

I have been thinking about this issue for some time... But I must admit I don't agree with you on the matter. I agree that I rather use JSON than XML (while soap is based on XML) but using a third party json2xml converter will provide exactly that: you don't have to use any xml yourself, it will be created for you based on the json you write. And there are a lot of json2xml libraries out there. Each with their own way to insert the needed attributes. Being able to use a third party converter of your liking is a big plus if you ask me...