Skip to content

Commit

Permalink
Improve deserialization of inline simpleType declarations (vpulim#1015)
Browse files Browse the repository at this point in the history
  • Loading branch information
kouak authored and herom committed Aug 6, 2018
1 parent 0f220a8 commit c9026a2
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 8 deletions.
34 changes: 26 additions & 8 deletions lib/wsdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,21 @@ var Primitives = {
};

function splitQName(nsName) {
var i = typeof nsName === 'string' ? nsName.indexOf(':') : -1;
return i < 0 ? {prefix: TNS_PREFIX, name: nsName} :
{prefix: nsName.substring(0, i), name: nsName.substring(i + 1)};
if(typeof nsName !== 'string') {
return {
prefix: TNS_PREFIX,
name: nsName,
};
}

const [topLevelName] = nsName.split('|');

const prefixOffset = topLevelName.indexOf(':');

return {
prefix: topLevelName.substring(0, prefixOffset) || TNS_PREFIX,
name: topLevelName.substring(prefixOffset + 1)
};
}

function xmlEscape(obj) {
Expand Down Expand Up @@ -239,7 +251,7 @@ var DefinitionsElement = Element.createSubClass();
var ElementTypeMap = {
types: [TypesElement, 'schema documentation'],
schema: [SchemaElement, 'element complexType simpleType include import'],
element: [ElementElement, 'annotation complexType'],
element: [ElementElement, 'annotation complexType simpleType'],
any: [AnyElement, ''],
simpleType: [SimpleTypeElement, 'restriction'],
restriction: [RestrictionElement, 'enumeration all choice sequence'],
Expand Down Expand Up @@ -742,7 +754,7 @@ SimpleTypeElement.prototype.description = function(definitions) {
var children = this.children;
for (var i = 0, child; child = children[i]; i++) {
if (child instanceof RestrictionElement)
return this.$name + "|" + child.description();
return [this.$name, child.description()].filter(Boolean).join('|');
}
return {};
};
Expand Down Expand Up @@ -770,11 +782,13 @@ RestrictionElement.prototype.description = function(definitions, xmlns) {
return desc;
}

// then simple element
// then simple element
var base = this.$base ? this.$base + "|" : "";
return base + this.children.map(function(child) {
var restrictions = this.children.map(function(child) {
return child.description();
}).join(",");

return [this.$base, restrictions].filter(Boolean).join('|');
};

ExtensionElement.prototype.description = function(definitions, xmlns) {
Expand Down Expand Up @@ -920,7 +934,10 @@ ElementElement.prototype.description = function(definitions, xmlns) {
var children = this.children;
element[name] = {};
for (var i = 0, child; child = children[i]; i++) {
if (child instanceof ComplexTypeElement) {
if (
child instanceof ComplexTypeElement
|| child instanceof SimpleTypeElement
) {
element[name] = child.description(definitions, xmlns);
}
}
Expand Down Expand Up @@ -1460,6 +1477,7 @@ WSDL.prototype.xmlToObject = function(xml, callback) {
var top = stack[stack.length - 1];
var name = splitQName(top.schema).name,
value;

if (self.options && self.options.customDeserializer && self.options.customDeserializer[name]) {
value = self.options.customDeserializer[name](text, top);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:tns="http://www.Dummy.com/Name/Types"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.Dummy.com/Name/Types"
>
<xs:element name="DummyRequest" />
<xs:element name="DummyResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="SimpleBoolean" type="xs:boolean" />
<xs:element name="InlineBoolean">
<xs:simpleType>
<xs:restriction base="xs:boolean" />
</xs:simpleType>
</xs:element>
<xs:element name="SimpleInt" type="xs:integer" />
<xs:element name="InlineInt">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0" />
<xs:maxInclusive value="100" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="SimpleDate" type="xs:date" />
<xs:element name="SimpleDateTime">
<xs:simpleType>
<xs:restriction base="tns:DateTime">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="SimpleTimeHourMinute">
<xs:simpleType>
<xs:restriction base="tns:TimeHourMinute">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="NamedNestedSimpleType">
<xs:simpleType name="NestedSimpleType">
<xs:restriction base="tns:TimeHourMinute">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="SimpleEnum">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:enumeration value="0"/>
<xs:enumeration value="1"/>
<xs:enumeration value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="TimeHourMinute">
<xs:restriction base="xs:string">
<xs:pattern value="(([0-9]{2}(:))[0-9]{2})"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="DateTime">
<xs:restriction base="xs:string">
<xs:pattern value="(((([0-9]{4}(\-))[0-9]{2})(\-))[0-9]{2})"/>
</xs:restriction>
</xs:simpleType>

</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tns="http://www.Dummy.com"
xmlns:n="http://www.Dummy.com/Name/Types"
>
<soap:Body>
<n:DummyRequest xmlns:n="http://www.Dummy.com/Name/Types">
</n:DummyRequest>
</soap:Body>
</soap:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"SimpleBoolean": true,
"InlineBoolean": true,
"SimpleInt": 4,
"InlineInt": 4,
"SimpleDate": "2018-07-21T00:00:00.000Z",
"SimpleDateTime": "2018-07-21T00:00:00.000Z",
"SimpleTimeHourMinute": 150,
"NamedNestedSimpleType": 240,
"SimpleEnum": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tns="http://www.Dummy.com"
xmlns:n="http://www.Dummy.com/Name/Types"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header></soap:Header>
<soap:Body>
<n:DummyResponse>
<n:SimpleBoolean>true</n:SimpleBoolean>
<n:InlineBoolean>true</n:InlineBoolean>
<n:SimpleInt>4</n:SimpleInt>
<n:InlineInt>4</n:InlineInt>
<n:SimpleDate>2018-07-21</n:SimpleDate>
<n:SimpleDateTime>2018-07-21</n:SimpleDateTime>
<n:SimpleTimeHourMinute>02:30</n:SimpleTimeHourMinute>
<n:NamedNestedSimpleType>04:00</n:NamedNestedSimpleType>
<n:SimpleEnum>2</n:SimpleEnum>
</n:DummyResponse>
</soap:Body>
</soap:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.Dummy.com" xmlns:n="http://www.Dummy.com/Name/Types" xmlns:ns="http://schemas.xmlsoap.org/soap/encoding/" targetNamespace="http://www.Dummy.com">
<wsdl:types>
<xs:schema>
<xs:import namespace="http://www.Dummy.com/Name/Types" schemaLocation="name.xsd"/>
</xs:schema>
</wsdl:types>
<wsdl:message name="DummyRequest">
<wsdl:part name="DummyRequest" element="n:DummyRequest"/>
</wsdl:message>
<wsdl:message name="DummyResponse">
<wsdl:part name="DummyResponse" element="n:DummyResponse"/>
</wsdl:message>
<wsdl:portType name="DummyPortType">
<wsdl:operation name="Dummy">
<wsdl:input message="tns:DummyRequest"/>
<wsdl:output message="tns:DummyResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="DummyBinding" type="tns:DummyPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="Dummy">
<soap:operation soapAction="http://www.Dummy.com#Dummy" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="DummyService">
<wsdl:port name="DummyPortType" binding="tns:DummyBinding">
<soap:address location="http://www.Dummy.com/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.customDeserializer = {
DateTime: (text) => new Date(text),
TimeHourMinute: (text) => {
// Return number of minutes since midnight
const results = text.split(':').map(t => parseInt(t, 10));
return results[0] * 60 + results[1];
},
NestedSimpleType: (text) => {
// Return number of minutes since midnight
const results = text.split(':').map(t => parseInt(t, 10));
return results[0] * 60 + results[1];
}
};

0 comments on commit c9026a2

Please sign in to comment.