Skip to content

[dotnet-svcutil] Fully qualify XmlSchemaProvider to ensure correct resolution #5796

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ internal static Type TypeOfXmlSchemaProviderAttribute
get
{
if (s_typeOfXmlSchemaProviderAttribute == null)
s_typeOfXmlSchemaProviderAttribute = typeof(XmlSchemaProviderAttribute);
s_typeOfXmlSchemaProviderAttribute = typeof(System.Xml.Serialization.XmlSchemaProviderAttribute);
return s_typeOfXmlSchemaProviderAttribute;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private static bool InvokeSchemaProviderMethod(Type clrType, XmlSchemaSet schema
return false;
}

XmlSchemaProviderAttribute provider = (XmlSchemaProviderAttribute)attrs[0];
System.Xml.Serialization.XmlSchemaProviderAttribute provider = (System.Xml.Serialization.XmlSchemaProviderAttribute)attrs[0];
if (provider.IsAny)
{
xsdType = CreateAnyElementType();
Expand All @@ -55,11 +55,11 @@ private static bool InvokeSchemaProviderMethod(Type clrType, XmlSchemaSet schema
}
else
{
MethodInfo getMethod = clrType.GetMethod(methodName, /*BindingFlags.DeclaredOnly |*/ BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, new Type[] { typeof(XmlSchemaSet) });
MethodInfo getMethod = clrType.GetMethod(methodName, /*BindingFlags.DeclaredOnly |*/ BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, new Type[] { typeof(System.Xml.Schema.XmlSchemaSet) });
if (getMethod == null)
throw /*System.Runtime.Serialization.*/DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(string.Format(SRSerialization.MissingGetSchemaMethod, DataContract.GetClrTypeFullName(clrType), methodName)));

if (!(Globals.TypeOfXmlQualifiedName.IsAssignableFrom(getMethod.ReturnType)))
if (!typeof(System.Xml.XmlQualifiedName).IsAssignableFrom(getMethod.ReturnType))
throw /*System.Runtime.Serialization.*/DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(string.Format(SRSerialization.InvalidReturnTypeOnGetSchemaMethod, DataContract.GetClrTypeFullName(clrType), methodName, DataContract.GetClrTypeFullName(getMethod.ReturnType), DataContract.GetClrTypeFullName(Globals.TypeOfXmlQualifiedName))));

object typeInfo = getMethod.Invoke(null, new object[] { schemas });
Expand All @@ -78,7 +78,8 @@ private static bool InvokeSchemaProviderMethod(Type clrType, XmlSchemaSet schema
}
else
{
stableName = (XmlQualifiedName)typeInfo;
var systemName = (System.Xml.XmlQualifiedName)typeInfo;
stableName = new XmlQualifiedName(systemName.Name, systemName.Namespace);
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"version": "99.99.99",
"options": {
"inputs": [
"../../../../../../src/dotnet-svcutil/lib/tests/TestCases/TypeReuse/TypeReuseIXmlSerializable.wsdl"
"../../../../../../src/dotnet-svcutil/lib/tests/TestCases/ReuseIXmlSerializableType/TypeReuseIXmlSerializable.wsdl"
],
"namespaceMappings": [
"*, ReuseIXmlSerializableType_NS"
],
"outputFile": "Reference.cs",
"references": [
"$testCasesPath$//TypeReuse//CommonTypes.dll"
"$testCasesPath$//ReuseIXmlSerializableType//CommonTypes//CommonTypes.csproj"
],
"targetFramework": "N.N",
"typeReuseMode": "Specified"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Runtime.Serialization;

namespace CommonTypes
{
[DataContract]
public struct AnotherSharedType
{
[DataMember]
public int SomeProperty { get; set; }

[DataMember]
public CustomSerializableType SomeXmlSerializableType { get; set; }

[DataMember]
public CustomSerializableTypeWithNs SomeXmlSerializableTypeWithNs { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Runtime.Serialization.Primitives" Version="4.1.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Globalization;
using System.Numerics;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace CommonTypes
{
public struct CustomSerializableType : IXmlSerializable
{
public BigInteger TestValue { get; set; }

public XmlSchema GetSchema()
{
XmlSchema schema = new XmlSchema()
{
Id = "CustomSerializableTypeSchema"
};
XmlSchemaSimpleType schemaSimpleType1 = new XmlSchemaSimpleType();
schemaSimpleType1.Name = "BigIntegerString";
XmlSchemaSimpleType schemaSimpleType2 = schemaSimpleType1;
XmlSchemaSimpleTypeRestriction simpleTypeRestriction = new XmlSchemaSimpleTypeRestriction()
{
BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
};
schemaSimpleType2.Content = (XmlSchemaSimpleTypeContent) simpleTypeRestriction;
schema.Items.Add((XmlSchemaObject) schemaSimpleType2);
return schema;
}

public void ReadXml(XmlReader reader)
{
reader.ReadStartElement();
reader.ReadStartElement();
this.TestValue = BigInteger.Parse(reader.ReadContentAsString());
reader.ReadEndElement();
reader.ReadEndElement();
}

public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement("BigIntegerString");
writer.WriteValue(this.TestValue.ToString((IFormatProvider) CultureInfo.InvariantCulture));
writer.WriteEndElement();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Globalization;
using System.Numerics;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace CommonTypes
{
[XmlSchemaProvider("GetXmlSchema")]
public struct CustomSerializableTypeWithNs : IXmlSerializable
{
public BigInteger TestValue { get; set; }

// This method is defined by the XmlSchemaProvider attributes and
// called by the SchemaExporter::InvokeSchemaProviderMethod to get
// the custom Xml Qualified name for the type mapping.
public static XmlQualifiedName GetXmlSchema(XmlSchemaSet xss)
{
return new XmlQualifiedName("CustomSerializableTypeWithNs", "http://www.example.com/Schemas/CommonTypes");
}

public XmlSchema GetSchema()
{
XmlSchema schema = new XmlSchema()
{
Id = "CustomSerializableTypeSchema"
};
XmlSchemaSimpleType schemaSimpleType1 = new XmlSchemaSimpleType();
schemaSimpleType1.Name = "BigIntegerString";
XmlSchemaSimpleType schemaSimpleType2 = schemaSimpleType1;
XmlSchemaSimpleTypeRestriction simpleTypeRestriction = new XmlSchemaSimpleTypeRestriction()
{
BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
};
schemaSimpleType2.Content = (XmlSchemaSimpleTypeContent)simpleTypeRestriction;
schema.Items.Add((XmlSchemaObject)schemaSimpleType2);
return schema;
}

public void ReadXml(XmlReader reader)
{
reader.ReadStartElement();
reader.ReadStartElement();
this.TestValue = BigInteger.Parse(reader.ReadContentAsString());
reader.ReadEndElement();
reader.ReadEndElement();
}

public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement("BigIntegerString");
writer.WriteValue(this.TestValue.ToString((IFormatProvider)CultureInfo.InvariantCulture));
writer.WriteEndElement();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Runtime.Serialization;

namespace CommonTypes
{
[DataContract]
public struct SomeSharedType
{
[DataMember]
public int SomeProperty { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
<xs:attribute name="Id" type="xs:ID"/>
<xs:attribute name="Ref" type="xs:IDREF"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/CommonTypes" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/CommonTypes">
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/CommonTypes" xmlns:ctex="http://www.example.com/Schemas/CommonTypes" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/CommonTypes">
<xs:import namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
<xs:complexType name="SomeSharedType">
<xs:annotation>
Expand All @@ -115,6 +115,7 @@
<xs:sequence>
<xs:element minOccurs="0" name="SomeProperty" type="xs:int"/>
<xs:element minOccurs="0" name="SomeXmlSerializableType" type="tns:CustomSerializableType"/>
<xs:element minOccurs="0" name="SomeXmlSerializableTypeWithNs" type="ctex:CustomSerializableTypeWithNs"/>
</xs:sequence>
</xs:complexType>
<xs:element name="AnotherSharedType" nillable="true" type="tns:AnotherSharedType"/>
Expand All @@ -130,6 +131,20 @@
</xs:complexType>
<xs:element name="CustomSerializableType" nillable="true" type="tns:CustomSerializableType"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.com/Schemas/CommonTypes" elementFormDefault="qualified" targetNamespace="http://www.example.com/Schemas/CommonTypes">
<xs:import namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
<xs:complexType name="CustomSerializableTypeWithNs">
<xs:annotation>
<xs:appinfo>
<IsValueType xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</IsValueType>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:any namespace=""/>
</xs:sequence>
</xs:complexType>
<xs:element name="CustomSerializableTypeWithNs" nillable="true" type="tns:CustomSerializableTypeWithNs"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" id="CustomSerializableTypeSchema">
<xs:simpleType name="BigIntegerString">
<xs:restriction base="xs:string"/>
Expand Down
Binary file not shown.
5 changes: 3 additions & 2 deletions src/dotnet-svcutil/lib/tests/src/GlobalToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,10 @@ public void ReuseIXmlSerializableType()
TestFixture();
InitializeGlobal(this_TestCaseName);

var uri = Path.Combine(g_TestCasesDir, "TypeReuse", "TypeReuseIXmlSerializable.wsdl");
var refs = Path.Combine(g_TestCasesDir, "TypeReuse", "CommonTypes.dll");
var uri = Path.Combine(g_TestCasesDir, "ReuseIXmlSerializableType", "TypeReuseIXmlSerializable.wsdl");
var refs = Path.Combine(g_TestCasesDir, "ReuseIXmlSerializableType", "CommonTypes", "CommonTypes.csproj");
var options = $"{uri} -r {refs} -nl -v minimal -n \"\"*,{this_TestCaseName}_NS\"\"";

TestGlobalSvcutil(options);
}
}
Expand Down
Loading