From 82c56fcbd8628e800e7182a726557f7f94fa7f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Fri, 18 Mar 2022 13:58:35 +0100 Subject: [PATCH 01/14] Add Test For try fix a Bug --- .../HttpResponseMessageExtensions.cs | 13 ++++++++++ .../Routing/Builders/BugsController.cs | 11 +++++++-- .../Helpers/NullableQueryParamsResponse.cs | 9 +++++++ .../Routing/TestServerExtensionsTests.cs | 24 +++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 tests/UnitTests/Acheve.TestHost/Routing/Helpers/NullableQueryParamsResponse.cs diff --git a/src/Acheve.TestHost/HttpResponseMessageExtensions.cs b/src/Acheve.TestHost/HttpResponseMessageExtensions.cs index e8bd581..bdee76c 100644 --- a/src/Acheve.TestHost/HttpResponseMessageExtensions.cs +++ b/src/Acheve.TestHost/HttpResponseMessageExtensions.cs @@ -1,3 +1,4 @@ +using System.Text.Json; using System.Threading.Tasks; namespace System.Net.Http @@ -20,5 +21,17 @@ public static async Task IsSuccessStatusCodeOrThrow(this HttpResponseMessage res throw new Exception($"Response status does not indicate success: {response.StatusCode:D} ({response.StatusCode}); \r\n{content}"); } + + public static async Task ReadContentAsAsync(this HttpResponseMessage responseMessage) + { + var json = await responseMessage.Content.ReadAsStringAsync(); + + var options = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }; + + return JsonSerializer.Deserialize(json, options); + } } } \ No newline at end of file diff --git a/tests/UnitTests/Acheve.TestHost/Routing/Builders/BugsController.cs b/tests/UnitTests/Acheve.TestHost/Routing/Builders/BugsController.cs index 73dd2a7..e567b5b 100644 --- a/tests/UnitTests/Acheve.TestHost/Routing/Builders/BugsController.cs +++ b/tests/UnitTests/Acheve.TestHost/Routing/Builders/BugsController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using System; +using UnitTests.Acheve.TestHost.Routing.Helpers; namespace UnitTests.Acheve.TestHost.Builders { @@ -9,9 +10,15 @@ public class BugsController : ControllerBase { [HttpGet("{param1}/{param2}")] - public IActionResult GuidSupport(string param1,Guid param2) + public IActionResult GuidSupport(string param1, Guid param2) { return Ok(); } + + [HttpGet("nullableQueryParams")] + public ActionResult NullableQueryParams(bool? param1, Guid? param2) + { + return Ok(new NullableQueryParamsResponse { Param1 = param1, Param2 = param2 }); + } } -} +} \ No newline at end of file diff --git a/tests/UnitTests/Acheve.TestHost/Routing/Helpers/NullableQueryParamsResponse.cs b/tests/UnitTests/Acheve.TestHost/Routing/Helpers/NullableQueryParamsResponse.cs new file mode 100644 index 0000000..9f21625 --- /dev/null +++ b/tests/UnitTests/Acheve.TestHost/Routing/Helpers/NullableQueryParamsResponse.cs @@ -0,0 +1,9 @@ +using System; + +namespace UnitTests.Acheve.TestHost.Routing.Helpers; + +public class NullableQueryParamsResponse +{ + public bool? Param1 { get; set; } + public Guid? Param2 { get; set; } +} \ No newline at end of file diff --git a/tests/UnitTests/Acheve.TestHost/Routing/TestServerExtensionsTests.cs b/tests/UnitTests/Acheve.TestHost/Routing/TestServerExtensionsTests.cs index 95c049e..2eba80d 100644 --- a/tests/UnitTests/Acheve.TestHost/Routing/TestServerExtensionsTests.cs +++ b/tests/UnitTests/Acheve.TestHost/Routing/TestServerExtensionsTests.cs @@ -8,6 +8,7 @@ using System.Text.Json; using System.Threading.Tasks; using UnitTests.Acheve.TestHost.Builders; +using UnitTests.Acheve.TestHost.Routing.Helpers; using Xunit; namespace UnitTests.Acheve.TestHost.Routing @@ -1329,6 +1330,29 @@ public void create_valid_request_of_patch_without_using_frombody_with_apicontrol requestPost2.GetConfiguredAddress().StartsWith("api/values/1").Should().Be(true); } + [Fact] + public async Task create_valid_request_supporting_nullable_params_on_query() + { + var server = new TestServerBuilder() + .UseDefaultStartup() + .Build(); + + var guid = Guid.NewGuid(); + + var request = server.CreateHttpApiRequest( + actionSelector: controller => controller.NullableQueryParams(null,guid), + tokenValues: null, + contentOptions: new NotIncludeContent()); + + var responseMessage = await request.GetAsync(); + + responseMessage.EnsureSuccessStatusCode(); + var response = await responseMessage.ReadContentAsAsync(); + + response.Param1.Should().Be(null); + response.Param2.Should().Be(guid); + } + private class PrivateNonControllerClass { public int SomeAction() From 5f77402ae6bbd4e8a272a089052ee15956417cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Mon, 21 Mar 2022 12:58:52 +0100 Subject: [PATCH 02/14] Allo Query Parameter Nullable --- src/Acheve.TestHost/Acheve.TestHost.csproj | 1 - .../Routing/TestServerAction.cs | 55 +++++++++++++------ .../ComplexParameterActionTokenizer.cs | 6 +- .../PrimitiveParameterActionTokenizer.cs | 10 +--- .../Routing/Tokenizers/PrimitiveTypeHelper.cs | 16 ++++++ .../Routing/Builders/BugsController.cs | 2 +- .../Helpers/NullableQueryParamsResponse.cs | 9 --- .../Models/NullableQueryParamsResponse.cs | 10 ++++ .../Routing/TestServerExtensionsTests.cs | 6 +- 9 files changed, 75 insertions(+), 40 deletions(-) create mode 100644 src/Acheve.TestHost/Routing/Tokenizers/PrimitiveTypeHelper.cs delete mode 100644 tests/UnitTests/Acheve.TestHost/Routing/Helpers/NullableQueryParamsResponse.cs create mode 100644 tests/UnitTests/Acheve.TestHost/Routing/Models/NullableQueryParamsResponse.cs diff --git a/src/Acheve.TestHost/Acheve.TestHost.csproj b/src/Acheve.TestHost/Acheve.TestHost.csproj index 7eace34..eb4ab56 100644 --- a/src/Acheve.TestHost/Acheve.TestHost.csproj +++ b/src/Acheve.TestHost/Acheve.TestHost.csproj @@ -15,7 +15,6 @@ - diff --git a/src/Acheve.TestHost/Routing/TestServerAction.cs b/src/Acheve.TestHost/Routing/TestServerAction.cs index 0eaa7eb..aa9777c 100644 --- a/src/Acheve.TestHost/Routing/TestServerAction.cs +++ b/src/Acheve.TestHost/Routing/TestServerAction.cs @@ -37,25 +37,48 @@ public void AddArgument(int order, Expression expression, bool activeBodyApiCont if (!ArgumentValues.ContainsKey(order)) { - switch (expression) + if (IsNullable(argument.ParameterType)) { - case ConstantExpression constant: - { - ArgumentValues.Add(order, new TestServerArgument(constant.Value?.ToString(), isFromBody, isFromForm, isFromHeader, argument.Name)); - } - break; - case MemberExpression member when member.NodeType == ExpressionType.MemberAccess: - { - var instance = Expression.Lambda(member) - .Compile() - .DynamicInvoke(); - - ArgumentValues.Add(order, new TestServerArgument(instance, isFromBody, isFromForm, isFromHeader, argument.Name)); - } - break; - default: return; + var expressionValue = Expression.Lambda(expression).Compile().DynamicInvoke(); + + if (expressionValue != null) + { + ArgumentValues.Add(order, new TestServerArgument(expressionValue.ToString(), isFromBody, isFromForm, isFromHeader, argument.Name)); + } + } + else + { + switch (expression) + { + case ConstantExpression constant: + { + ArgumentValues.Add(order, new TestServerArgument(constant.Value?.ToString(), isFromBody, isFromForm, isFromHeader, argument.Name)); + } + break; + + case MemberExpression member when member.NodeType == ExpressionType.MemberAccess: + { + var instance = Expression.Lambda(member) + .Compile() + .DynamicInvoke(); + + ArgumentValues.Add(order, new TestServerArgument(instance, isFromBody, isFromForm, isFromHeader, argument.Name)); + } + break; + + case MethodCallExpression method: + { + var instance = Expression.Lambda(method).Compile().DynamicInvoke(); + ArgumentValues.Add(order, new TestServerArgument(instance, isFromBody, isFromForm, isFromHeader, argument.Name)); + } + break; + + default: return; + } } } } + + bool IsNullable(Type type) => Nullable.GetUnderlyingType(type) != null; } } diff --git a/src/Acheve.TestHost/Routing/Tokenizers/ComplexParameterActionTokenizer.cs b/src/Acheve.TestHost/Routing/Tokenizers/ComplexParameterActionTokenizer.cs index cb9592f..683e365 100644 --- a/src/Acheve.TestHost/Routing/Tokenizers/ComplexParameterActionTokenizer.cs +++ b/src/Acheve.TestHost/Routing/Tokenizers/ComplexParameterActionTokenizer.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; using System; +using System.Linq; using System.Reflection; namespace Acheve.TestHost.Routing.Tokenizers @@ -15,9 +16,10 @@ public void AddTokens(TestServerAction action, TestServerTokenColle for (int i = 0; i < parameters.Length; i++) { var type = parameters[i].ParameterType; - var instance = action.ArgumentValues[i].Instance; + var instance = action.ArgumentValues.Any(x => x.Key == i) ? action.ArgumentValues[i].Instance : null; - if (instance != null && !(type.IsPrimitive || type == typeof(String) || type == typeof(Decimal) || type == typeof(Guid))) + + if (instance != null && !type.IsPrimitiveType()) { if (!IgnoreBind(parameters[i])) { diff --git a/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs b/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs index 30e7beb..5455a2b 100644 --- a/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs +++ b/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs @@ -15,17 +15,11 @@ public void AddTokens(TestServerAction action, TestServerTokenColle for (var i = 0; i < parameters.Length; i++) { - if ((parameters[i].ParameterType.IsPrimitive - || - parameters[i].ParameterType == typeof(string) - || - parameters[i].ParameterType == typeof(decimal) - || - parameters[i].ParameterType == typeof(Guid)) + if (parameters[i].ParameterType.IsPrimitiveType() && !IgnoreHeader(parameters[i])) { var tokenName = parameters[i].Name.ToLowerInvariant(); - var tokenValue = action.ArgumentValues[i].Instance; + var tokenValue = action.ArgumentValues.Any(x => x.Key == i) ? action.ArgumentValues[i].Instance : null; if (tokenValue != null) { diff --git a/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveTypeHelper.cs b/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveTypeHelper.cs new file mode 100644 index 0000000..806a54f --- /dev/null +++ b/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveTypeHelper.cs @@ -0,0 +1,16 @@ +using System; + +namespace Acheve.TestHost.Routing.Tokenizers; + +public static class PrimitiveTypeHelper +{ + public static bool IsPrimitiveType(this Type typeToInspect) + { + var type = Nullable.GetUnderlyingType(typeToInspect) ?? typeToInspect; + + return type.IsPrimitive + || type == typeof(string) + || type == typeof(decimal) + || type == typeof(Guid); + } +} \ No newline at end of file diff --git a/tests/UnitTests/Acheve.TestHost/Routing/Builders/BugsController.cs b/tests/UnitTests/Acheve.TestHost/Routing/Builders/BugsController.cs index e6d43ee..42a9cab 100644 --- a/tests/UnitTests/Acheve.TestHost/Routing/Builders/BugsController.cs +++ b/tests/UnitTests/Acheve.TestHost/Routing/Builders/BugsController.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Mvc; using System; -using UnitTests.Acheve.TestHost.Routing.Helpers; +using UnitTests.Acheve.TestHost.Routing.Models; namespace UnitTests.Acheve.TestHost.Builders { diff --git a/tests/UnitTests/Acheve.TestHost/Routing/Helpers/NullableQueryParamsResponse.cs b/tests/UnitTests/Acheve.TestHost/Routing/Helpers/NullableQueryParamsResponse.cs deleted file mode 100644 index 9f21625..0000000 --- a/tests/UnitTests/Acheve.TestHost/Routing/Helpers/NullableQueryParamsResponse.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace UnitTests.Acheve.TestHost.Routing.Helpers; - -public class NullableQueryParamsResponse -{ - public bool? Param1 { get; set; } - public Guid? Param2 { get; set; } -} \ No newline at end of file diff --git a/tests/UnitTests/Acheve.TestHost/Routing/Models/NullableQueryParamsResponse.cs b/tests/UnitTests/Acheve.TestHost/Routing/Models/NullableQueryParamsResponse.cs new file mode 100644 index 0000000..6514f90 --- /dev/null +++ b/tests/UnitTests/Acheve.TestHost/Routing/Models/NullableQueryParamsResponse.cs @@ -0,0 +1,10 @@ +using System; + +namespace UnitTests.Acheve.TestHost.Routing.Models +{ + public class NullableQueryParamsResponse + { + public bool? Param1 { get; set; } + public Guid? Param2 { get; set; } + } +} \ No newline at end of file diff --git a/tests/UnitTests/Acheve.TestHost/Routing/TestServerExtensionsTests.cs b/tests/UnitTests/Acheve.TestHost/Routing/TestServerExtensionsTests.cs index ed15444..4e66314 100644 --- a/tests/UnitTests/Acheve.TestHost/Routing/TestServerExtensionsTests.cs +++ b/tests/UnitTests/Acheve.TestHost/Routing/TestServerExtensionsTests.cs @@ -8,7 +8,7 @@ using System.Text.Json; using System.Threading.Tasks; using UnitTests.Acheve.TestHost.Builders; -using UnitTests.Acheve.TestHost.Routing.Helpers; +using UnitTests.Acheve.TestHost.Routing.Models; using Xunit; namespace UnitTests.Acheve.TestHost.Routing @@ -1355,9 +1355,9 @@ public async Task create_valid_request_supporting_nullable_params_on_query() var guid = Guid.NewGuid(); var request = server.CreateHttpApiRequest( - actionSelector: controller => controller.NullableQueryParams(null,guid), + actionSelector: controller => controller.NullableQueryParams(null, guid), tokenValues: null, - contentOptions: new NotIncludeContent()); + contentOptions: new NotIncludeContent()); var responseMessage = await request.GetAsync(); From 2f2f5923d470ac1fd97addecad162c7d3ab80908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Mon, 21 Mar 2022 13:02:53 +0100 Subject: [PATCH 03/14] allow .net 3.1 build --- .../Routing/Tokenizers/PrimitiveTypeHelper.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveTypeHelper.cs b/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveTypeHelper.cs index 806a54f..fe72af9 100644 --- a/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveTypeHelper.cs +++ b/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveTypeHelper.cs @@ -1,16 +1,17 @@ using System; -namespace Acheve.TestHost.Routing.Tokenizers; - -public static class PrimitiveTypeHelper +namespace Acheve.TestHost.Routing.Tokenizers { - public static bool IsPrimitiveType(this Type typeToInspect) + public static class PrimitiveTypeHelper { - var type = Nullable.GetUnderlyingType(typeToInspect) ?? typeToInspect; + public static bool IsPrimitiveType(this Type typeToInspect) + { + var type = Nullable.GetUnderlyingType(typeToInspect) ?? typeToInspect; - return type.IsPrimitive - || type == typeof(string) - || type == typeof(decimal) - || type == typeof(Guid); + return type.IsPrimitive + || type == typeof(string) + || type == typeof(decimal) + || type == typeof(Guid); + } } } \ No newline at end of file From 923e12f49a8ac93d5f9eb370667d2899972c06d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Tue, 22 Mar 2022 09:37:20 +0100 Subject: [PATCH 04/14] add folder again --- src/Acheve.TestHost/Acheve.TestHost.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Acheve.TestHost/Acheve.TestHost.csproj b/src/Acheve.TestHost/Acheve.TestHost.csproj index eb4ab56..5521624 100644 --- a/src/Acheve.TestHost/Acheve.TestHost.csproj +++ b/src/Acheve.TestHost/Acheve.TestHost.csproj @@ -15,6 +15,7 @@ + From ca201da50fb02e21ab69f46589b29f214aac6f3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Tue, 22 Mar 2022 19:06:07 +0100 Subject: [PATCH 05/14] fix indent --- src/Acheve.TestHost/Acheve.TestHost.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Acheve.TestHost/Acheve.TestHost.csproj b/src/Acheve.TestHost/Acheve.TestHost.csproj index 5521624..5378b6c 100644 --- a/src/Acheve.TestHost/Acheve.TestHost.csproj +++ b/src/Acheve.TestHost/Acheve.TestHost.csproj @@ -14,8 +14,8 @@ - - + + From 04a6a918364232986b0dfeae667ee6fa6d04f38e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Tue, 22 Mar 2022 19:06:47 +0100 Subject: [PATCH 06/14] fix indent --- src/Acheve.TestHost/Acheve.TestHost.csproj | 46 +++++++++++----------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Acheve.TestHost/Acheve.TestHost.csproj b/src/Acheve.TestHost/Acheve.TestHost.csproj index 5378b6c..a9bcb8d 100644 --- a/src/Acheve.TestHost/Acheve.TestHost.csproj +++ b/src/Acheve.TestHost/Acheve.TestHost.csproj @@ -1,30 +1,30 @@  - - $(NetCoreTargetVersion) + + $(NetCoreTargetVersion) - $(Version) - $(PackageLicenseUrl) - $(PackageProjectUrl) - $(RepositoryUrl) - $(Authors) - $(Company) - $(Description) - $(Tags) - - - - - - + $(Version) + $(PackageLicenseUrl) + $(PackageProjectUrl) + $(RepositoryUrl) + $(Authors) + $(Company) + $(Description) + $(Tags) + - - - + + + + - - - - + + + + + + + + From b2a13f0c60e121441b32722510a29458dfcf6f5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Tue, 22 Mar 2022 19:08:33 +0100 Subject: [PATCH 07/14] indent --- src/Acheve.TestHost/Acheve.TestHost.csproj | 48 +++++++++++----------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/Acheve.TestHost/Acheve.TestHost.csproj b/src/Acheve.TestHost/Acheve.TestHost.csproj index a9bcb8d..39b1ffd 100644 --- a/src/Acheve.TestHost/Acheve.TestHost.csproj +++ b/src/Acheve.TestHost/Acheve.TestHost.csproj @@ -1,30 +1,30 @@  - - $(NetCoreTargetVersion) + + $(NetCoreTargetVersion) - $(Version) - $(PackageLicenseUrl) - $(PackageProjectUrl) - $(RepositoryUrl) - $(Authors) - $(Company) - $(Description) - $(Tags) - + $(Version) + $(PackageLicenseUrl) + $(PackageProjectUrl) + $(RepositoryUrl) + $(Authors) + $(Company) + $(Description) + $(Tags) + + + + + + - - - - + + + - - - + + + + - - - - - - + \ No newline at end of file From 3da64c6f89e26d6d20f7b8dc4e5f8544dedc8e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Tue, 22 Mar 2022 19:09:38 +0100 Subject: [PATCH 08/14] indent --- src/Acheve.TestHost/Acheve.TestHost.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Acheve.TestHost/Acheve.TestHost.csproj b/src/Acheve.TestHost/Acheve.TestHost.csproj index 39b1ffd..7eace34 100644 --- a/src/Acheve.TestHost/Acheve.TestHost.csproj +++ b/src/Acheve.TestHost/Acheve.TestHost.csproj @@ -27,4 +27,4 @@ - \ No newline at end of file + From 71487c4016b57eac3d761dd5b5a28969da1dfad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Tue, 22 Mar 2022 19:10:47 +0100 Subject: [PATCH 09/14] fix merge --- .../HttpResponseMessageExtensions.cs | 1 + .../PrimitiveParameterActionTokenizer.cs | 31 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Acheve.TestHost/HttpResponseMessageExtensions.cs b/src/Acheve.TestHost/HttpResponseMessageExtensions.cs index 3dbbd86..b7078a4 100644 --- a/src/Acheve.TestHost/HttpResponseMessageExtensions.cs +++ b/src/Acheve.TestHost/HttpResponseMessageExtensions.cs @@ -1,3 +1,4 @@ +using Newtonsoft.Json; using System.Threading.Tasks; namespace System.Net.Http diff --git a/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs b/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs index 037b4ca..1160820 100644 --- a/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs +++ b/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs @@ -22,24 +22,23 @@ public void AddTokens(TestServerAction action, TestServerTokenColle var tokenName = parameters[i].Name.ToLowerInvariant(); var tokenValue = action.ArgumentValues.Any(x => x.Key == i) ? action.ArgumentValues[i].Instance : null; - if (tokenValue != null) - { - tokens.AddToken(tokenName, tokenValue.ToString(), isConventional: false); - } - } - else if (parameters[i].ParameterType.IsArray - && IsPrimitiveType(parameters[i].ParameterType.GetElementType())) + if (tokenValue != null) { - var arrayValues = (Array)action.ArgumentValues[i].Instance; + tokens.AddToken(tokenName, tokenValue.ToString(), isConventional: false); + } + } + else if (parameters[i].ParameterType.IsArray + && IsPrimitiveType(parameters[i].ParameterType.GetElementType())) + { + var arrayValues = (Array)action.ArgumentValues[i].Instance; - if (arrayValues != null - && arrayValues.Length != 0 - ) - { - var tokenName = parameters[i].Name.ToLowerInvariant(); - var tokenValue = GetTokenValue(arrayValues, tokenName); - tokens.AddToken(tokenName, tokenValue, isConventional: false); - } + if (arrayValues != null + && arrayValues.Length != 0 + ) + { + var tokenName = parameters[i].Name.ToLowerInvariant(); + var tokenValue = GetTokenValue(arrayValues, tokenName); + tokens.AddToken(tokenName, tokenValue, isConventional: false); } } } From 83fae1621b4819995ba6765ea00df839f78325ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Mon, 28 Mar 2022 09:03:50 +0200 Subject: [PATCH 10/14] Change class name and access --- .../Tokenizers/{PrimitiveTypeHelper.cs => TypeExtensions.cs} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/Acheve.TestHost/Routing/Tokenizers/{PrimitiveTypeHelper.cs => TypeExtensions.cs} (75%) diff --git a/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveTypeHelper.cs b/src/Acheve.TestHost/Routing/Tokenizers/TypeExtensions.cs similarity index 75% rename from src/Acheve.TestHost/Routing/Tokenizers/PrimitiveTypeHelper.cs rename to src/Acheve.TestHost/Routing/Tokenizers/TypeExtensions.cs index fe72af9..1813f54 100644 --- a/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveTypeHelper.cs +++ b/src/Acheve.TestHost/Routing/Tokenizers/TypeExtensions.cs @@ -2,9 +2,9 @@ namespace Acheve.TestHost.Routing.Tokenizers { - public static class PrimitiveTypeHelper + internal static class TypeExtensions { - public static bool IsPrimitiveType(this Type typeToInspect) + internal static bool IsPrimitiveType(this Type typeToInspect) { var type = Nullable.GetUnderlyingType(typeToInspect) ?? typeToInspect; From 105dc65040b6233b84e4c97561738c375e8ce8fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Mon, 28 Mar 2022 09:26:17 +0200 Subject: [PATCH 11/14] Pr Comments --- src/Acheve.TestHost/HttpResponseMessageExtensions.cs | 6 ++++++ .../Routing/Tokenizers/ComplexParameterActionTokenizer.cs | 1 - .../Tokenizers/PrimitiveParameterActionTokenizer.cs | 8 +++++--- src/Acheve.TestHost/Routing/Tokenizers/TypeExtensions.cs | 4 +--- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Acheve.TestHost/HttpResponseMessageExtensions.cs b/src/Acheve.TestHost/HttpResponseMessageExtensions.cs index b7078a4..b5702ee 100644 --- a/src/Acheve.TestHost/HttpResponseMessageExtensions.cs +++ b/src/Acheve.TestHost/HttpResponseMessageExtensions.cs @@ -22,6 +22,12 @@ public static async Task IsSuccessStatusCodeOrThrow(this HttpResponseMessage res throw new Exception($"Response status does not indicate success: {response.StatusCode:D} ({response.StatusCode}); \r\n{content}"); } + /// + /// Deserialize HttpResponseMessage to T Class + /// + /// Class + /// The httpResponseMessage instance + /// public static async Task ReadContentAsAsync(this HttpResponseMessage responseMessage) { var json = await responseMessage.Content.ReadAsStringAsync(); diff --git a/src/Acheve.TestHost/Routing/Tokenizers/ComplexParameterActionTokenizer.cs b/src/Acheve.TestHost/Routing/Tokenizers/ComplexParameterActionTokenizer.cs index 683e365..2612011 100644 --- a/src/Acheve.TestHost/Routing/Tokenizers/ComplexParameterActionTokenizer.cs +++ b/src/Acheve.TestHost/Routing/Tokenizers/ComplexParameterActionTokenizer.cs @@ -18,7 +18,6 @@ public void AddTokens(TestServerAction action, TestServerTokenColle var type = parameters[i].ParameterType; var instance = action.ArgumentValues.Any(x => x.Key == i) ? action.ArgumentValues[i].Instance : null; - if (instance != null && !type.IsPrimitiveType()) { if (!IgnoreBind(parameters[i])) diff --git a/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs b/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs index 1160820..962a9ac 100644 --- a/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs +++ b/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs @@ -16,10 +16,12 @@ public void AddTokens(TestServerAction action, TestServerTokenColle for (var i = 0; i < parameters.Length; i++) { + var tokenName = parameters[i].Name.ToLowerInvariant(); + if (parameters[i].ParameterType.IsPrimitiveType() && !IgnoreHeader(parameters[i])) { - var tokenName = parameters[i].Name.ToLowerInvariant(); + var tokenValue = action.ArgumentValues.Any(x => x.Key == i) ? action.ArgumentValues[i].Instance : null; if (tokenValue != null) @@ -28,7 +30,8 @@ public void AddTokens(TestServerAction action, TestServerTokenColle } } else if (parameters[i].ParameterType.IsArray - && IsPrimitiveType(parameters[i].ParameterType.GetElementType())) + && IsPrimitiveType(parameters[i].ParameterType.GetElementType()) + && !IgnoreHeader(parameters[i])) { var arrayValues = (Array)action.ArgumentValues[i].Instance; @@ -36,7 +39,6 @@ public void AddTokens(TestServerAction action, TestServerTokenColle && arrayValues.Length != 0 ) { - var tokenName = parameters[i].Name.ToLowerInvariant(); var tokenValue = GetTokenValue(arrayValues, tokenName); tokens.AddToken(tokenName, tokenValue, isConventional: false); } diff --git a/src/Acheve.TestHost/Routing/Tokenizers/TypeExtensions.cs b/src/Acheve.TestHost/Routing/Tokenizers/TypeExtensions.cs index 1813f54..e45adf5 100644 --- a/src/Acheve.TestHost/Routing/Tokenizers/TypeExtensions.cs +++ b/src/Acheve.TestHost/Routing/Tokenizers/TypeExtensions.cs @@ -1,6 +1,4 @@ -using System; - -namespace Acheve.TestHost.Routing.Tokenizers +namespace System { internal static class TypeExtensions { From e1a2ccd1c5047590bdaeaf86b6a9d58c7ffce42f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Mon, 28 Mar 2022 09:27:22 +0200 Subject: [PATCH 12/14] better comment method --- src/Acheve.TestHost/HttpResponseMessageExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Acheve.TestHost/HttpResponseMessageExtensions.cs b/src/Acheve.TestHost/HttpResponseMessageExtensions.cs index b5702ee..edf32c9 100644 --- a/src/Acheve.TestHost/HttpResponseMessageExtensions.cs +++ b/src/Acheve.TestHost/HttpResponseMessageExtensions.cs @@ -23,7 +23,7 @@ public static async Task IsSuccessStatusCodeOrThrow(this HttpResponseMessage res } /// - /// Deserialize HttpResponseMessage to T Class + /// Read HttpResponseMessage and convert to T Class /// /// Class /// The httpResponseMessage instance From 078064d7e0149b3f9454ccc6704b68e81695855f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Mon, 28 Mar 2022 09:30:17 +0200 Subject: [PATCH 13/14] comment --- src/Acheve.TestHost/HttpResponseMessageExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Acheve.TestHost/HttpResponseMessageExtensions.cs b/src/Acheve.TestHost/HttpResponseMessageExtensions.cs index edf32c9..601e1de 100644 --- a/src/Acheve.TestHost/HttpResponseMessageExtensions.cs +++ b/src/Acheve.TestHost/HttpResponseMessageExtensions.cs @@ -27,7 +27,7 @@ public static async Task IsSuccessStatusCodeOrThrow(this HttpResponseMessage res /// /// Class /// The httpResponseMessage instance - /// + /// T class object public static async Task ReadContentAsAsync(this HttpResponseMessage responseMessage) { var json = await responseMessage.Content.ReadAsStringAsync(); From a8c38882db9d49f1c34de3f12e3c14a433250673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20Martin=20Pe=C3=B1a?= Date: Mon, 28 Mar 2022 09:35:11 +0200 Subject: [PATCH 14/14] improve performance --- .../PrimitiveParameterActionTokenizer.cs | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs b/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs index 962a9ac..2de578c 100644 --- a/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs +++ b/src/Acheve.TestHost/Routing/Tokenizers/PrimitiveParameterActionTokenizer.cs @@ -16,31 +16,31 @@ public void AddTokens(TestServerAction action, TestServerTokenColle for (var i = 0; i < parameters.Length; i++) { - var tokenName = parameters[i].Name.ToLowerInvariant(); - - if (parameters[i].ParameterType.IsPrimitiveType() - && !IgnoreHeader(parameters[i])) + if (!IgnoreHeader(parameters[i])) { - - var tokenValue = action.ArgumentValues.Any(x => x.Key == i) ? action.ArgumentValues[i].Instance : null; + var tokenName = parameters[i].Name.ToLowerInvariant(); - if (tokenValue != null) + if (parameters[i].ParameterType.IsPrimitiveType()) { - tokens.AddToken(tokenName, tokenValue.ToString(), isConventional: false); - } - } - else if (parameters[i].ParameterType.IsArray - && IsPrimitiveType(parameters[i].ParameterType.GetElementType()) - && !IgnoreHeader(parameters[i])) - { - var arrayValues = (Array)action.ArgumentValues[i].Instance; + var tokenValue = action.ArgumentValues.Any(x => x.Key == i) ? action.ArgumentValues[i].Instance : null; - if (arrayValues != null - && arrayValues.Length != 0 - ) + if (tokenValue != null) + { + tokens.AddToken(tokenName, tokenValue.ToString(), isConventional: false); + } + } + else if (parameters[i].ParameterType.IsArray + && IsPrimitiveType(parameters[i].ParameterType.GetElementType())) { - var tokenValue = GetTokenValue(arrayValues, tokenName); - tokens.AddToken(tokenName, tokenValue, isConventional: false); + var arrayValues = (Array)action.ArgumentValues[i].Instance; + + if (arrayValues != null + && arrayValues.Length != 0 + ) + { + var tokenValue = GetTokenValue(arrayValues, tokenName); + tokens.AddToken(tokenName, tokenValue, isConventional: false); + } } } }