Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit cbf0f73

Browse files
authored
Add Tests covering WCF Soap Scenario. (#19258)
Ref dotnet/wcf#1549
1 parent e91a95d commit cbf0f73

File tree

4 files changed

+124
-28
lines changed

4 files changed

+124
-28
lines changed

src/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,28 +1056,28 @@ private object WriteArray(ArrayMapping arrayMapping, bool readOnly, bool isNulla
10561056
}
10571057

10581058
TypeDesc td = arrayMapping.TypeDesc;
1059-
if (td.IsEnumerable || td.IsCollection)
1059+
if (rre != null)
10601060
{
1061-
if (rre != null)
1061+
if (td.IsEnumerable || td.IsCollection)
10621062
{
10631063
WriteAddCollectionFixup(member.GetSource, member.Source, rre, td, readOnly);
1064+
1065+
// member.Source has been set at this point.
1066+
// Setting the source to no-op to avoid setting the
1067+
// source again.
1068+
member.Source = NoopAction;
10641069
}
1065-
}
1066-
else
1067-
{
1068-
if (member == null)
1070+
else
10691071
{
1070-
throw new InvalidOperationException(SR.Format(SR.XmlInternalError));
1071-
}
1072+
if (member == null)
1073+
{
1074+
throw new InvalidOperationException(SR.Format(SR.XmlInternalError));
1075+
}
10721076

1073-
member.Source(rre);
1077+
member.Source(rre);
1078+
}
10741079
}
10751080

1076-
// member.Source has been set at this point.
1077-
// Setting the source to no-op to avoid setting the
1078-
// source again.
1079-
member.Source = NoopAction;
1080-
10811081
o = rre;
10821082
}
10831083
else
@@ -1360,10 +1360,20 @@ private object WriteEncodedStructMethod(StructMapping structMapping)
13601360
{
13611361
MemberMapping mapping = mappings[i];
13621362
var member = new Member(mapping);
1363-
member.Source = (value) =>
1363+
1364+
TypeDesc td = member.Mapping.TypeDesc;
1365+
if (td.IsCollection || td.IsEnumerable)
13641366
{
1365-
SetMemberValue(o, value, member.Mapping.MemberInfo);
1366-
};
1367+
member.Source = (value) => WriteAddCollectionFixup(o, member, value);
1368+
}
1369+
else if (!member.Mapping.ReadOnly)
1370+
{
1371+
member.Source = (value) => SetMemberValue(o, value, member.Mapping.MemberInfo);
1372+
}
1373+
else
1374+
{
1375+
member.Source = NoopAction;
1376+
}
13671377

13681378
members[i] = member;
13691379
}
@@ -1426,7 +1436,6 @@ private XmlSerializationFixupCallback CreateWriteFixupMethod(Member[] members)
14261436
return (fixupObject) =>
14271437
{
14281438
var fixup = (Fixup)fixupObject;
1429-
object o = fixup.Source;
14301439
string[] ids = fixup.Ids;
14311440
foreach (Member member in members)
14321441
{
@@ -1436,15 +1445,7 @@ private XmlSerializationFixupCallback CreateWriteFixupMethod(Member[] members)
14361445
if (ids[fixupIndex] != null)
14371446
{
14381447
var memberValue = GetTarget(ids[fixupIndex]);
1439-
TypeDesc td = member.Mapping.TypeDesc;
1440-
if (td.IsCollection || td.IsEnumerable)
1441-
{
1442-
WriteAddCollectionFixup(o, member, memberValue);
1443-
}
1444-
else
1445-
{
1446-
SetMemberValue(o, memberValue, member.Mapping.Name);
1447-
}
1448+
member.Source(memberValue);
14481449
}
14491450
}
14501451
}

src/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ public static MemberInfo GetMember(Type declaringType, string memberName)
13871387
MemberInfo[] memberInfos = declaringType.GetMember(memberName);
13881388
if (memberInfos == null || memberInfos.Length == 0)
13891389
{
1390-
throw new InvalidOperationException(SR.Format(SR.XmlInternalError, memberName));
1390+
throw new InvalidOperationException(SR.Format(SR.XmlInternalErrorDetails, $"Could not find member named {memberName} of type {declaringType.ToString()}"));
13911391
}
13921392

13931393
MemberInfo memberInfo = memberInfos[0];

src/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4010,6 +4010,20 @@ public static void XmlMembersMapping_Soap_SimpleType()
40104010
Assert.Equal(getDataRequestBodyValue.value, getDataRequestBodyActual.value);
40114011
}
40124012

4013+
[Fact]
4014+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "dotnet/corefx #18964")]
4015+
public static void XmlMembersMapping_Soap_CompositeType()
4016+
{
4017+
string memberName = "GetDataUsingDataContract";
4018+
var requestBodyValue = new CompositeTypeForXmlMembersMapping() { BoolValue = true, StringValue = "foo" };
4019+
var requestBodyActual = RoundTripWithXmlMembersMappingSoap<CompositeTypeForXmlMembersMapping>(requestBodyValue, memberName,
4020+
"<?xml version=\"1.0\"?>\r\n<q1:CompositeTypeForXmlMembersMapping xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" id=\"id1\" xmlns:q1=\"http://tempuri.org/\">\r\n <BoolValue xsi:type=\"xsd:boolean\">true</BoolValue>\r\n <StringValue xsi:type=\"xsd:string\">foo</StringValue>\r\n</q1:CompositeTypeForXmlMembersMapping>");
4021+
4022+
Assert.NotNull(requestBodyActual);
4023+
Assert.Equal(requestBodyValue.BoolValue, requestBodyActual.BoolValue);
4024+
Assert.Equal(requestBodyValue.StringValue, requestBodyActual.StringValue);
4025+
}
4026+
40134027
[Fact]
40144028
public static void XmlMembersMapping_Soap_PrimitiveValue_HasWrapperElement()
40154029
{
@@ -4232,6 +4246,74 @@ public static void Xml_Soap_TypeWithReadOnlyMyCollectionProperty()
42324246
Assert.True(value.Collection.SequenceEqual(actual.Collection));
42334247
}
42344248

4249+
[Fact]
4250+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "dotnet/corefx #18964")]
4251+
public static void XmlMembersMapping_Soap_SoapComplexType()
4252+
{
4253+
string memberName = "EchoComositeTypeXmlSerializerFormatSoapResult";
4254+
var requestBodyValue = new SoapComplexType() { BoolValue = true, StringValue = "hello" };
4255+
4256+
string baseline = "<root><q1:EchoComositeTypeXmlSerializerFormatSoapResponse xmlns:q1=\"http://tempuri.org/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><EchoComositeTypeXmlSerializerFormatSoapResult href=\"#id1\"/></q1:EchoComositeTypeXmlSerializerFormatSoapResponse><q2:SoapComplexType id=\"id1\" a:type=\"q2:SoapComplexType\" xmlns:q2=\"http://tempuri.org/encoded\" xmlns:a=\"http://www.w3.org/2001/XMLSchema-instance\"><BoolValue a:type=\"q3:boolean\" xmlns:q3=\"http://www.w3.org/2001/XMLSchema\">true</BoolValue><StringValue a:type=\"q4:string\" xmlns:q4=\"http://www.w3.org/2001/XMLSchema\">hello</StringValue></q2:SoapComplexType></root>";
4257+
string ns = s_defaultNs;
4258+
string wrapperName = "EchoComositeTypeXmlSerializerFormatSoapResponse";
4259+
4260+
object[] value = new object[] { requestBodyValue };
4261+
XmlReflectionMember member = GetReflectionMember<SoapComplexType>(memberName, ns);
4262+
member.SoapAttributes.SoapElement = new SoapElementAttribute(memberName);
4263+
var members = new XmlReflectionMember[] { member };
4264+
4265+
var importer = new SoapReflectionImporter(null, "http://tempuri.org/encoded");
4266+
var membersMapping = importer.ImportMembersMapping(wrapperName, ns, members, hasWrapperElement: true, writeAccessors: true);
4267+
var serializer = XmlSerializer.FromMappings(new XmlMapping[] { membersMapping })[0];
4268+
4269+
object[] actual = SerializeAndDeserializeWithWrapper(value, serializer, baseline);
4270+
Assert.NotNull(actual);
4271+
Assert.Equal(value.Length, actual.Length);
4272+
4273+
var requestBodyActual = (SoapComplexType)actual[0];
4274+
Assert.NotNull(requestBodyActual);
4275+
Assert.Equal(requestBodyValue.BoolValue, requestBodyActual.BoolValue);
4276+
Assert.Equal(requestBodyValue.StringValue, requestBodyActual.StringValue);
4277+
}
4278+
4279+
[Fact]
4280+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "dotnet/corefx #18964")]
4281+
public static void XmlMembersMapping_Soap_SoapComplexTypeWithArray()
4282+
{
4283+
string memberName = "EchoComositeTypeXmlSerializerFormatSoapResult";
4284+
var requestBodyValue = new SoapComplexTypeWithArray()
4285+
{
4286+
IntArray = new int[] { 1, 2 },
4287+
StringArray = new string[] { "foo", "bar" },
4288+
IntList = new List<int>() { 1, 2 },
4289+
StringList = new List<string>() { "foo", "bar" }
4290+
};
4291+
4292+
string baseline = "<root><q1:EchoComositeTypeXmlSerializerFormatSoapResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:q1=\"http://tempuri.org/\"><EchoComositeTypeXmlSerializerFormatSoapResult href=\"#id1\" /></q1:EchoComositeTypeXmlSerializerFormatSoapResponse><q2:SoapComplexTypeWithArray id=\"id1\" d2p1:type=\"q2:SoapComplexTypeWithArray\" xmlns:d2p1=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:q2=\"http://tempuri.org/encoded\"><IntArray href=\"#id2\" /><StringArray href=\"#id3\" /><IntList href=\"#id4\" /><StringList href=\"#id5\" /></q2:SoapComplexTypeWithArray><q3:Array id=\"id2\" xmlns:q4=\"http://www.w3.org/2001/XMLSchema\" q3:arrayType=\"q4:int[2]\" xmlns:q3=\"http://schemas.xmlsoap.org/soap/encoding/\"><Item>1</Item><Item>2</Item></q3:Array><q5:Array id=\"id3\" xmlns:q6=\"http://www.w3.org/2001/XMLSchema\" q5:arrayType=\"q6:string[2]\" xmlns:q5=\"http://schemas.xmlsoap.org/soap/encoding/\"><Item>foo</Item><Item>bar</Item></q5:Array><q7:Array id=\"id4\" xmlns:q8=\"http://www.w3.org/2001/XMLSchema\" q7:arrayType=\"q8:int[2]\" xmlns:q7=\"http://schemas.xmlsoap.org/soap/encoding/\"><Item>1</Item><Item>2</Item></q7:Array><q9:Array id=\"id5\" xmlns:q10=\"http://www.w3.org/2001/XMLSchema\" q9:arrayType=\"q10:string[2]\" xmlns:q9=\"http://schemas.xmlsoap.org/soap/encoding/\"><Item>foo</Item><Item>bar</Item></q9:Array></root>";
4293+
string ns = s_defaultNs;
4294+
string wrapperName = "EchoComositeTypeXmlSerializerFormatSoapResponse";
4295+
4296+
object[] value = new object[] { requestBodyValue };
4297+
XmlReflectionMember member = GetReflectionMember<SoapComplexTypeWithArray>(memberName, ns);
4298+
member.SoapAttributes.SoapElement = new SoapElementAttribute(memberName);
4299+
var members = new XmlReflectionMember[] { member };
4300+
4301+
var importer = new SoapReflectionImporter(null, "http://tempuri.org/encoded");
4302+
var membersMapping = importer.ImportMembersMapping(wrapperName, ns, members, hasWrapperElement: true, writeAccessors: true);
4303+
var serializer = XmlSerializer.FromMappings(new XmlMapping[] { membersMapping })[0];
4304+
4305+
object[] actual = SerializeAndDeserializeWithWrapper(value, serializer, baseline);
4306+
Assert.NotNull(actual);
4307+
Assert.Equal(value.Length, actual.Length);
4308+
4309+
var requestBodyActual = (SoapComplexTypeWithArray)actual[0];
4310+
Assert.NotNull(requestBodyActual);
4311+
Assert.True(requestBodyValue.IntArray.SequenceEqual(requestBodyActual.IntArray));
4312+
Assert.True(requestBodyValue.StringArray.SequenceEqual(requestBodyActual.StringArray));
4313+
Assert.True(requestBodyValue.IntList.SequenceEqual(requestBodyActual.IntList));
4314+
Assert.True(requestBodyValue.StringList.SequenceEqual(requestBodyActual.StringList));
4315+
}
4316+
42354317
[Fact]
42364318
public static void Xml_XmlTextAttributeTest()
42374319
{

src/System.Runtime.Serialization.Xml/tests/SerializationTypes.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5143,6 +5143,19 @@ public People()
51435143
public string Name;
51445144
}
51455145

5146+
public class SoapComplexType
5147+
{
5148+
public bool BoolValue;
5149+
public string StringValue;
5150+
}
5151+
5152+
public class SoapComplexTypeWithArray
5153+
{
5154+
public int[] IntArray;
5155+
public string[] StringArray;
5156+
public List<int> IntList;
5157+
public List<string> StringList;
5158+
}
51465159
[KnownType("KnownTypes")]
51475160
[DataContract]
51485161
public class EmployeeC

0 commit comments

Comments
 (0)