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

support for XmlEnumAttribute specifiying custom names for enum members #986

Merged
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
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
Loading