Skip to content

Commit 8d8ea82

Browse files
committed
Merge pull request Astn#62 from artnim/master
Proper handle nested, recursive types for parameters and returns.
2 parents f1f16d8 + bf97f1f commit 8d8ea82

3 files changed

Lines changed: 52 additions & 11 deletions

File tree

AustinHarris.JsonRpcTestN/Test.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,16 @@ public void TestExtraParameters()
18621862
Assert.IsTrue(result.Result.Contains("\"code\":-32602"));
18631863
}
18641864

1865+
[Test]
1866+
public void TestNestedReturnType()
1867+
{
1868+
var request = @"{""jsonrpc"":""2.0"",""method"":""TestNestedReturnType"",""id"":1}";
1869+
var expected = @"{""jsonrpc"":""2.0"",""result"":{""NodeId"":1,""Leafs"":[{""NodeId"":2,""Leafs"":[]},{""NodeId"":3,""Leafs"":[]}]},""id"":1}";
1870+
var result = JsonRpcProcessor.Process(request);
1871+
result.Wait();
1872+
Assert.AreEqual(expected, result.Result);
1873+
}
1874+
18651875
private static void AssertJsonAreEqual(string expectedJson, string actualJson)
18661876
{
18671877
Newtonsoft.Json.Linq.JObject expected = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(expectedJson);

AustinHarris.JsonRpcTestN/service.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
namespace AustinHarris.JsonRpcTestN
99
{
10+
public class TreeNode
11+
{
12+
public int NodeId { get; set; }
13+
14+
public IList<TreeNode> Leafs { get; set; }
15+
}
16+
1017
public class CalculatorService : JsonRpcService
1118
{
1219
[JsonRpcMethod]
@@ -354,5 +361,21 @@ public string TestPostProcessorSetsException(string inputValue)
354361
JsonRpcContext.SetException(new JsonRpcException(-27001, "This exception was thrown using: JsonRpcContext.SetException()", null));
355362
return null;
356363
}
364+
365+
[JsonRpcMethod]
366+
public TreeNode TestNestedReturnType()
367+
{
368+
return new TreeNode
369+
{
370+
NodeId = 1,
371+
Leafs =
372+
new[]
373+
{
374+
new TreeNode {NodeId = 2, Leafs = new List<TreeNode>()},
375+
new TreeNode {NodeId = 3, Leafs = new List<TreeNode>()}
376+
}
377+
};
378+
}
379+
357380
}
358381
}

Json-Rpc/SMDService.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,24 @@ public void AddService(string method, Dictionary<string,Type> parameters, Dictio
4242

4343
public static int AddType(JObject jo)
4444
{
45-
var hash = "t_" + jo.ToString().GetHashCode();
45+
var hash = string.Format("t_{0}", jo.ToString().GetHashCode());
46+
4647
lock (TypeHashes)
47-
{
48-
var idx = 0;
49-
if (TypeHashes.Contains(hash) == false)
50-
{
51-
TypeHashes.Add(hash);
52-
idx = TypeHashes.IndexOf(hash);
53-
Types.Add(idx, jo);
54-
}
48+
{
49+
if (TypeHashes.Contains(hash)) return TypeHashes.IndexOf(hash);
50+
51+
TypeHashes.Add(hash);
52+
var idx = TypeHashes.IndexOf(hash);
53+
Types.Add(idx, jo);
5554
}
55+
5656
return TypeHashes.IndexOf(hash);
5757
}
58+
59+
public static bool ContainsType(JObject jo)
60+
{
61+
return TypeHashes.Contains(string.Format("t_{0}", jo.ToString().GetHashCode()));
62+
}
5863
}
5964

6065
public class SMDService
@@ -163,11 +168,14 @@ internal static int GetTypeRecursive(Type t)
163168
{
164169
JObject jo = new JObject();
165170
jo.Add("__name", t.Name.ToLower());
166-
if (isSimpleType(t))
171+
172+
if (isSimpleType(t) || SMD.ContainsType(jo))
167173
{
168174
return SMD.AddType(jo);
169175
}
170176

177+
var retVal = SMD.AddType(jo);
178+
171179
var genArgs = t.GetGenericArguments();
172180
PropertyInfo[] properties = t.GetProperties();
173181
FieldInfo[] fields = t.GetFields();
@@ -225,7 +233,7 @@ internal static int GetTypeRecursive(Type t)
225233
}
226234
}
227235

228-
return SMD.AddType(jo);
236+
return retVal;
229237
}
230238

231239
internal static bool isSimpleType(Type t)

0 commit comments

Comments
 (0)