-
Notifications
You must be signed in to change notification settings - Fork 92
To allow user to customize the root element of xml output #272
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -433,19 +433,26 @@ function toJSON(input) { | |
| } | ||
| } | ||
|
|
||
| function toXML(input) { | ||
| function toXML(input, options) { | ||
| var xml; | ||
| var xmlDefaultOptions = { declaration : true }; | ||
| var xmlOptions = util._extend(xmlDefaultOptions, options); | ||
| if (input && typeof input.toXML === 'function') { | ||
| xml = input.toXML(); | ||
| } else { | ||
| if (input) { | ||
| if (typeof input == 'string') { | ||
| // this is a hack to handle js2xmlparser throws syntax error on string | ||
| // wraps string from "a string" to "\"a string\"" | ||
| input = JSON.stringify(input); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be input = '"' + input + '"';couldn't it?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ritch we could, but we will also need to handle escaping the double quotes in the string itself, not sure any other stuff
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a unit-test to verify this case.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unit-test added. |
||
| } else if (typeof input == 'object') { | ||
| // Trigger toJSON() conversions | ||
| input = toJSON(input); | ||
| } | ||
| if (Array.isArray(input)) { | ||
| input = { result: input }; | ||
| } | ||
| xml = js2xmlparser('response', input, { | ||
| xml = js2xmlparser(xmlOptions.wrapperElement || 'response', input, { | ||
| declaration : { include : xmlOptions.declaration }, | ||
| prettyPrinting: { | ||
| indentString: ' ' | ||
| }, | ||
|
|
@@ -501,13 +508,14 @@ function sendBodyJsonp(res, data) { | |
| res.jsonp(data); | ||
| } | ||
|
|
||
| function sendBodyXml(res, data) { | ||
| function sendBodyXml(res, data, method) { | ||
| if (data === null) { | ||
| res.header('Content-Length', '7'); | ||
| res.send('<null/>'); | ||
| } else if (data) { | ||
| try { | ||
| var xml = toXML(data); | ||
| var xmlOptions = method.returns[0].xml || {}; | ||
| var xml = toXML(data, xmlOptions); | ||
| res.send(xml); | ||
| } catch (e) { | ||
| res.status(500).send(e + '\n' + data); | ||
|
|
@@ -629,7 +637,7 @@ HttpContext.prototype.done = function(cb) { | |
| res.header('Content-Type', operationResults.contentType); | ||
| } | ||
| if (dataExists) { | ||
| operationResults.sendBody(res, data); | ||
| operationResults.sendBody(res, data, method); | ||
| } else { | ||
| if (res.statusCode === undefined || res.statusCode === 200) { | ||
| res.statusCode = 204; | ||
|
|
||
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.
AFAICT, this is adding another feature, which is to allow the developer to specify whether XML declaration is included in the response. I can live with the fact that it's introduced together with other changes, but please add a unit-test to verify.
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.
Unit-test added.