Skip to content

Add support for null parameter serialization and related tests #29

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions Kveer.XmlRPC.Tests/serializetest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,5 +1303,47 @@ public void XmlRpcStruct()
Assert.IsTrue(xmlRpcStruct["ms"] is string);
Assert.AreEqual((string) xmlRpcStruct["ms"], "another test string");
}
//---------------------- null parameter with flag --------------------------//
[Test]
public void NullParameter_WithAllowNullParams()
{
Stream stm = new MemoryStream();
var req = new XmlRpcRequest();
req.args = new object[] { null };
req.method = "Foo";

var ser = new XmlRpcSerializer();
ser.NonStandard = XmlRpcNonStandard.AllowNullParams;

ser.SerializeRequest(stm, req);
stm.Position = 0;
TextReader tr = new StreamReader(stm);
var reqstr = tr.ReadToEnd();

Assert.AreEqual(
@"<?xml version=""1.0""?>
<methodCall>
<methodName>Foo</methodName>
<params>
<param>
<value>
<nil />
</value>
</param>
</params>
</methodCall>".Replace("\r\n", Environment.NewLine), reqstr);
}
[Test]
public void NullParam_ThrowsWithoutFlag()
{
Stream stm = new MemoryStream();
var req = new XmlRpcRequest();
req.args = new object[] { null };
req.method = "Foo";

var ser = new XmlRpcSerializer();

Assert.That(() => ser.SerializeRequest(stm, req), Throws.TypeOf<XmlRpcNullParameterException>());
}
}
}
1 change: 1 addition & 0 deletions Kveer.XmlRPC/XmlRpcNonStandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum XmlRpcNonStandard
MapZerosDateTimeToMinValue = 0x8,
MapEmptyDateTimeToMinValue = 0x10,
AllowInvalidHttpContent = 0x20,
AllowNullParams = 0x40,
All = 0x7fff
}
}
95 changes: 68 additions & 27 deletions Kveer.XmlRPC/XmlRpcSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,29 +125,55 @@ private void SerializeParams(XmlTextWriter xtw, XmlRpcRequest request,
&& Attribute.IsDefined(pis[i], typeof(ParamArrayAttribute)))
{
var ary = (Array)request.args[i];
foreach (var o in ary)
{
if (o == null)
throw new XmlRpcNullParameterException(
"Null parameter in params array");
xtw.WriteStartElement("", "param", "");
Serialize(xtw, o, mappingAction);
xtw.WriteEndElement();
}

break;
}
}

if (request.args[i] == null)
throw new XmlRpcNullParameterException(string.Format(
"Null method parameter #{0}", i + 1));
xtw.WriteStartElement("", "param", "");
Serialize(xtw, request.args[i], mappingAction);
xtw.WriteEndElement();
}
}

foreach (var o in ary)
{
xtw.WriteStartElement("", "param", "");
if (o == null)
{
if ((NonStandard & XmlRpcNonStandard.AllowNullParams) != 0)
{
xtw.WriteStartElement("", "value", "");
xtw.WriteStartElement("", "nil", "");
xtw.WriteEndElement();
xtw.WriteEndElement();
}
else
{
throw new XmlRpcNullParameterException("Null parameter in params array");
}
}
else
{
Serialize(xtw, o, mappingAction);
}
xtw.WriteEndElement();
}
break;
}
}

xtw.WriteStartElement("", "param", "");
if (request.args[i] == null)
{
if ((NonStandard & XmlRpcNonStandard.AllowNullParams) != 0)
{
xtw.WriteStartElement("", "value", "");
xtw.WriteStartElement("", "nil", "");
xtw.WriteEndElement();
xtw.WriteEndElement();
}
else
{
throw new XmlRpcNullParameterException($"Null method parameter #{i + 1}");
}
}
else
{
Serialize(xtw, request.args[i], mappingAction);
}
xtw.WriteEndElement();
}
}
private void SerializeStructParams(XmlTextWriter xtw, XmlRpcRequest request,
MappingAction mappingAction)
{
Expand All @@ -164,12 +190,27 @@ private void SerializeStructParams(XmlTextWriter xtw, XmlRpcRequest request,
xtw.WriteStartElement("", "struct", "");
for (var i = 0; i < request.args.Length; i++)
{
if (request.args[i] == null)
throw new XmlRpcNullParameterException(string.Format(
"Null method parameter #{0}", i + 1));

xtw.WriteStartElement("", "member", "");
xtw.WriteElementString("name", pis[i].Name);
Serialize(xtw, request.args[i], mappingAction);
if (request.args[i] == null)
{
if ((NonStandard & XmlRpcNonStandard.AllowNullParams) != 0)
{
xtw.WriteStartElement("", "value", "");
xtw.WriteStartElement("", "nil", "");
xtw.WriteEndElement();
xtw.WriteEndElement();
}
else
{
throw new XmlRpcNullParameterException($"Null method parameter #{i + 1}");
}
}
else
{
Serialize(xtw, request.args[i], mappingAction);
}
xtw.WriteEndElement();
}

Expand Down