From 7aa924ec28d94e0dec6133a90fe2ffb83424a191 Mon Sep 17 00:00:00 2001 From: Misiu Date: Thu, 10 Mar 2022 11:21:44 +0100 Subject: [PATCH 1/2] support json property name attributes --- .../Extensions/PropertyInfoExtensions.cs | 30 +++++++++++++++++++ .../HybridModelBinding.csproj | 1 + .../ModelBinding/BodyValueProvider.cs | 11 ++++--- 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/HybridModelBinding/Extensions/PropertyInfoExtensions.cs diff --git a/src/HybridModelBinding/Extensions/PropertyInfoExtensions.cs b/src/HybridModelBinding/Extensions/PropertyInfoExtensions.cs new file mode 100644 index 0000000..03910db --- /dev/null +++ b/src/HybridModelBinding/Extensions/PropertyInfoExtensions.cs @@ -0,0 +1,30 @@ +using System.Reflection; +using System.Text.Json.Serialization; +using Newtonsoft.Json; + +namespace HybridModelBinding.Extensions +{ + public static class PropertyInfoExtensions + { + public static string GetNameFromAttributes(this PropertyInfo propertyInfo) + { + //default value + var name = propertyInfo.Name; + + //newtonsoft.json + var jsonPropertyAttribute = propertyInfo.GetCustomAttribute(); + if (jsonPropertyAttribute != null) + { + return jsonPropertyAttribute.PropertyName; + } + //system.text.json + var jsonPropertyNameAttribute = propertyInfo.GetCustomAttribute(); + if (jsonPropertyNameAttribute != null) + { + return jsonPropertyNameAttribute.Name; + } + + return name; + } + } +} \ No newline at end of file diff --git a/src/HybridModelBinding/HybridModelBinding.csproj b/src/HybridModelBinding/HybridModelBinding.csproj index bd6d87f..a193978 100755 --- a/src/HybridModelBinding/HybridModelBinding.csproj +++ b/src/HybridModelBinding/HybridModelBinding.csproj @@ -22,6 +22,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all + diff --git a/src/HybridModelBinding/ModelBinding/BodyValueProvider.cs b/src/HybridModelBinding/ModelBinding/BodyValueProvider.cs index 8fd6f8f..61bd0c9 100755 --- a/src/HybridModelBinding/ModelBinding/BodyValueProvider.cs +++ b/src/HybridModelBinding/ModelBinding/BodyValueProvider.cs @@ -41,10 +41,13 @@ public BodyValueProvider( .ToArray(); } - foreach (var property in model.GetPropertiesNotPartOfType() - .Where(x => !requestKeys.Any() || requestKeys.Contains(x.Name, StringComparer.OrdinalIgnoreCase))) + foreach (var property in model.GetPropertiesNotPartOfType()) { - values.Add(property.Name, property.GetValue(model, null)); + var propertyName = property.GetNameFromAttributes(); + if (!requestKeys.Any() || requestKeys.Contains(propertyName, StringComparer.OrdinalIgnoreCase)) + { + values.Add(property.Name, property.GetValue(model, null)); + } } } @@ -78,4 +81,4 @@ public object GetObject(string key) public ValueProviderResult GetValue(string key) => throw new NotImplementedException($"Use `{nameof(GetObject)}`."); } -} +} \ No newline at end of file From b9b530741c46bc84113c12b9d48cf3b014b629c0 Mon Sep 17 00:00:00 2001 From: Misiu Date: Thu, 10 Mar 2022 11:55:58 +0100 Subject: [PATCH 2/2] adjust demo --- .../AspNetCoreWebApplication.csproj | 1 + .../Controllers/DataController.cs | 31 +++++++++++++++++++ samples/AspNetCoreWebApplication/Startup.cs | 10 +++--- 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 samples/AspNetCoreWebApplication/Controllers/DataController.cs diff --git a/samples/AspNetCoreWebApplication/AspNetCoreWebApplication.csproj b/samples/AspNetCoreWebApplication/AspNetCoreWebApplication.csproj index 6d940eb..b6cf54e 100755 --- a/samples/AspNetCoreWebApplication/AspNetCoreWebApplication.csproj +++ b/samples/AspNetCoreWebApplication/AspNetCoreWebApplication.csproj @@ -3,6 +3,7 @@ netcoreapp3.1 + diff --git a/samples/AspNetCoreWebApplication/Controllers/DataController.cs b/samples/AspNetCoreWebApplication/Controllers/DataController.cs new file mode 100644 index 0000000..768d734 --- /dev/null +++ b/samples/AspNetCoreWebApplication/Controllers/DataController.cs @@ -0,0 +1,31 @@ +using System.Text.Json.Serialization; +using HybridModelBinding; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; + +namespace AspNetCoreWebApplication.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class DataController : ControllerBase + { + [HttpPost] + public ActionResult Index(DataModel model) + { + return $"{model.Name} - {model.IsAdmin}"; + } + } + + public class DataModel + { + [HybridBindProperty(Source.Body)] + public string Name { get; set; } + + [HybridBindProperty(Source.Body)] + [JsonPropertyName("is_admin"), JsonProperty("is_admin")] + public bool IsAdmin { get; set; } + + [HybridBindProperty(Source.Header, "X-UserId")] + public int UserId { get; set; } + } +} \ No newline at end of file diff --git a/samples/AspNetCoreWebApplication/Startup.cs b/samples/AspNetCoreWebApplication/Startup.cs index 1586411..62ba3f9 100755 --- a/samples/AspNetCoreWebApplication/Startup.cs +++ b/samples/AspNetCoreWebApplication/Startup.cs @@ -10,7 +10,8 @@ public void ConfigureServices(IServiceCollection services) { services .AddMvc() - .AddHybridModelBinder(); + .AddHybridModelBinder() + .AddNewtonsoftJson(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) @@ -27,10 +28,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseStaticFiles(); app.UseRouting(); - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - }); + app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } -} +} \ No newline at end of file