Skip to content

Optional params #11

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
52 changes: 50 additions & 2 deletions Kveer.XmlRPC.Tests/paramstest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,14 @@
return args;
}

[XmlRpcMethod]
public int OptioanlNullParameter(string optional = null)

Check failure on line 68 in Kveer.XmlRPC.Tests/paramstest.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Kveer.XmlRPC.Tests/paramstest.cs#L68

Use the overloading mechanism instead of the optional parameters.
{
return 1;
}

private readonly string massimoRequest =

private readonly string massimoRequest =
@"<?xml version=""1.0""?>
<methodCall>
<methodName>Send_Param</methodName>
Expand Down Expand Up @@ -579,7 +585,49 @@
.Replace("\r\n", Environment.NewLine), reqstr);
}

[Test]
[Test]
public void SerializeNullParameterUnallowed()
{
Assert.Throws<XmlRpcNullParameterException>(
() => SerializeNullParameter(XmlRpcNonStandard.None));
}

[Test]
public void SerializeNullParameter()
{
string reqstr = SerializeNullParameter(XmlRpcNonStandard.AllowNull);
Assert.AreEqual(
@"<?xml version=""1.0""?>
<methodCall>
<methodName>SerializeOptionalNullParameter</methodName>
<params>
<param>
<value>
<nil />
</value>
</param>
</params>
</methodCall>"
.Replace("\r\n", Environment.NewLine), reqstr);
}

private string SerializeNullParameter(XmlRpcNonStandard options)
{
Stream stm = new MemoryStream();
var req = new XmlRpcRequest();
req.args = new object[] { null };
req.method = "SerializeOptionalNullParameter";
req.mi = typeof(IFoo).GetMethod("SerializeOptionalNullParameter");
var ser = new XmlRpcSerializer();
ser.NonStandard = options;
ser.SerializeRequest(stm, req);
stm.Position = 0;
TextReader tr = new StreamReader(stm);
var reqstr = tr.ReadToEnd();
return reqstr;
}

[Test]
public void SerializeZeroParameters()
{
Stream stm = new MemoryStream();
Expand Down
3 changes: 2 additions & 1 deletion 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,
All = 0x7fff
AllowNull = 0x40,
All = 0x7fff
}
}
27 changes: 21 additions & 6 deletions Kveer.XmlRPC/XmlRpcSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public class XmlRpcSerializer

private bool MapZerosDateTimeToMinValue => (NonStandard & XmlRpcNonStandard.MapZerosDateTimeToMinValue) != 0;

public void SerializeRequest(Stream stm, XmlRpcRequest request)
private bool AllowNull => (NonStandard & XmlRpcNonStandard.AllowNull) != 0;

public void SerializeRequest(Stream stm, XmlRpcRequest request)
{
var xtw = new XmlTextWriter(stm, XmlEncoding);
ConfigureXmlFormat(xtw);
Expand Down Expand Up @@ -137,11 +139,24 @@ private void SerializeParams(XmlTextWriter xtw, XmlRpcRequest request,
}
}

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.WriteStartElement("", "param", "");
if (request.args[i] == null)
{
if (!AllowNull)
{
throw new XmlRpcNullParameterException(string.Format(
"Null method parameter #{0}", i + 1));
}

xtw.WriteStartElement("", "value", "");
xtw.WriteStartElement("", "nil", "");
xtw.WriteEndElement();
xtw.WriteEndElement();
}
else
{
Serialize(xtw, request.args[i], mappingAction);
}
xtw.WriteEndElement();
}
}
Expand Down