Skip to content

Commit 144b738

Browse files
committed
HTTP GET primitive type parameter resolve [Bug Fix]
1 parent 9698680 commit 144b738

File tree

6 files changed

+38
-23
lines changed

6 files changed

+38
-23
lines changed

src/NetCoreStack.Proxy/Extensions/DictionaryExtensions.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Globalization;
6+
using System.Net;
67

78
namespace NetCoreStack.Proxy.Extensions
89
{
@@ -24,17 +25,6 @@ private static string TypeParser(KeyValuePair<string, object> selector)
2425
return selector.Value.ToString();
2526
}
2627

27-
public static void Merge(this IDictionary<string, object> instance, IDictionary<string, object> from, bool replaceExisting)
28-
{
29-
foreach (KeyValuePair<string, object> entry in from)
30-
{
31-
if (replaceExisting || !instance.ContainsKey(entry.Key))
32-
{
33-
instance[entry.Key] = entry.Value;
34-
}
35-
}
36-
}
37-
3828
public static void MergeArgs(this IDictionary<string, object> dictionary, object[] args, ParameterDescriptor[] parameters)
3929
{
4030
if (args.Length == 0)
@@ -60,6 +50,7 @@ public static string ToQueryString(this Uri baseUrl, IDictionary<string, object>
6050
dict.Add(entry.Key, TypeParser(entry));
6151
}
6252
}
53+
// encode and create url
6354
return QueryHelpers.AddQueryString(baseUrl.AbsoluteUri, dict);
6455
}
6556
}

src/NetCoreStack.Proxy/ProxyMethodDescriptor.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Mvc;
22
using Microsoft.AspNetCore.Mvc.Abstractions;
3+
using NetCoreStack.Common.Extensions;
34
using NetCoreStack.Proxy.Extensions;
45
using System;
56
using System.Collections.Generic;
@@ -46,21 +47,23 @@ public ProxyMethodDescriptor(MethodInfo methodInfo)
4647
public IDictionary<string, object> Resolve(object[] args)
4748
{
4849
var values = new Dictionary<string, object>();
49-
5050
if (HttpMethod == HttpMethod.Get)
5151
{
52-
if (args.Length > 1)
53-
throw new ArgumentOutOfRangeException($"Methods marked with HTTP GET can take one reference type parameter.");
54-
55-
if (args.Length != 0)
52+
// Ref type parameter resolver
53+
if (Parameters.Count == 1 && Parameters[0].ParameterType.IsReferenceType())
5654
{
5755
var obj = args[0].ToDictionary();
5856
values.Merge(obj, true);
57+
return values;
58+
}
59+
60+
if (Parameters.Count > 1 && Parameters.Any(x => x.ParameterType.IsReferenceType()))
61+
{
62+
throw new ArgumentOutOfRangeException($"Methods marked with HTTP GET can take only one reference type parameter at the same time.");
5963
}
6064
}
61-
else
62-
values.MergeArgs(args, Parameters.ToArray());
6365

66+
values.MergeArgs(args, Parameters.ToArray());
6467
return values;
6568
}
6669
}

test/NetCoreStack.Proxy.ServerApp/Controllers/GuidelineController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ public async Task GetWithReferenceType([FromQuery]SimpleModel model)
8888
}
8989

9090
[HttpGet(nameof(PrimitiveReturn))]
91-
public int PrimitiveReturn(int i, string s, long l, DateTime dt)
91+
public async Task<int> PrimitiveReturn(int i, string s, long l, DateTime dt)
9292
{
93+
await Task.CompletedTask;
9394
Logger.LogDebug($"{nameof(PrimitiveReturn)}, i:{i}, s:{s}, l:{l}, dt:{dt}");
9495
return i + 10;
9596
}
@@ -110,8 +111,9 @@ public async Task TaskOperation()
110111
}
111112

112113
[HttpGet(nameof(VoidOperation))]
113-
public void VoidOperation()
114+
public async Task VoidOperation()
114115
{
116+
await Task.CompletedTask;
115117
var str = "Hello World!";
116118
Logger.LogDebug($"{nameof(VoidOperation)}, {str}");
117119
}

test/NetCoreStack.Proxy.Test.Contracts/IGuidelineApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace NetCoreStack.Proxy.Test.Contracts
99
[ApiRoute("api/[controller]", regionKey: "Main")]
1010
public interface IGuidelineApi : IApiContract
1111
{
12-
void VoidOperation();
12+
Task VoidOperation();
1313

14-
int PrimitiveReturn(int i, string s, long l, DateTime dt);
14+
Task<int> PrimitiveReturn(int i, string s, long l, DateTime dt);
1515

1616
Task TaskOperation();
1717

test/NetCoreStack.Proxy.Test.Contracts/TestTypes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class SimpleModel
66
{
77
public string Name { get; set; }
88
public DateTime Date { get; set; }
9-
public object Value { get; set; }
9+
public string Value { get; set; }
1010
}
1111

1212
public class Post

test/NetCoreStack.Proxy.WebClient/Controllers/TestController.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public TestController(IGuidelineApi api)
1414
_api = api;
1515
}
1616

17+
public async Task<IActionResult> PrimitiveReturn()
18+
{
19+
var items = await _api.PrimitiveReturn(12, "Hello World", long.MaxValue, DateTime.Now);
20+
return Json(items);
21+
}
22+
1723
public async Task<IActionResult> GetPostsAsync()
1824
{
1925
var items = await _api.GetPostsAsync();
@@ -32,6 +38,19 @@ public IActionResult DirectStreamTransports()
3238
return Json(items);
3339
}
3440

41+
public async Task<IActionResult> TaskActionPost()
42+
{
43+
var simpleModel = new SimpleModel
44+
{
45+
Name = "WebClient",
46+
Date = DateTime.Now,
47+
Value = "<<string>>"
48+
};
49+
50+
await _api.TaskActionPost(simpleModel);
51+
return Json(simpleModel);
52+
}
53+
3554
public async Task<IActionResult> GetWithReferenceType()
3655
{
3756
var simpleModel = new SimpleModel

0 commit comments

Comments
 (0)