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

XmlSerializer: Implement MessageContract(IsWrapped=false) for return results #310

Merged
merged 3 commits into from
Aug 8, 2019
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
27 changes: 27 additions & 0 deletions src/SoapCore.Tests/RawRequestSoap12Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ public void Soap12PingWithActionInHeader()
}
}

[TestMethod]
public void Soap12PingWithActionInEnvelopeHeader()
Copy link
Collaborator Author

@Simon-Campbell Simon-Campbell Aug 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is unrelated but to avoid the admin overhead; I have included it to increase test coverage.

{
var body = @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"" xmlns:wsa=""http://www.w3.org/2005/08/addressing"">
<soap12:Header>
<wsa:Action>Ping</wsa:Action>
</soap12:Header>
<soap12:Body>
<Ping xmlns=""http://tempuri.org/"">
<s>abc</s>
</Ping>
</soap12:Body>
</soap12:Envelope>
";
var bodyBytes = Encoding.UTF8.GetBytes(body);
using (var host = CreateTestHost())
using (var client = host.CreateClient())
using (var content = new StringContent(body, Encoding.UTF8, "application/soap+xml"))
{
using (var res = host.CreateRequest("/Service.svc").And(msg => msg.Content = content).PostAsync().Result)
{
res.EnsureSuccessStatusCode();
}
}
}

[TestMethod]
public void Soap12PingNoActionInHeader()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;

namespace SoapCore.Tests.Serialization.Models.Xml
{
[MessageContract]
public class BasicMessageContractPayload
{
}
}
12 changes: 12 additions & 0 deletions src/SoapCore.Tests/Serialization/Models.Xml/ISampleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ NotWrappedFieldComplexInputResponse NotWrappedFieldComplexInputRequestMethod(
NotWrappedFieldComplexInputResponse NotWrappedFieldDoubleComplexInputRequestMethod(
NotWrappedFieldDoubleComplexInputRequest request);

// Ideally this would be void however the WCF client requires that if you have a MessageContract
// response you *must* have a MessageContract input
[OperationContract(Action = ServiceNamespace.Value + nameof(TestUnwrappedMultipleMessageBodyMember), ReplyAction = "*")]
[XmlSerializerFormat(SupportFaults = true)]
UnwrappedMultipleMessageBodyMemberResponse TestUnwrappedMultipleMessageBodyMember(BasicMessageContractPayload x);

// Ideally this would be void however WCF client requires that if you have a MessageContract
// response you *must* have a MessageContract input
[OperationContract(Action = ServiceNamespace.Value + nameof(TestUnwrappedStringMessageBodyMember), ReplyAction = "*")]
[XmlSerializerFormat(SupportFaults = true)]
UnwrappedStringMessageBodyMemberResponse TestUnwrappedStringMessageBodyMember(BasicMessageContractPayload x);

[OperationContract(Action = ServiceNamespace.Value + nameof(EnumMethod), ReplyAction = "*")]
[XmlSerializerFormat(SupportFaults = true)]
bool EnumMethod(out SampleEnum e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ServiceModel;

namespace SoapCore.Tests.Serialization.Models.Xml
{
[MessageContract(IsWrapped = false)]
public class UnwrappedMultipleMessageBodyMemberResponse
{
[MessageBodyMember(Name = "foo1", Namespace = "http://tempuri.org/NotWrappedPropertyComplexInput")]
public NotWrappedPropertyComplexInput NotWrappedComplexInput1 { get; set; }

[MessageBodyMember(Name = "foo2", Namespace = "http://tempuri.org/NotWrappedPropertyComplexInput")]
public NotWrappedPropertyComplexInput NotWrappedComplexInput2 { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.ServiceModel;

namespace SoapCore.Tests.Serialization.Models.Xml
{
[MessageContract(IsWrapped = false)]
public class UnwrappedStringMessageBodyMemberResponse
{
[MessageBodyMember]
public string StringProperty { get; set; }
}
}
69 changes: 57 additions & 12 deletions src/SoapCore.Tests/Serialization/XmlSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,59 @@ public void TestNotWrappedPropertyComplexInput(SoapSerializer soapSerializer)

clientResponse.ShouldNotBeNull();

// The client does not support unpacking these message contracts, so further assertions have been
// commented
// clientResponse.NotWrappedComplexInput.ShouldNotBeNull();
// clientResponse.NotWrappedComplexInput.StringProperty.ShouldBe("z");
clientResponse.NotWrappedComplexInput.ShouldNotBeNull();
clientResponse.NotWrappedComplexInput.StringProperty.ShouldBe("z");
}

[Theory]
[InlineData(SoapSerializer.XmlSerializer)]
public void TestUnwrappedSimpleMessageBodyMemberResponse(SoapSerializer soapSerializer)
{
var sampleServiceClient = _fixture.GetSampleServiceClient(soapSerializer);

_fixture.ServiceMock
.Setup(x => x.TestUnwrappedStringMessageBodyMember(It.IsAny<BasicMessageContractPayload>()))
.Returns(() => new UnwrappedStringMessageBodyMemberResponse
{
StringProperty = "one"
});

var clientResponse = sampleServiceClient.TestUnwrappedStringMessageBodyMember(new BasicMessageContractPayload());

clientResponse.ShouldNotBeNull();
clientResponse.StringProperty.ShouldBe("one");
}

[Theory]
[InlineData(SoapSerializer.XmlSerializer)]
public void TestUnwrappedMultipleMessageBodyMemberResponse(SoapSerializer soapSerializer)
{
var sampleServiceClient = _fixture.GetSampleServiceClient(soapSerializer);

_fixture.ServiceMock
.Setup(x => x.TestUnwrappedMultipleMessageBodyMember(It.IsAny<BasicMessageContractPayload>()))
.Returns(new UnwrappedMultipleMessageBodyMemberResponse
{
NotWrappedComplexInput1 = new NotWrappedPropertyComplexInput
{
StringProperty = "one"
},

NotWrappedComplexInput2 = new NotWrappedPropertyComplexInput
{
StringProperty = "two"
}
});

var clientResponse = sampleServiceClient.TestUnwrappedMultipleMessageBodyMember(new BasicMessageContractPayload());

clientResponse.ShouldNotBeNull();

clientResponse.NotWrappedComplexInput1.ShouldNotBeNull();
clientResponse.NotWrappedComplexInput1.StringProperty.ShouldBe("one");

clientResponse.NotWrappedComplexInput2.ShouldNotBeNull();
clientResponse.NotWrappedComplexInput2.StringProperty.ShouldBe("two");
}

//not compatible with DataContractSerializer
Expand Down Expand Up @@ -387,10 +436,8 @@ public void TestNotWrappedFieldComplexInput(SoapSerializer soapSerializer)

clientResponse.ShouldNotBeNull();

// The client does not support unpacking these message contracts, so further assertions have been
// commented
// clientResponse.NotWrappedComplexInput.ShouldNotBeNull();
// clientResponse.NotWrappedComplexInput.StringProperty.ShouldBe("z");
clientResponse.NotWrappedComplexInput.ShouldNotBeNull();
clientResponse.NotWrappedComplexInput.StringProperty.ShouldBe("z");
}

//not compatible with DataContractSerializer
Expand Down Expand Up @@ -434,10 +481,8 @@ public void TestNotWrappedFieldDoubleComplexInput(SoapSerializer soapSerializer)

clientResponse.ShouldNotBeNull();

// The client does not support unpacking these message contracts, so further assertions have been
// commented
// clientResponse.NotWrappedComplexInput.ShouldNotBeNull();
// clientResponse.NotWrappedComplexInput.StringProperty.ShouldBe("z");
clientResponse.NotWrappedComplexInput.ShouldNotBeNull();
clientResponse.NotWrappedComplexInput.StringProperty.ShouldBe("z");
}

//not compatible with DataContractSerializer
Expand Down
1 change: 0 additions & 1 deletion src/SoapCore.Tests/SoapCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Serialization\" />
<Folder Include="Serialization\Models.DataContract\" />
<Folder Include="Serialization\Models.Models.Xml\" />
</ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions src/SoapCore/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,20 @@ internal static Type GetPropertyOrFieldType(this MemberInfo memberInfo)

return null;
}

internal static object GetPropertyOrFieldValue(this MemberInfo memberInfo, object obj)
{
if (memberInfo is FieldInfo fi)
{
return fi.GetValue(obj);
}

if (memberInfo is PropertyInfo pi)
{
return pi.GetValue(obj);
}

throw new NotImplementedException($"Unable to get value out of member with type {memberInfo.GetType()}");
}
}
}
Loading