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

fix test fails for request-response-samples #6

Merged
merged 2 commits into from
Sep 14, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix test fails for request-response-samples
errors in test xml and wsdl files are fixed. some errors are in the code
related to namespaces, prefixes, attributes
  • Loading branch information
deepakrkris committed Sep 14, 2016
commit 3be6b91175f203b205e20bd488a581f7cd7a918b
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -4,3 +4,4 @@ coverage
lib
.idea
*.iml
.vscode
Binary file added .vscode/.browse.VC.db
Binary file not shown.
Binary file added .vscode/.browse.VC.db-wal
Binary file not shown.
46 changes: 46 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/index.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"externalConsole": false,
"sourceMaps": false,
"outDir": null
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outDir": null,
"localRoot": "${workspaceRoot}",
"remoteRoot": null
},
{
"name": "Attach to Process",
"type": "node",
"request": "attach",
"processId": "${command.PickProcess}",
"port": 5858,
"sourceMaps": false,
"outDir": null
}
]
}
4 changes: 2 additions & 2 deletions src/parser/nscontext.js
Original file line number Diff line number Diff line change
@@ -79,7 +79,7 @@ class NamespaceScope {
default:
for (var p in this.namespaces) {
if (this.namespaces[p].uri === nsURI) {
return p;
return this.namespaces[p];
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we return the object instead of namespace prefix as a string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@raymondfeng the namespace object has more details needed by the calling stack

}
}
if (!localOnly && this.parent) {
@@ -206,7 +206,7 @@ class NamespaceContext {
return mapping;
}
}
if (this.getNamespaceURI(prefix, true)) {
if (this.getNamespaceURI(prefix)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder why you need to check the prefix beyond the local scope. We only have to generate a unique prefix if there is a conflict at current level. It's fine to redeclare a prefix mapping to a different uri.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@raymondfeng, suppose we have a parent element with prefix ns1 (xmlns: "http://AAA") and a child with prefix ns1 (xmlns: "http://XYZ"), it might be misleading to read the xml document

Copy link
Contributor

Choose a reason for hiding this comment

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

The override of prefix/uri mapping is valid though.

// The prefix is already mapped to a different namespace
prefix = this.generatePrefix();
}
104 changes: 77 additions & 27 deletions src/parser/xmlHandler.js
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ class XMLHandler {
if (descriptor instanceof ElementDescriptor) {
name = descriptor.qname.name;
let isSimple = descriptor.isSimple;
let attrs = null;
if (descriptor.isMany && !isSimple) {
if (Array.isArray(val)) {
for (let i = 0, n = val.length; i < n; i++) {
@@ -58,39 +59,60 @@ class XMLHandler {
return node;
}
}
if(isSimple && val !== null && typeof val === "object"){
if (typeof val[this.options.attributesKey] !== "undefined"){
attrs = val[this.options.attributesKey];
}
if (typeof val[this.options.valueKey] !== "undefined"){
val = val[this.options.valueKey];
}
}

let element;
if (descriptor.form === 'unqualified') {
element = isSimple ? node.element(name, val) : node.element(name);
} else if (descriptor.qname) {
nsContext.pushContext();
let mapping = declareNamespace(nsContext, null,
let mapping = nsContext.getPrefix(descriptor.qname.nsURI);
Copy link
Contributor

Choose a reason for hiding this comment

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

Some formatting issues here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@raymondfeng I am adding more comments and correcting indentation. Will request for a PTAL tomorrow

let newlyDeclared = false;
if(mapping === null || mapping.declared === false) {
newlyDeclared = true;
mapping = declareNamespace(nsContext, null,
descriptor.qname.prefix, descriptor.qname.nsURI);
}
let prefix = mapping ? mapping.prefix : descriptor.qname.prefix;
let elementName = prefix ? prefix + ':' + name : name;
element = isSimple ? node.element(elementName, val) :
node.element(elementName);
if (mapping) {
if (newlyDeclared) {
element.attribute(prefix ? 'xmlns:' + prefix : 'xmlns',
descriptor.qname.nsURI);
}
}
if (isSimple) {
nsContext.popContext();
return node;
}
if (val == null) {
if (descriptor.isNillable) {
// Set xsi:nil = true
declareNamespace(nsContext, element, 'xsi', helper.namespaces.xsi);
element.attribute('xsi:nil', true);
if(attrs !== null){
if(typeof attrs === "object"){
for (var _p in attrs) {
var _child = attrs[_p];
element.attribute(_p,_child);
}
}
}
if (val == null) {
if (descriptor.isNillable) {
// Set xsi:nil = true
declareNamespace(nsContext, element, 'xsi', helper.namespaces.xsi);
element.attribute('xsi:nil', true);
}
<<<<<<< e0fcf4af0b54a12fc73109c541de73a25f1f95f9
}
nsContext.popContext();
return node;
}

if (typeof val !== 'object' || (val instanceof Date)) {
element.text(val);
nsContext.popContext();
//nsContext.popContext();
return node;
}

@@ -139,25 +161,29 @@ class XMLHandler {
}
}

for (let p in val) {
if (p === this.options.attributesKey) continue;
let child = val[p];
let childDescriptor = elements[p] || attributes[p];
if (childDescriptor == null) {
if (this.options.ignoreUnknownProperties) continue;
else childDescriptor =
new ElementDescriptor(QName.parse(p), null, 'unqualified',
Array.isArray(child));
}
this.jsonToXml(node, nsContext, childDescriptor, child);
if (!Array.isArray(val)) {
for (let p in val) {
if (p === this.options.attributesKey) continue;
let child = val[p];
let childDescriptor = elements[p] || attributes[p];
if (childDescriptor == null) {
if (this.options.ignoreUnknownProperties) continue;
else childDescriptor =
new ElementDescriptor(QName.parse(p), null, 'unqualified',
Array.isArray(child));
}
if (childDescriptor){
this.jsonToXml(node, nsContext, childDescriptor, child);
}
}
}

var attrs = val[this.options.attributesKey];
if (attrs != null && typeof attrs === 'object') {
for (let p in attrs) {
let child = attrs[p];
if (p === this.options.xsiTypeKey) {
let xsiType = QName.parse(child);
let xsiType = QName.parse(child.type, child.xmlns);
declareNamespace(nsContext, node, 'xsi', helper.namespaces.xsi);
let mapping = declareNamespace(nsContext, node, xsiType.prefix,
xsiType.nsURI);
@@ -348,6 +374,7 @@ class XMLHandler {
var root = {};
var refs = {}, id; // {id: {hrefs:[], obj:}, ...}
var stack = [{name: null, object: root, descriptor: descriptor}];
var options = this.options;

p.onopentag = function(node) {
nsContext.pushContext();
@@ -370,23 +397,40 @@ class XMLHandler {
for (let a in attrs) {
if (/^xmlns:|^xmlns$/.test(a)) continue;
let qname = QName.parse(a);
var isXsiType = false;
var xsiType = null;
var xsiXmlns = null;
if (nsContext.getNamespaceURI(qname.prefix) === helper.namespaces.xsi) {
// Handle xsi:*
if (qname.name == 'nil') {
// xsi:nil
if (attrs[a] === 'true') {
obj = null;
}
continue;
} else if (qname.name === 'type') {
// xsi:type
isXsiType = true;
xsiType = attrs[a];
xsiType = QName.parse(xsiType);
attrs[a] = xsiType.name;
if(xsiType.prefix){
xsiXmlns = nsContext.getNamespaceURI(xsiType.prefix);
}
}
continue;
}
let attrName = qname.name;
elementAttributes = elementAttributes || {};
let attrDescriptor = descriptor && descriptor.findAttribute(qname.name);
let attrValue = parseValue(attrs[a], attrDescriptor);
elementAttributes[attrName] = attrs[a];
if(isXsiType) {
xsiType = {};
xsiType.type = attrs[a];
xsiType.xmlns = xsiXmlns;
elementAttributes[options.xsiTypeKey] = xsiType;
} else {
elementAttributes[attrName] = attrs[a];
}
}

if (elementAttributes) {
@@ -457,9 +501,14 @@ class XMLHandler {
if (/<\?xml[\s\S]+\?>/.test(text)) {
text = self.xmlToJson(null, text);
}
p.ontext(text);
p.handleJsonObject(text);
};

p.handleJsonObject = function (text) {
var top = stack[stack.length - 1];
self._processText(top, text);
};

p.ontext = function(text) {
text = text && text.trim();
if (!text.length)
@@ -518,7 +567,8 @@ function declareNamespace(nsContext, node, prefix, nsURI) {
} else if (node) {
node.attribute('xmlns:' + mapping.prefix, mapping.uri);
return mapping;
}
}
return mapping;
}

function parseValue(text, descriptor) {
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.Dummy.com/Common"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:simpletype name="RestrictedStringType">
<xs:restriction base="xs:string">
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpletype>
<xs:element name="Order">
<xs:complexType>
<xs:sequence>
<xs:element name="Id" type="xs:integer"/>
<xs:element name="Name" type="RestrictedStringType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Result">
<xs:complexType>
<xs:sequence>
<xs:element name="Status" type="xs:integer"/>
<xs:element name="Message" type="RestrictedStringType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://NamespaceTest.com/CommonTypes"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:simpleType name="restrictedString">
<xs:restriction base="xs:string">
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:cmn="http://NamespaceTest.com/CommonTypes"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.Dummy.com/Common"
xmlns="http://www.Dummy.com/Common">
<xs:import schemaLocation="common.xsd" namespace="http://NamespaceTest.com/CommonTypes" />
<xs:element name="Order">
<xs:complexType>
<xs:sequence>
<xs:element name="Id" type="xs:integer"/>
<xs:element name="Name" type="cmn:restrictedString"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Result">
<xs:complexType>
<xs:sequence>
<xs:element name="Status" type="xs:integer"/>
<xs:element name="Message" type="cmn:restrictedString"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"Order": {
"Id": 1,
"Name": "foo"
{
"DummyRequest": {
"Order": {
"Id": 1,
"Name": "foo"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dummy="http://www.Dummy.com"
xmlns:tns="http://www.Dummy.com"
xmlns:common="http://www.Dummy.com/Common">
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header/>
<soap:Body>
<dummy:DummyRequest
xmlns:dummy="http://www.Dummy.com"
xmlns="http://www.Dummy.com">
<common:Order
xmlns:common="http://www.Dummy.com/Common">
<common:Id>1</common:Id>
<common:Name>foo</common:Name>
</common:Order>
</dummy:DummyRequest>
<ns1:DummyRequest
xmlns:ns1="http://www.Dummy.com">
<ns2:Order
xmlns:ns2="http://www.Dummy.com/Common">
<Id>1</Id>
<Name>foo</Name>
</ns2:Order>
</ns1:DummyRequest>
</soap:Body>
</soap:Envelope>
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
targetNamespace="http://www.Dummy.com"
xmlns:common="http://www.Dummy.com/Common"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.Dummy.com/Common" schemaLocation="common.xsd"/>
<xsd:import namespace="http://www.Dummy.com/Common" schemaLocation="main.xsd"/>
<xsd:element name="DummyRequest">
<xsd:complexType>
<xsd:sequence>
Loading