Skip to content

Commit

Permalink
Merge pull request #1076 from navozenko/system-import
Browse files Browse the repository at this point in the history
Do not import target namespace
  • Loading branch information
andersjonsson authored Aug 16, 2024
2 parents 815c9a0 + ac579a3 commit 42169a0
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 9 deletions.
20 changes: 20 additions & 0 deletions src/SoapCore.Tests/Wsdl/Services/ISystemImportService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.ServiceModel;

namespace SoapCore.Tests.Wsdl.Services
{
[ServiceContract]
public interface ISystemImportService
{
[OperationContract]
ComplexType GetValue();
}

public class SystemImportService : ISystemImportService
{
public ComplexType GetValue()
{
throw new NotImplementedException();
}
}
}
50 changes: 50 additions & 0 deletions src/SoapCore.Tests/Wsdl/WsdlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,56 @@ public void CheckSystemTypes()
Assert.AreEqual(element.Attributes["type"]?.Value, "xs:anyURI");
}

[TestMethod]
public void CheckSystemAndArraysImport()
{
StartService(typeof(SystemImportService));
var wsdl = GetWsdl();
StopServer();

var root = new XmlDocument();
root.LoadXml(wsdl);

var nsmgr = new XmlNamespaceManager(root.NameTable);
nsmgr.AddNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");

var customNamespace = "http://schemas.datacontract.org/2004/07/SoapCore.Tests.Wsdl.Services";
var systemNamespace = "http://schemas.datacontract.org/2004/07/System";
var arraysNamespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";

// Schema with custom target namespace
var schemaPath = $"/wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='{customNamespace}']";
var schemaElement = root.SelectSingleNode(schemaPath, nsmgr);
var systemImportElement = schemaElement.SelectSingleNode($"xs:import[@namespace='{systemNamespace}']", nsmgr);
var arraysImportElement = schemaElement.SelectSingleNode($"xs:import[@namespace='{arraysNamespace}']", nsmgr);

Assert.IsNotNull(schemaElement);
Assert.IsNotNull(systemImportElement);
Assert.IsNotNull(arraysImportElement);

// Schema with system target namespace
schemaPath =
$"/wsdl:definitions/wsdl:types" +
$"/xs:schema[@targetNamespace='{systemNamespace}']" +
$"/xs:complexType[@name='ArrayOfByte']" +
$"/..";

schemaElement = root.SelectSingleNode(schemaPath, nsmgr);
systemImportElement = schemaElement.SelectSingleNode($"xs:import[@namespace='{systemNamespace}']", nsmgr);
arraysImportElement = schemaElement.SelectSingleNode($"xs:import[@namespace='{arraysNamespace}']", nsmgr);

Assert.IsNull(systemImportElement);
Assert.IsNotNull(arraysImportElement);

// Schema with arrays target namespace
schemaPath = $"/wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='{arraysNamespace}']";
schemaElement = root.SelectSingleNode(schemaPath, nsmgr);
arraysImportElement = schemaElement.SelectSingleNode($"xs:import[@namespace='{arraysNamespace}']", nsmgr);

Assert.IsNull(arraysImportElement);
}

[TestMethod]
public void CheckStreamDeclaration()
{
Expand Down
25 changes: 16 additions & 9 deletions src/SoapCore/Meta/MetaWCFBodyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,23 +661,30 @@ private void AddComplexTypes(XmlDictionaryWriter writer)

foreach (var types in groupedByNamespace.Distinct())
{
var targetNamespace = GetModelNamespace(types.Key);
writer.WriteStartElement("xs", "schema", Namespaces.XMLNS_XSD);
writer.WriteAttributeString("elementFormDefault", "qualified");
writer.WriteAttributeString("targetNamespace", GetModelNamespace(types.Key));
writer.WriteAttributeString("targetNamespace", targetNamespace);
writer.WriteXmlnsAttribute("xs", Namespaces.XMLNS_XSD);
writer.WriteXmlnsAttribute("tns", GetModelNamespace(types.Key));
writer.WriteXmlnsAttribute("tns", targetNamespace);
writer.WriteXmlnsAttribute("ser", Namespaces.SERIALIZATION_NS);

_namespaceCounter = 1;
_schemaNamespace = GetModelNamespace(types.Key);
_schemaNamespace = targetNamespace;

writer.WriteStartElement("xs", "import", Namespaces.XMLNS_XSD);
writer.WriteAttributeString("namespace", Namespaces.SYSTEM_NS);
writer.WriteEndElement();
if (targetNamespace != Namespaces.SYSTEM_NS)
{
writer.WriteStartElement("xs", "import", Namespaces.XMLNS_XSD);
writer.WriteAttributeString("namespace", Namespaces.SYSTEM_NS);
writer.WriteEndElement();
}

writer.WriteStartElement("xs", "import", Namespaces.XMLNS_XSD);
writer.WriteAttributeString("namespace", Namespaces.ARRAYS_NS);
writer.WriteEndElement();
if (targetNamespace != Namespaces.ARRAYS_NS)
{
writer.WriteStartElement("xs", "import", Namespaces.XMLNS_XSD);
writer.WriteAttributeString("namespace", Namespaces.ARRAYS_NS);
writer.WriteEndElement();
}

foreach (var type in types.Value.Distinct(new TypesComparer(GetTypeName)))
{
Expand Down

0 comments on commit 42169a0

Please sign in to comment.