From f9144729e8ce5f984f124a9a64f969e5a50e14c7 Mon Sep 17 00:00:00 2001 From: Jonathan Roberts <jonrober@uk.ibm.com> Date: Fri, 27 Mar 2020 17:36:42 +0000 Subject: [PATCH 1/4] Return type info when using restriction --- src/parser/xsd/element.js | 1 + src/parser/xsd/simpleType.js | 2 +- test/wsdl-test.js | 26 ++++++++++++++ test/wsdl/simpleTypeTest.wsdl | 64 +++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 test/wsdl/simpleTypeTest.wsdl diff --git a/src/parser/xsd/element.js b/src/parser/xsd/element.js index 8e645b35..0de85492 100644 --- a/src/parser/xsd/element.js +++ b/src/parser/xsd/element.js @@ -84,6 +84,7 @@ class Element extends XSDElement { } else if (child instanceof SimpleType) { descriptor.isSimple = true; descriptor.jsType = child.jsType; + descriptor.type = child.type; } } } diff --git a/src/parser/xsd/simpleType.js b/src/parser/xsd/simpleType.js index 60fba387..cb02b47d 100644 --- a/src/parser/xsd/simpleType.js +++ b/src/parser/xsd/simpleType.js @@ -38,7 +38,7 @@ class SimpleType extends XSDElement { this.restriction.postProcess(definitions); if (this.restriction.base) { // Use the base type - this.type = this.restriction.base.type; + this.type = this.restriction.base; } } else if (this.list) { this.list.postProcess(definitions); diff --git a/test/wsdl-test.js b/test/wsdl-test.js index 099e68c3..8aae3d29 100644 --- a/test/wsdl-test.js +++ b/test/wsdl-test.js @@ -187,6 +187,32 @@ describe('wsdl-tests', function() { }); }); + it('should return type data about simpleTypes within elements', function(done) { + openWSDL(path.resolve(__dirname, 'wsdl/simpleTypeTest.wsdl'), function( + err, + def + ) { + var operation = def.definitions.bindings.SimpleTypeExampleBinding.operations.SimpleTypeExample; + var operationDesc = operation.describe(def); + + assert(operationDesc.input.body.elements[0].qname); + assert.equal(operationDesc.input.body.elements[0].qname.name, "elementWithType"); + assert(operationDesc.input.body.elements[0].isSimple); + assert(operationDesc.input.body.elements[0].type); + assert.equal(operationDesc.input.body.elements[0].type.name, "string"); + + + assert(operationDesc.input.body.elements[1].qname); + assert.equal(operationDesc.input.body.elements[1].qname.name, "elementWithSimpleType1"); + assert(operationDesc.input.body.elements[1].isSimple); + assert(operationDesc.input.body.elements[1].type); + assert.equal(operationDesc.input.body.elements[1].type.name, "simpleType"); + assert.equal(operationDesc.input.body.elements[1].type.$name, "string"); + + done(); + }); + }); + it('should map isMany values correctly', function(done) { openWSDL(path.resolve(__dirname, 'wsdl/marketo.wsdl'), function( err, diff --git a/test/wsdl/simpleTypeTest.wsdl b/test/wsdl/simpleTypeTest.wsdl new file mode 100644 index 00000000..490b5228 --- /dev/null +++ b/test/wsdl/simpleTypeTest.wsdl @@ -0,0 +1,64 @@ +<?xml version="1.0" encoding="UTF-8"?> +<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:s0="urn:restriction:functions" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="urn:restriction:functions"> + <types> + <xsd:schema targetNamespace="urn:restriction:functions"> + <xsd:element name="SimpleTypeExample"> + <xsd:complexType> + <xsd:all> + <xsd:element type="xsd:string" name="elementWithType"/> + <xsd:element name="elementWithSimpleType1"> + <xsd:simpleType> + <xsd:restriction base="xsd:string"> + <xsd:maxLength value="3"/> + </xsd:restriction> + </xsd:simpleType> + </xsd:element> + </xsd:all> + </xsd:complexType> + </xsd:element> + <xsd:element name="SimpleTypeExample.Response"> + <xsd:complexType> + <xsd:all> + <xsd:element name="elementWithSimpleType2"> + <xsd:simpleType> + <xsd:restriction base="xsd:string"> + <xsd:maxLength value="10"/> + </xsd:restriction> + </xsd:simpleType> + </xsd:element> + </xsd:all> + </xsd:complexType> + </xsd:element> + </xsd:schema> + </types> + <message name="SimpleTypeExampleInput"> + <part name="parameters" element="s0:SimpleTypeExample"/> + </message> + <message name="SimpleTypeExampleOutput"> + <part name="parameters" element="s0:SimpleTypeExample.Response"/> + </message> + <portType name="SimpleTypeExamplePortType"> + <operation name="SimpleTypeExample"> + <input message="s0:SimpleTypeExampleInput"/> + <output message="s0:SimpleTypeExampleOutput"/> + </operation> + </portType> + <binding name="SimpleTypeExampleBinding" type="s0:SimpleTypeExamplePortType"> + <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> + <operation name="SimpleTypeExample"> + <soap:operation soapAction="http://www.sap.com/SimpleTypeExample"/> + <input> + <soap:body use="literal"/> + </input> + <output> + <soap:body use="literal"/> + </output> + </operation> + </binding> + <service name="SimpleTypeExampleService"> + <documentation>Service SimpleTypeExample via SOAP</documentation> + <port name="SimpleTypeExamplePortType" binding="s0:SimpleTypeExampleBinding"> + <soap:address location="http://example:8000/endpoint"/> + </port> + </service> +</definitions> From 3dd56470e0a51d98884279213e53111f0881fb2e Mon Sep 17 00:00:00 2001 From: Jonathan Roberts <jonrober@uk.ibm.com> Date: Sun, 29 Mar 2020 17:51:37 +0100 Subject: [PATCH 2/4] Add comments --- test/wsdl-test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/wsdl-test.js b/test/wsdl-test.js index 8aae3d29..d84d83fe 100644 --- a/test/wsdl-test.js +++ b/test/wsdl-test.js @@ -195,13 +195,14 @@ describe('wsdl-tests', function() { var operation = def.definitions.bindings.SimpleTypeExampleBinding.operations.SimpleTypeExample; var operationDesc = operation.describe(def); + // Check that specified as '<xsd:element type="xsd:string"' is present assert(operationDesc.input.body.elements[0].qname); assert.equal(operationDesc.input.body.elements[0].qname.name, "elementWithType"); assert(operationDesc.input.body.elements[0].isSimple); assert(operationDesc.input.body.elements[0].type); assert.equal(operationDesc.input.body.elements[0].type.name, "string"); - + // Check that a type specified as a restriction is present assert(operationDesc.input.body.elements[1].qname); assert.equal(operationDesc.input.body.elements[1].qname.name, "elementWithSimpleType1"); assert(operationDesc.input.body.elements[1].isSimple); From 2c34d198fb93ae1184d09a145be44e7ef354ed09 Mon Sep 17 00:00:00 2001 From: Jonathan Roberts <jonrober@uk.ibm.com> Date: Wed, 8 Apr 2020 22:35:47 +0100 Subject: [PATCH 3/4] Add restriction test --- src/parser/xsd/restriction.js | 6 ++++++ test/client-restrictions-test.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/parser/xsd/restriction.js b/src/parser/xsd/restriction.js index 5ce388e1..f0ccf4ff 100644 --- a/src/parser/xsd/restriction.js +++ b/src/parser/xsd/restriction.js @@ -107,6 +107,12 @@ class Restriction extends XSDElement { } else if (typeof this.base.jsType === 'function' && this.base.jsType !== Date && this.base.jsType !== Number) { val = this.base.jsType(val); } + + if(this.base.jsType === Number) { + if(isNaN(val)){ + violations.push('value is not a number (' + val + ')'); + } + } } if (this.minExclusive !== undefined) { diff --git a/test/client-restrictions-test.js b/test/client-restrictions-test.js index cbaada0c..ce2d2ff4 100644 --- a/test/client-restrictions-test.js +++ b/test/client-restrictions-test.js @@ -107,6 +107,24 @@ describe('SOAP Client', function() { }, 'http://' + hostname + ":" + server.address().port); }); + it('should throw error if the type doesn\'t match its restriction of number', function (done) { + soap.createClient(__dirname + '/wsdl/restrictions.wsdl', { enforceRestrictions: true }, function (err, client) { + assert.equal(err, null); + try { + client.TestRestrictions({ + RestrictionRequest: { + minExclusive: "five" + } + }, function () { + done('It should have thrown error'); + }); + } catch (err) { + assert.equal(err.message, 'The field MinExclusiveType cannot have value five due to the violations: ["value is not a number (five)"]'); + done(); + } + }, 'http://' + hostname + ":" + server.address().port); + }); + it('should throw if the minInclusive doesn\'t match its restriction', function (done) { soap.createClient(__dirname + '/wsdl/restrictions.wsdl', { enforceRestrictions: true }, function (err, client) { assert.equal(err, null); From 56067bab524e572aa34d794cd8885c8118b8ed96 Mon Sep 17 00:00:00 2001 From: DEEPAK RAJAMOHAN <deepak.r.kris@gmail.com> Date: Fri, 17 Apr 2020 02:26:56 -0700 Subject: [PATCH 4/4] fix: add restrictions client tests --- src/parser/xmlHandler.js | 5 +- src/parser/xsd/element.js | 13 ++++- test/client-restrictions-test.js | 18 ++++++ test/wsdl-test.js | 39 +++++++------ test/wsdl/restriction_anonymous_types.wsdl | 61 +++++++++++++++++++++ test/wsdl/restriction_types.xsd | 8 ++- test/wsdl/simpleTypeTest.wsdl | 64 ---------------------- 7 files changed, 122 insertions(+), 86 deletions(-) create mode 100644 test/wsdl/restriction_anonymous_types.wsdl delete mode 100644 test/wsdl/simpleTypeTest.wsdl diff --git a/src/parser/xmlHandler.js b/src/parser/xmlHandler.js index 82c3c585..6264bb56 100644 --- a/src/parser/xmlHandler.js +++ b/src/parser/xmlHandler.js @@ -123,7 +123,10 @@ class XMLHandler { if (this.options.enforceRestrictions && descriptor.type) { const schema = this.schemas[descriptor.type.nsURI]; if (schema) { - const type = schema.simpleTypes[descriptor.type.name]; + let type = schema.simpleTypes[descriptor.type.name]; + if (!type && descriptor.type.anonymous) { + type = descriptor.type.anonymous; + } if (type) { const restriction = type.restriction; if (restriction) { diff --git a/src/parser/xsd/element.js b/src/parser/xsd/element.js index 0de85492..6051d568 100644 --- a/src/parser/xsd/element.js +++ b/src/parser/xsd/element.js @@ -82,9 +82,18 @@ class Element extends XSDElement { } break; } else if (child instanceof SimpleType) { + child.$name = this.$name; + let typeQName = child.getQName(); + var descriptor = this.descriptor = + new XSDElement.ElementDescriptor(qname, typeQName, form, isMany); descriptor.isSimple = true; - descriptor.jsType = child.jsType; - descriptor.type = child.type; + if (child.type && child.type.jsType) { + descriptor.jsType = child.type.jsType; + } else if (child.jsType) { + descriptor.jsType = child.jsType; + } + descriptor.type = typeQName; + descriptor.type.anonymous = child; } } } diff --git a/test/client-restrictions-test.js b/test/client-restrictions-test.js index ce2d2ff4..aa990b24 100644 --- a/test/client-restrictions-test.js +++ b/test/client-restrictions-test.js @@ -374,6 +374,24 @@ describe('SOAP Client', function() { }, 'http://' + hostname + ":" + server.address().port); }); + it('should throw if the maxLength doesn\'t match its restriction in a private simple type', function (done) { + soap.createClient(__dirname + '/wsdl/restriction_anonymous_types.wsdl', { enforceRestrictions: true }, function (err, client) { + assert.equal(err, null); + try { + client.TestRestrictions({ + RestrictionRequest: { + elementWithAnonymousType: 'abcdef' + } + }, function () { + done('It should have thrown error'); + }); + } catch (err) { + assert.equal(err.message, 'The field elementWithAnonymousType cannot have value abcdef due to the violations: ["length is bigger than maxLength (6 > 3)"]'); + done(); + } + + }, 'http://' + hostname + ":" + server.address().port); + }); }); }); diff --git a/test/wsdl-test.js b/test/wsdl-test.js index d84d83fe..7289d015 100644 --- a/test/wsdl-test.js +++ b/test/wsdl-test.js @@ -187,28 +187,31 @@ describe('wsdl-tests', function() { }); }); - it('should return type data about simpleTypes within elements', function(done) { - openWSDL(path.resolve(__dirname, 'wsdl/simpleTypeTest.wsdl'), function( + it('should return type data about anonymous simpleTypes within elements', function(done) { + openWSDL(path.resolve(__dirname, 'wsdl/restriction_anonymous_types.wsdl'), function( err, def ) { - var operation = def.definitions.bindings.SimpleTypeExampleBinding.operations.SimpleTypeExample; + var operation = def.definitions.bindings.RestrictionsBinding.operations.TestRestrictions; var operationDesc = operation.describe(def); - - // Check that specified as '<xsd:element type="xsd:string"' is present - assert(operationDesc.input.body.elements[0].qname); - assert.equal(operationDesc.input.body.elements[0].qname.name, "elementWithType"); - assert(operationDesc.input.body.elements[0].isSimple); - assert(operationDesc.input.body.elements[0].type); - assert.equal(operationDesc.input.body.elements[0].type.name, "string"); - - // Check that a type specified as a restriction is present - assert(operationDesc.input.body.elements[1].qname); - assert.equal(operationDesc.input.body.elements[1].qname.name, "elementWithSimpleType1"); - assert(operationDesc.input.body.elements[1].isSimple); - assert(operationDesc.input.body.elements[1].type); - assert.equal(operationDesc.input.body.elements[1].type.name, "simpleType"); - assert.equal(operationDesc.input.body.elements[1].type.$name, "string"); + + var requestElements = operationDesc.input.body.elements[0].elements; + + // second element in the operation request is an element with anonymous type + // , hence pick array[1] + let elementWithAnonymousType = requestElements[1]; + assert(elementWithAnonymousType.isSimple); + assert.equal(elementWithAnonymousType.qname.name, "elementWithAnonymousType"); + // anonymous types have same name as parent element + assert.equal(elementWithAnonymousType.type.name, elementWithAnonymousType.qname.name); + // Check if anonymous type restriction is embedded in the parent element + assert.equal(elementWithAnonymousType.type.anonymous.name, "simpleType"); + assert.equal(elementWithAnonymousType.type.anonymous.type.$name, "string"); + + // also check element with type for regression + assert.equal(requestElements[0].qname.name, "stringElement"); + assert(requestElements[0].isSimple); + assert.equal(requestElements[0].type.name, "string"); done(); }); diff --git a/test/wsdl/restriction_anonymous_types.wsdl b/test/wsdl/restriction_anonymous_types.wsdl new file mode 100644 index 00000000..ed40f2d7 --- /dev/null +++ b/test/wsdl/restriction_anonymous_types.wsdl @@ -0,0 +1,61 @@ +<?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:xsd="http://www.w3.org/2001/XMLSchema" + xmlns:tns="http://www.Restrictions.com" + xmlns:n="http://www.Restriction.com/Types" + xmlns:ns="http://schemas.xmlsoap.org/soap/encoding/" + targetNamespace="http://www.Restrictions.com"> + <wsdl:types> + <xsd:schema xmlns:tns="http://www.Restriction.com/Types" + xmlns:xs="http://www.w3.org/2001/XMLSchema" + targetNamespace="http://www.Restriction.com/Types" + elementFormDefault="qualified" attributeFormDefault="unqualified"> + <xsd:complexType name="RestrictionRequestType"> + <xsd:all> + <xsd:element type="xsd:string" name="stringElement"/> + <xsd:element name="elementWithAnonymousType"> + <xsd:simpleType> + <xsd:restriction base="xsd:string"> + <xsd:maxLength value="3"/> + </xsd:restriction> + </xsd:simpleType> + </xsd:element> + </xsd:all> + </xsd:complexType> + <xs:element name="RestrictionRequest" type="RestrictionRequestType"/> + <xs:element name="RestrictionResponse" type="RestrictionRequestType"/> + </xsd:schema> + </wsdl:types> + <wsdl:message name="RestrictionsRequest"> + <wsdl:part name="RestrictionsRequest" element="n:RestrictionRequest"/> + </wsdl:message> + <wsdl:message name="RestrictionsResponse"> + <wsdl:part name="RestrictionsResponse" element="n:RestrictionResponse"/> + </wsdl:message> + <wsdl:portType name="RestrictionsPortType"> + <wsdl:operation name="TestRestrictions"> + <wsdl:input message="tns:RestrictionsRequest"/> + <wsdl:output message="tns:RestrictionsResponse"/> + </wsdl:operation> + </wsdl:portType> + <wsdl:binding name="RestrictionsBinding" type="tns:RestrictionsPortType"> + <soap:binding style="document" + transport="http://schemas.xmlsoap.org/soap/http"/> + <wsdl:operation name="TestRestrictions"> + <soap:operation soapAction="http://www.Restrictions.com#Restrictions" + 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="RestrictionsService"> + <wsdl:port name="RestrictionsPortType" binding="tns:RestrictionsBinding"> + <soap:address location="http://www.Restrictions.com/"/> + </wsdl:port> + </wsdl:service> +</wsdl:definitions> diff --git a/test/wsdl/restriction_types.xsd b/test/wsdl/restriction_types.xsd index 421ece4a..b9ef3879 100644 --- a/test/wsdl/restriction_types.xsd +++ b/test/wsdl/restriction_types.xsd @@ -94,9 +94,15 @@ <xs:element name="enumeration1" type="EnumerationType"/> <xs:element name="enumeration2" type="EnumerationType"/> <xs:element name="enumeration3" type="EnumerationType"/> + <xs:element name="elementWithAnonymousType"> + <xs:simpleType> + <xs:restriction base="xs:string"> + <xs:maxLength value="3"/> + </xs:restriction> + </xs:simpleType> + </xs:element> </xs:sequence> </xs:complexType> <xs:element name="RestrictionRequest" type="RestrictionRequestType"/> <xs:element name="RestrictionResponse" type="RestrictionRequestType"/> </xs:schema> - diff --git a/test/wsdl/simpleTypeTest.wsdl b/test/wsdl/simpleTypeTest.wsdl deleted file mode 100644 index 490b5228..00000000 --- a/test/wsdl/simpleTypeTest.wsdl +++ /dev/null @@ -1,64 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:s0="urn:restriction:functions" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="urn:restriction:functions"> - <types> - <xsd:schema targetNamespace="urn:restriction:functions"> - <xsd:element name="SimpleTypeExample"> - <xsd:complexType> - <xsd:all> - <xsd:element type="xsd:string" name="elementWithType"/> - <xsd:element name="elementWithSimpleType1"> - <xsd:simpleType> - <xsd:restriction base="xsd:string"> - <xsd:maxLength value="3"/> - </xsd:restriction> - </xsd:simpleType> - </xsd:element> - </xsd:all> - </xsd:complexType> - </xsd:element> - <xsd:element name="SimpleTypeExample.Response"> - <xsd:complexType> - <xsd:all> - <xsd:element name="elementWithSimpleType2"> - <xsd:simpleType> - <xsd:restriction base="xsd:string"> - <xsd:maxLength value="10"/> - </xsd:restriction> - </xsd:simpleType> - </xsd:element> - </xsd:all> - </xsd:complexType> - </xsd:element> - </xsd:schema> - </types> - <message name="SimpleTypeExampleInput"> - <part name="parameters" element="s0:SimpleTypeExample"/> - </message> - <message name="SimpleTypeExampleOutput"> - <part name="parameters" element="s0:SimpleTypeExample.Response"/> - </message> - <portType name="SimpleTypeExamplePortType"> - <operation name="SimpleTypeExample"> - <input message="s0:SimpleTypeExampleInput"/> - <output message="s0:SimpleTypeExampleOutput"/> - </operation> - </portType> - <binding name="SimpleTypeExampleBinding" type="s0:SimpleTypeExamplePortType"> - <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> - <operation name="SimpleTypeExample"> - <soap:operation soapAction="http://www.sap.com/SimpleTypeExample"/> - <input> - <soap:body use="literal"/> - </input> - <output> - <soap:body use="literal"/> - </output> - </operation> - </binding> - <service name="SimpleTypeExampleService"> - <documentation>Service SimpleTypeExample via SOAP</documentation> - <port name="SimpleTypeExamplePortType" binding="s0:SimpleTypeExampleBinding"> - <soap:address location="http://example:8000/endpoint"/> - </port> - </service> -</definitions>