-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
fix(oas3): validate xml and x-www-form-urlencoded request body #9705
base: issue-9673
Are you sure you want to change the base?
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
|
||
const traverseXML = (node, result = {}) => { | ||
if (node.nodeType === 1) { | ||
// ELEMENT_NODE - parameter | ||
const name = node.nodeName | ||
|
||
if (node.nodeName === "parsererror") { | ||
throw new Error("Parameter string value must be valid XML") | ||
} | ||
|
||
const childNodes = Array.from(node.childNodes) | ||
|
||
if (childNodes.length === 1) { | ||
// only one child node - value of the parameter | ||
result[name] = traverseXML(childNodes[0]) | ||
} else if (childNodes.length > 0) { | ||
// more than one child node - the parameter is an object or an array | ||
const traversedChildren = childNodes.map((child) => traverseXML(child)) | ||
result[name] = traversedChildren.reduce((paramObj, child) => { | ||
Object.entries(child).forEach(([key, value]) => { | ||
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. As I mentioned in a comment
Perhaps we could just add
It seems like the actual parameters should be |
||
paramObj[key] = value | ||
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 is fine if the type of our parameter is an object but if we have an array, ex.
the JSON result for it will be
and that's the only thing we'll be validating. Perhaps we could do something like this
In this case, arrays of length 0 or 1 would still be seen as an object. |
||
}) | ||
return paramObj | ||
}, {}) | ||
} | ||
return result | ||
} else if (node.nodeType === 3) { | ||
// TEXT_NODE - value of a parameter (parent node) | ||
return node.nodeValue | ||
} | ||
return undefined | ||
} | ||
|
||
export const parseXML = (xml) => { | ||
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. Not sure about the name of this function, perhaps it should be |
||
try { | ||
xml = new DOMParser().parseFromString(xml, "application/xml") | ||
xml = traverseXML(xml.childNodes[0]) | ||
return Object.values(xml)[0] | ||
} catch (e) { | ||
return { xmlError: e.message } | ||
} | ||
} |
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.
We might need to get rid of nodes with only whitespace characters here, as at this point our result is this:
The validation for this will pass but perhaps it might create issues later on.