Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Microsoft match an XSD xs:integer to a string when importing WSDL?

Looking at the WDSL on a webservice. the xml states that the datatype is an integer, however, when calling the web method the method expect a string, WDSL code below

<System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType:="integer", Order:=0)> _

Public Property ID() As String
    Get
        Return Me.ID
    End Get
    Set(value As String)
        Me.ID= value
    End Set
End Property

Is this correct? I am confused as of why it requires a string to be passed if the serialization is saying the type in an integer?

My experience is telling me that on their side i.e. server, that they will cast the string into an integer? correct?

Thanks

like image 706
user3428422 Avatar asked Sep 11 '15 08:09

user3428422


People also ask

What is xsd integer?

xsd:integer. The integer data type accepts any decimal literal value, in which the decimal point is omitted from the lexical representation.

What is xsd string?

Description. The lexical and value spaces of xsd:string are the set of all possible strings composed of any character allowed in a XML 1.0 document without any treatment done on whitespace.

What is integer XML?

The integer data type is used to specify a numeric value without a fractional component. The following is an example of an integer declaration in a schema: <xs:element name="price" type="xs:integer"/>


1 Answers

This turns out to be a surprisingly good question that touches on some subtleties of XSD and how it can be mapped to a .NET language like C# or VB.NET.

The XSD type in your WSDL is of type xs:integer. The XML Schema specification defines xs:integer as unbounded. This in contrast to xs:int, which is bounded to 32 bit.

If your WSDL were to use xs:int instead, you would find the WSDL importer to map it to an Int32.

While implementations are allowed to impose restrictions on xs:integer, for instance by setting the range to be limited by a machine's word size (which can be any size), it is good practice to "Be liberal in what you accept, and conservative in what you send".

Since .NET did not have a native BigInteger type until recently, the only option to be able to send or receive the full range of values of xs:intger is by taking a data type that allows unlimited ranges and they chose string. I may have preferred a datatype that is itself limited to accepting only digits, but there's something to say for the fact that they chose a core datatype.

They apply similar methods to other datatypes, for different reasons. For instance, gYearMonth could have been mapped to a DateTime type, setting the other values to zero when sending. But again they chose string.

Here is a full list of the mappings that Microsoft uses when importing WSDL.

on their side i.e. server, they will cast the string into an integer? correct?

No. It works somewhat as follows (roughly):

  • Your datatype gets validated against the facets of the type, this happens on your side, so you won't be able to sent a value of "john"
  • After validation, the types are serialized using the SOAP protocol
  • The data is sent over the wire
  • The data gets received by whatever system they use that also understands the SOAP protocol
  • The data gets validated against the schema
  • The data gets deserialized in whatever type the programmers on the other side found convenient, depending on tools or whether they manipulated the mapping by hand. If they used WCF, this will be a string again, unless they changed the defaults.
  • The data gets validated against the business logic (if any)
  • Business logic is executed
  • .... some other steps and finally the SOAP response is sent
like image 81
Abel Avatar answered Oct 23 '22 05:10

Abel