Skip to content

Commit

Permalink
Merge pull request #1079 from paramamue/develop
Browse files Browse the repository at this point in the history
Fix WSDL for list of anonymous type
  • Loading branch information
andersjonsson authored Aug 16, 2024
2 parents c8b2be0 + ffac338 commit 815c9a0
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/SoapCore.Tests/Wsdl/Services/ComplexTypeAnonymous.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace SoapCore.Tests.Wsdl.Services
{
[XmlType(AnonymousType = true)]
public class ComplexTypeAnonymous
{
public int IntProperty { get; set; }
[XmlElement(ElementName = "stringprop")]
public string StringProperty { get; set; }
[XmlElement(ElementName = "mybytes")]
public byte[] ByteArrayProperty { get; set; }

public Guid MyGuid { get; set; }

public List<string> StringList { get; set; }

public List<int> IntList { get; set; }
}
}
21 changes: 21 additions & 0 deletions src/SoapCore.Tests/Wsdl/Services/IComplexAnonymousListService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace SoapCore.Tests.Wsdl.Services
{
[ServiceContract]
public interface IComplexAnonymousListService
{
[OperationContract]
List<ComplexTypeAnonymous> Test();
}

public class ComplexAnonymousListService : IComplexAnonymousListService
{
public List<ComplexTypeAnonymous> Test() => throw new NotImplementedException();
}
}
47 changes: 47 additions & 0 deletions src/SoapCore.Tests/Wsdl/WsdlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,52 @@ public void CheckComplexBaseTypeServiceWsdl()
Assert.IsNotNull(listDerivedType);
}

[DataTestMethod]
[DataRow(SoapSerializer.XmlSerializer)]
public async Task CheckComplexAnonymousTypeListWsdl(SoapSerializer soapSerializer)
{
var wsdl = await GetWsdlFromMetaBodyWriter<ComplexAnonymousListService>(soapSerializer);
Trace.TraceInformation(wsdl);
Assert.IsNotNull(wsdl);

var root = XElement.Parse(wsdl);

// Check complexType exists for xmlserializer meta
var testResultElement = GetElements(root, _xmlSchema + "element").SingleOrDefault(a => a.Attribute("type") != null && a.Attribute("name")?.Value.Equals("TestResult") == true);
Assert.IsNotNull(testResultElement);

// Now check if we can match the array type up with it's declaration
var split = testResultElement.Attribute("type").Value.Split(':');
var typeNamespace = testResultElement.GetNamespaceOfPrefix(split[0]);

var matchingSchema = GetElements(root, _xmlSchema + "schema").Where(schema => schema.Attribute("targetNamespace")?.Value.Equals(typeNamespace.NamespaceName) == true);
Assert.IsTrue(matchingSchema.Count() > 0);

var matched = false;
XElement matchingComplexType = null;
foreach (var schema in matchingSchema)
{
matchingComplexType = GetElements(schema, _xmlSchema + "complexType").SingleOrDefault(a => a.Attribute("name")?.Value.Equals(split[1]) == true);
if (matchingComplexType != null)
{
matched = true;
}
}

Assert.IsTrue(matched);

// The complex type is an array with a single element, which is an anonymous complex type
var arrayElement = matchingComplexType.Element(_xmlSchema + "sequence")?.Element(_xmlSchema + "element");
Assert.IsNotNull(arrayElement);

// The element needs a name and a complex type
var nameAttribute = arrayElement.Attribute("name");
Assert.IsFalse(string.IsNullOrEmpty(nameAttribute.Value));

var arrayElementType = arrayElement.Element(_xmlSchema + "complexType");
Assert.IsNotNull(arrayElementType);
}

[TestCleanup]
public void StopServer()
{
Expand Down Expand Up @@ -1281,6 +1327,7 @@ private async Task<string> GetWsdlFromMetaBodyWriter<T>(SoapSerializer serialize
var service = new ServiceDescription(typeof(T), false);
var baseUrl = "http://tempuri.org/";
var xmlNamespaceManager = Namespaces.CreateDefaultXmlNamespaceManager(useMicrosoftGuid);
xmlNamespaceManager.AddNamespace("tns", service.GeneralContract.Namespace);
var defaultBindingName = !string.IsNullOrWhiteSpace(bindingName) ? bindingName : "BasicHttpBinding";
var bodyWriter = serializer == SoapSerializer.DataContractSerializer
? new MetaWCFBodyWriter(service, baseUrl, defaultBindingName, false, new[] { new SoapBindingInfo(MessageVersion.None, bindingName, portName) }, new DefaultWsdlOperationNameGenerator()) as BodyWriter
Expand Down
5 changes: 5 additions & 0 deletions src/SoapCore/Meta/MetaBodyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,11 @@ private void AddSchemaType(XmlDictionaryWriter writer, TypeToBuild toBuild, stri
}
else if (toBuild.IsAnonumous)
{
if (string.IsNullOrEmpty(name))
{
name = typeName;
}

writer.WriteAttributeString("name", name);
WriteQualification(writer, isUnqualified);
AddSchemaComplexType(writer, newTypeToBuild);
Expand Down

0 comments on commit 815c9a0

Please sign in to comment.