Skip to content

Commit

Permalink
Merge pull request #986 from edgridin/feature/XmlEnumAttribute_Support
Browse files Browse the repository at this point in the history
support for XmlEnumAttribute specifiying custom names for enum members
  • Loading branch information
andersjonsson committed Jan 8, 2024
2 parents f950e2e + b5de0fb commit d714e11
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/SoapCore.Tests/Wsdl/Services/EnumWithCustomNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Xml.Serialization;

namespace SoapCore.Tests.Wsdl.Services
{
public enum EnumWithCustomNames
{
[XmlEnum("F")]
FirstEnumMember,

[XmlEnum("S")]
SecondEnumMember,

ThirdEnumMember
}
}
20 changes: 20 additions & 0 deletions src/SoapCore.Tests/Wsdl/Services/IEnumWithCustomNamesService.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 IEnumWithCustomNamesService
{
[OperationContract]
EnumWithCustomNames? GetEnum(string text);
}

public class EnumWithCustomNamesService : IEnumWithCustomNamesService
{
public EnumWithCustomNames? GetEnum(string text)
{
throw new NotImplementedException();
}
}
}
30 changes: 30 additions & 0 deletions src/SoapCore.Tests/Wsdl/WsdlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,36 @@ public async Task CheckComplexComplexTypeWithCustomXmlNamesWsdl()
Assert.IsNotNull(testElementWithDefaultName);
}

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

var root = XElement.Parse(wsdl);

//loading definition of EnumWithCustomNames
var enumWithCustomNamesElement = GetElements(root, _xmlSchema + "simpleType").FirstOrDefault(a => a.Attribute("name")?.Value.Equals("EnumWithCustomNames") == true);
Assert.IsNotNull(enumWithCustomNamesElement);

//checking restriction to be there
var testRestrictionOfEnumWithCustomNames = GetElements(enumWithCustomNamesElement, _xmlSchema + "restriction").SingleOrDefault();
Assert.IsNotNull(testRestrictionOfEnumWithCustomNames);

//checking enumeration elements to be there
var testEnumerationElements = GetElements(testRestrictionOfEnumWithCustomNames, _xmlSchema + "enumeration").ToList();
Assert.IsNotNull(testEnumerationElements);
Assert.AreEqual(3, testEnumerationElements.Count);

//checking custom names specified per XmlEnumAttribute are used
Assert.IsNotNull(testEnumerationElements.SingleOrDefault(e => e.FirstAttribute?.Value == "F"));
Assert.IsNotNull(testEnumerationElements.SingleOrDefault(e => e.FirstAttribute?.Value == "S"));

//checking default name specified by enum member
Assert.IsNotNull(testEnumerationElements.SingleOrDefault(e => e.FirstAttribute?.Value == "ThirdEnumMember"));
}

[DataTestMethod]
[DataRow(SoapSerializer.XmlSerializer)]
public async Task CheckOccuranceOfStringType(SoapSerializer soapSerializer)
Expand Down
23 changes: 21 additions & 2 deletions src/SoapCore/Meta/MetaBodyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,29 @@ private void AddTypes(XmlDictionaryWriter writer)
writer.WriteStartElement("restriction", Namespaces.XMLNS_XSD);
writer.WriteAttributeString("base", $"{_xmlNamespaceManager.LookupPrefix(Namespaces.XMLNS_XSD)}:string");

foreach (var value in Enum.GetValues(toBuild))
var membersWithCustomNames = from n in Enum.GetNames(toBuild)
join m in toBuild.GetMembers() on n equals m.Name
let ca = m.CustomAttributes.FirstOrDefault(ca => ca.AttributeType == typeof(XmlEnumAttribute))
select new
{
m.Name,
CustomName =
(ca?
.ConstructorArguments?
.FirstOrDefault()
.Value
??
ca?
.NamedArguments?
.FirstOrDefault(a => a.MemberName == "Name")
.TypedValue
.Value)?
.ToString()
};
foreach (var member in membersWithCustomNames)
{
writer.WriteStartElement("enumeration", Namespaces.XMLNS_XSD);
writer.WriteAttributeString("value", value.ToString());
writer.WriteAttributeString("value", member.CustomName ?? member.Name);
writer.WriteEndElement(); // enumeration
}

Expand Down

0 comments on commit d714e11

Please sign in to comment.