Skip to content

Commit

Permalink
Merge pull request #983 from edgridin/bugfix/XmlSerializer_Wrong_Cust…
Browse files Browse the repository at this point in the history
…om_ElementName

bugfix: complex type with a member of a complex type decorated per XmlElementAttribute with custom name breaks custom names of the members specified per XmlElementAttributes of the sub-complex type
  • Loading branch information
andersjonsson authored Jan 2, 2024
2 parents 7559a0e + f6b9f90 commit 6dfd59e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/SoapCore.Tests/Wsdl/Services/ComplexComplexType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Xml.Serialization;

namespace SoapCore.Tests.Wsdl.Services
{
public class ComplexComplexType
{
[XmlElement(ElementName = "complex")]
public ComplexType ComplexType { get; set; }
}
}
2 changes: 0 additions & 2 deletions src/SoapCore.Tests/Wsdl/Services/ComplexType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace SoapCore.Tests.Wsdl.Services
Expand Down
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 IComplexComplexTypeWithCustomXmlNamesService
{
[OperationContract]
ComplexComplexType Method(out string message);
}

public class ComplexComplexTypeWithCustomXmlNamesService : IComplexComplexTypeWithCustomXmlNamesService
{
public ComplexComplexType Method(out string message)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;

namespace SoapCore.Tests.Wsdl.Services
{
Expand Down
44 changes: 44 additions & 0 deletions src/SoapCore.Tests/Wsdl/WsdlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,50 @@ public async Task CheckComplexTypeAndOutParameterWsdl(SoapSerializer soapSeriali
Assert.IsNotNull(testElementMessage);
}

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

var root = XElement.Parse(wsdl);

//loading definition of ComplexComplexType
var testComplexComplexType = GetElements(root, _xmlSchema + "complexType").SingleOrDefault(a => a.Attribute("name")?.Value == "ComplexComplexType");
Assert.IsNotNull(testComplexComplexType);

//checking sequence to be there
var testSequenceOfComplexComplexType = GetElements(testComplexComplexType, _xmlSchema + "sequence").SingleOrDefault();
Assert.IsNotNull(testSequenceOfComplexComplexType);

//checking custom name specified per XmlElementAttribute is used
var testElementOfComplexComplexType = GetElements(testSequenceOfComplexComplexType, _xmlSchema + "element").SingleOrDefault(a => a.Attribute("name").Value == "complex");
Assert.IsNotNull(testElementOfComplexComplexType);

//loading definition of ComplexType
var testComplexType = GetElements(root, _xmlSchema + "complexType").SingleOrDefault(a => a.Attribute("name")?.Value == "ComplexType");
Assert.IsNotNull(testComplexType);

//checking sequence to be there
var testSequenceOfComplexType = GetElements(testComplexType, _xmlSchema + "sequence").SingleOrDefault();
Assert.IsNotNull(testSequenceOfComplexType);

//checking custom names specified per XmlElementAttribute are used
var testElementWithCustomName = GetElements(testSequenceOfComplexType, _xmlSchema + "element").SingleOrDefault(a => a.Attribute("name").Value == "stringprop");
Assert.IsNotNull(testElementWithCustomName);

testElementWithCustomName = GetElements(testSequenceOfComplexType, _xmlSchema + "element").SingleOrDefault(a => a.Attribute("name").Value == "mybytes");
Assert.IsNotNull(testElementWithCustomName);

//checking both properties without custom names to use the same names as properties in the ComplexType class
var testElementWithDefaultName = GetElements(testSequenceOfComplexType, _xmlSchema + "element").SingleOrDefault(a => a.Attribute("name").Value == "IntProperty");
Assert.IsNotNull(testElementWithDefaultName);

testElementWithDefaultName = GetElements(testSequenceOfComplexType, _xmlSchema + "element").SingleOrDefault(a => a.Attribute("name").Value == "MyGuid");
Assert.IsNotNull(testElementWithDefaultName);
}

[DataTestMethod]
[DataRow(SoapSerializer.XmlSerializer)]
public async Task CheckOccuranceOfStringType(SoapSerializer soapSerializer)
Expand Down
2 changes: 1 addition & 1 deletion src/SoapCore/Meta/MetaBodyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ private void AddSchemaTypePropertyOrField(XmlDictionaryWriter writer, MemberInfo
}
}

AddSchemaType(writer, toBuild, parentTypeToBuild.ChildElementName ?? elementNameFromAttribute ?? member.Name, isArray: createListWithoutProxyType, isListWithoutWrapper: createListWithoutProxyType, isUnqualified: isUnqualified, defaultValue: defaultValue);
AddSchemaType(writer, toBuild, elementNameFromAttribute ?? member.Name ?? parentTypeToBuild.ChildElementName, isArray: createListWithoutProxyType, isListWithoutWrapper: createListWithoutProxyType, isUnqualified: isUnqualified, defaultValue: defaultValue);
}
}

Expand Down

0 comments on commit 6dfd59e

Please sign in to comment.