From ac579a36a49920f95c5360904ae6f28be0e8f63e Mon Sep 17 00:00:00 2001 From: Sergey Navozenko Date: Tue, 9 Jul 2024 20:08:55 +0300 Subject: [PATCH] Do not import target namespace --- .../Wsdl/Services/ISystemImportService.cs | 20 ++++++++ src/SoapCore.Tests/Wsdl/WsdlTests.cs | 50 +++++++++++++++++++ src/SoapCore/Meta/MetaWCFBodyWriter.cs | 25 ++++++---- 3 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 src/SoapCore.Tests/Wsdl/Services/ISystemImportService.cs diff --git a/src/SoapCore.Tests/Wsdl/Services/ISystemImportService.cs b/src/SoapCore.Tests/Wsdl/Services/ISystemImportService.cs new file mode 100644 index 00000000..b1473fe3 --- /dev/null +++ b/src/SoapCore.Tests/Wsdl/Services/ISystemImportService.cs @@ -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(); + } + } +} diff --git a/src/SoapCore.Tests/Wsdl/WsdlTests.cs b/src/SoapCore.Tests/Wsdl/WsdlTests.cs index d77921be..403e70b2 100644 --- a/src/SoapCore.Tests/Wsdl/WsdlTests.cs +++ b/src/SoapCore.Tests/Wsdl/WsdlTests.cs @@ -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() { diff --git a/src/SoapCore/Meta/MetaWCFBodyWriter.cs b/src/SoapCore/Meta/MetaWCFBodyWriter.cs index d34e5264..2a0ed52f 100644 --- a/src/SoapCore/Meta/MetaWCFBodyWriter.cs +++ b/src/SoapCore/Meta/MetaWCFBodyWriter.cs @@ -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))) {