-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add ModelBinder support * feat:Simplify PatchObjectModelBinder
- Loading branch information
Showing
9 changed files
with
96 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,4 @@ public IEnumerable<string> Patch([FromBody] Patch<Person> person) | |
}; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Dynamic; | ||
|
||
namespace Simple.HttpPatch; | ||
|
||
public class DynamicMemberBinder : SetMemberBinder | ||
{ | ||
public DynamicMemberBinder(string name, bool ignoreCase) : base(name, ignoreCase) | ||
{ | ||
} | ||
|
||
public override DynamicMetaObject FallbackSetMember(DynamicMetaObject target, DynamicMetaObject value, | ||
DynamicMetaObject errorSuggestion) | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ namespace Simple.HttpPatch | |
public class PatchIgnoreAttribute : Attribute | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using System; | ||
|
||
namespace Simple.HttpPatch; | ||
|
||
public class PatchModelBinderProvider:IModelBinderProvider | ||
{ | ||
public IModelBinder GetBinder(ModelBinderProviderContext context) | ||
{ | ||
if (context.Metadata is { IsComplexType: true, ModelType.IsGenericType: true } && | ||
context.Metadata.ModelType.GetGenericTypeDefinition() == typeof(Patch<>)) | ||
{ | ||
var modelType = context.Metadata.ModelType.GenericTypeArguments[0]; | ||
var binderType = typeof(PatchObjectModelBinder<>).MakeGenericType(modelType); | ||
return (IModelBinder)Activator.CreateInstance(binderType); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Microsoft.AspNetCore.Mvc.Formatters; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Dynamic; | ||
using System.IO; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace Simple.HttpPatch; | ||
|
||
public class PatchObjectModelBinder<T> : IModelBinder where T : class | ||
{ | ||
public async Task BindModelAsync(ModelBindingContext bindingContext) | ||
{ | ||
if (bindingContext == null) | ||
{ | ||
throw new ArgumentNullException(nameof(bindingContext)); | ||
} | ||
|
||
using (var reader = new StreamReader(bindingContext.HttpContext.Request.Body)) | ||
{ | ||
var json = await reader.ReadToEndAsync(); | ||
|
||
dynamic dynamicObject = Activator.CreateInstance(typeof(Patch<T>)); | ||
|
||
using (JsonDocument doc = JsonDocument.Parse(json)) | ||
{ | ||
foreach (var property in doc.RootElement.EnumerateObject()) | ||
{ | ||
dynamicObject.TrySetMember(new DynamicMemberBinder(property.Name, true), property.Value.ToString()); | ||
} | ||
} | ||
|
||
bindingContext.Result = ModelBindingResult.Success(dynamicObject); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters