Skip to content
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

Fix WSDL for list of anonymous type #1079

Merged
merged 1 commit into from
Aug 16, 2024
Merged
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
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 @@ -20,7 +20,7 @@
public class MetaBodyWriter : BodyWriter
{
private const string FaultSuffix = "Fault";
private static int _namespaceCounter = 1;

Check warning on line 23 in src/SoapCore/Meta/MetaBodyWriter.cs

View workflow job for this annotation

GitHub Actions / Analyze

The field 'MetaBodyWriter._namespaceCounter' is assigned but its value is never used

private readonly ServiceDescription _service;
private readonly string _baseUrl;
Expand Down Expand Up @@ -1240,6 +1240,11 @@
}
else if (toBuild.IsAnonumous)
{
if (string.IsNullOrEmpty(name))
{
name = typeName;
}

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