Skip to content

Commit

Permalink
HTTP GET primitive type parameter resolve [Bug Fix]
Browse files Browse the repository at this point in the history
  • Loading branch information
gencebay committed Mar 23, 2017
1 parent 9698680 commit 144b738
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 23 deletions.
13 changes: 2 additions & 11 deletions src/NetCoreStack.Proxy/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;

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

public static void Merge(this IDictionary<string, object> instance, IDictionary<string, object> from, bool replaceExisting)
{
foreach (KeyValuePair<string, object> entry in from)
{
if (replaceExisting || !instance.ContainsKey(entry.Key))
{
instance[entry.Key] = entry.Value;
}
}
}

public static void MergeArgs(this IDictionary<string, object> dictionary, object[] args, ParameterDescriptor[] parameters)
{
if (args.Length == 0)
Expand All @@ -60,6 +50,7 @@ public static string ToQueryString(this Uri baseUrl, IDictionary<string, object>
dict.Add(entry.Key, TypeParser(entry));
}
}
// encode and create url
return QueryHelpers.AddQueryString(baseUrl.AbsoluteUri, dict);
}
}
Expand Down
17 changes: 10 additions & 7 deletions src/NetCoreStack.Proxy/ProxyMethodDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using NetCoreStack.Common.Extensions;
using NetCoreStack.Proxy.Extensions;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -46,21 +47,23 @@ public ProxyMethodDescriptor(MethodInfo methodInfo)
public IDictionary<string, object> Resolve(object[] args)
{
var values = new Dictionary<string, object>();

if (HttpMethod == HttpMethod.Get)
{
if (args.Length > 1)
throw new ArgumentOutOfRangeException($"Methods marked with HTTP GET can take one reference type parameter.");

if (args.Length != 0)
// Ref type parameter resolver
if (Parameters.Count == 1 && Parameters[0].ParameterType.IsReferenceType())
{
var obj = args[0].ToDictionary();
values.Merge(obj, true);
return values;
}

if (Parameters.Count > 1 && Parameters.Any(x => x.ParameterType.IsReferenceType()))
{
throw new ArgumentOutOfRangeException($"Methods marked with HTTP GET can take only one reference type parameter at the same time.");
}
}
else
values.MergeArgs(args, Parameters.ToArray());

values.MergeArgs(args, Parameters.ToArray());
return values;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public async Task GetWithReferenceType([FromQuery]SimpleModel model)
}

[HttpGet(nameof(PrimitiveReturn))]
public int PrimitiveReturn(int i, string s, long l, DateTime dt)
public async Task<int> PrimitiveReturn(int i, string s, long l, DateTime dt)
{
await Task.CompletedTask;
Logger.LogDebug($"{nameof(PrimitiveReturn)}, i:{i}, s:{s}, l:{l}, dt:{dt}");
return i + 10;
}
Expand All @@ -110,8 +111,9 @@ public async Task TaskOperation()
}

[HttpGet(nameof(VoidOperation))]
public void VoidOperation()
public async Task VoidOperation()
{
await Task.CompletedTask;
var str = "Hello World!";
Logger.LogDebug($"{nameof(VoidOperation)}, {str}");
}
Expand Down
4 changes: 2 additions & 2 deletions test/NetCoreStack.Proxy.Test.Contracts/IGuidelineApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace NetCoreStack.Proxy.Test.Contracts
[ApiRoute("api/[controller]", regionKey: "Main")]
public interface IGuidelineApi : IApiContract
{
void VoidOperation();
Task VoidOperation();

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

Task TaskOperation();

Expand Down
2 changes: 1 addition & 1 deletion test/NetCoreStack.Proxy.Test.Contracts/TestTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class SimpleModel
{
public string Name { get; set; }
public DateTime Date { get; set; }
public object Value { get; set; }
public string Value { get; set; }
}

public class Post
Expand Down
19 changes: 19 additions & 0 deletions test/NetCoreStack.Proxy.WebClient/Controllers/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public TestController(IGuidelineApi api)
_api = api;
}

public async Task<IActionResult> PrimitiveReturn()
{
var items = await _api.PrimitiveReturn(12, "Hello World", long.MaxValue, DateTime.Now);
return Json(items);
}

public async Task<IActionResult> GetPostsAsync()
{
var items = await _api.GetPostsAsync();
Expand All @@ -32,6 +38,19 @@ public IActionResult DirectStreamTransports()
return Json(items);
}

public async Task<IActionResult> TaskActionPost()
{
var simpleModel = new SimpleModel
{
Name = "WebClient",
Date = DateTime.Now,
Value = "<<string>>"
};

await _api.TaskActionPost(simpleModel);
return Json(simpleModel);
}

public async Task<IActionResult> GetWithReferenceType()
{
var simpleModel = new SimpleModel
Expand Down

0 comments on commit 144b738

Please sign in to comment.