Skip to content

Commit

Permalink
Merge pull request #1073 from avoronin565/XmlIncludeAttribute
Browse files Browse the repository at this point in the history
Add XmlIncludeAttribute support when generating wsdl.
  • Loading branch information
andersjonsson authored Jun 27, 2024
2 parents 142a64b + ba2fee0 commit c8b2be0
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
40 changes: 40 additions & 0 deletions src/SoapCore.Tests/Wsdl/Services/IXmlIncludeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.ServiceModel;
using System.Xml.Serialization;

namespace SoapCore.Tests.Wsdl.Services
{
[ServiceContract]
internal interface IXmlIncludeService
{
[XmlInclude(typeof(XmlIncludeService.Dog))]
[XmlInclude(typeof(XmlIncludeService.Cat))]
[OperationContract]
XmlIncludeService.Animal Test(XmlIncludeService.Animal value);
}

public class XmlIncludeService : IXmlIncludeService
{
public Animal Test(Animal value)
{
return value;
}

[Serializable]
[XmlInclude(typeof(Dog))]
[XmlInclude(typeof(Cat))]
public class Animal
{
}

[Serializable]
public class Dog : Animal
{
}

[Serializable]
public class Cat : Animal
{
}
}
}
21 changes: 21 additions & 0 deletions src/SoapCore.Tests/Wsdl/WsdlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,27 @@ public void CheckAnonymousServiceKnownType()
Assert.IsNotNull(dogElement);
}

[TestMethod]
public void CheckXmlIncludeTypesASMX()
{
StartService(typeof(XmlIncludeService));
var wsdl = GetWsdlFromAsmx();
StopServer();

var root = XElement.Parse(wsdl);
var dogElement = GetElements(root, _xmlSchema + "complexType").SingleOrDefault(a => a.Attribute("name")?.Value.Equals("Dog") == true);
Assert.IsNotNull(dogElement);

var catElement = GetElements(root, _xmlSchema + "complexType").SingleOrDefault(a => a.Attribute("name")?.Value.Equals("Cat") == true);
Assert.IsNotNull(dogElement);

var animalElement = GetElements(dogElement, _xmlSchema + "extension").SingleOrDefault(a => a.Attribute("base")?.Value.Equals("tns:Animal") == true);
Assert.IsNotNull(animalElement);

animalElement = GetElements(catElement, _xmlSchema + "extension").SingleOrDefault(a => a.Attribute("base")?.Value.Equals("tns:Animal") == true);
Assert.IsNotNull(animalElement);
}

[TestMethod]
public void CheckEmptyMembersServiceASMX()
{
Expand Down
22 changes: 22 additions & 0 deletions src/SoapCore/Meta/MetaBodyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ private void AddTypes(XmlDictionaryWriter writer)
{
bool hasWrittenOutParameters = false;

Type[] additionalTypesForOperation = GetKnownTypesFromMethod(operation.DispatchMethod);
foreach (Type additionalTypeForOperation in additionalTypesForOperation)
{
_complexTypeToBuild.Enqueue(new TypeToBuild(additionalTypeForOperation));
}

// input parameters of operation
writer.WriteStartElement("element", Namespaces.XMLNS_XSD);
writer.WriteAttributeString("name", GetOuterInputElementName(operation));
Expand Down Expand Up @@ -535,6 +541,22 @@ join m in toBuild.GetMembers() on n equals m.Name
writer.WriteEndElement(); // wsdl:types
}

private Type[] GetKnownTypesFromMethod(MethodInfo methodInfo)
{
List<Type> includeTypes = new();

if (methodInfo != null)
{
object[] customAttributes = methodInfo.GetCustomAttributes(typeof(XmlIncludeAttribute), true);
foreach (XmlIncludeAttribute attribute in customAttributes)
{
includeTypes.Add(attribute.Type);
}
}

return includeTypes.ToArray();
}

private void AddMessage(XmlDictionaryWriter writer)
{
foreach (var operation in _service.Operations)
Expand Down
3 changes: 1 addition & 2 deletions src/SoapCore/ServiceBodyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ private void OnWriteXmlSerializerBodyContents(XmlDictionaryWriter writer)
}
else
{
var serializer = CachedXmlSerializer.GetXmlSerializer(resultType, xmlName, xmlNs);

if (_result is Stream)
{
writer.WriteStartElement(_resultName, _serviceNamespace);
Expand Down Expand Up @@ -222,6 +220,7 @@ private void OnWriteXmlSerializerBodyContents(XmlDictionaryWriter writer)
}
else
{
var serializer = CachedXmlSerializer.GetXmlSerializer(resultType, xmlName, xmlNs);
//https://github.com/DigDes/SoapCore/issues/719
serializer.Serialize(writer, _result);
}
Expand Down

0 comments on commit c8b2be0

Please sign in to comment.