From 8715631380f06f8f76a848e5884573df2d06a331 Mon Sep 17 00:00:00 2001 From: "Kasper F. Brandt" Date: Tue, 14 Jan 2025 14:53:18 +0100 Subject: [PATCH] Implement InputFormatter.GetSupportedContentTypes on SystemTextJsonMergePatchInputFormatter --- .../SystemTextJsonMergePatchInputFormatter.cs | 24 ++++++++++++++++++- .../SystemTextJsonMergePatchInputFormatter.cs | 24 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/3.0-JsonMergePatch.SystemText/SystemTextJsonMergePatchInputFormatter.cs b/src/3.0-JsonMergePatch.SystemText/SystemTextJsonMergePatchInputFormatter.cs index 61c4294..cbf94ff 100644 --- a/src/3.0-JsonMergePatch.SystemText/SystemTextJsonMergePatchInputFormatter.cs +++ b/src/3.0-JsonMergePatch.SystemText/SystemTextJsonMergePatchInputFormatter.cs @@ -34,7 +34,19 @@ public SystemTextJsonMergePatchInputFormatter( } private static bool ContainerIsIEnumerable(InputFormatterContext context) - => context.ModelType.IsGenericType && (context.ModelType.GetGenericTypeDefinition() == typeof(IEnumerable<>)); + => GetEnumerableElementType(context.ModelType) != null; + private static Type GetEnumerableElementType(Type type) + { + if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IEnumerable<>))) + return type.GetGenericArguments()[0]; + return null; + } + private static Type GetMergePatchDocumentModelType(Type type) + { + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(JsonMergePatchDocument<>)) + return type.GetGenericArguments()[0]; + return null; + } private IInternalJsonMergePatchDocument CreatePatchDocument(Type jsonMergePatchType, Type modelType, JsonElement jsonElement) { @@ -67,6 +79,16 @@ private object ConvertToPatch(object o, IList container, Type jsonMergePatchType } } + public override IReadOnlyList GetSupportedContentTypes(string contentType, Type objectType) + { + if (GetMergePatchDocumentModelType(objectType) != null || + (GetEnumerableElementType(objectType) is Type elementType && GetMergePatchDocumentModelType(elementType) != null)) + { + return base.GetSupportedContentTypes(contentType, objectType); + } + return Array.Empty(); + } + public override async Task ReadRequestBodyAsync(InputFormatterContext context) { var patchContext = new InputFormatterContext( diff --git a/src/6.0-JsonMergePatch.SystemText/SystemTextJsonMergePatchInputFormatter.cs b/src/6.0-JsonMergePatch.SystemText/SystemTextJsonMergePatchInputFormatter.cs index 61c4294..cbf94ff 100644 --- a/src/6.0-JsonMergePatch.SystemText/SystemTextJsonMergePatchInputFormatter.cs +++ b/src/6.0-JsonMergePatch.SystemText/SystemTextJsonMergePatchInputFormatter.cs @@ -34,7 +34,19 @@ public SystemTextJsonMergePatchInputFormatter( } private static bool ContainerIsIEnumerable(InputFormatterContext context) - => context.ModelType.IsGenericType && (context.ModelType.GetGenericTypeDefinition() == typeof(IEnumerable<>)); + => GetEnumerableElementType(context.ModelType) != null; + private static Type GetEnumerableElementType(Type type) + { + if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IEnumerable<>))) + return type.GetGenericArguments()[0]; + return null; + } + private static Type GetMergePatchDocumentModelType(Type type) + { + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(JsonMergePatchDocument<>)) + return type.GetGenericArguments()[0]; + return null; + } private IInternalJsonMergePatchDocument CreatePatchDocument(Type jsonMergePatchType, Type modelType, JsonElement jsonElement) { @@ -67,6 +79,16 @@ private object ConvertToPatch(object o, IList container, Type jsonMergePatchType } } + public override IReadOnlyList GetSupportedContentTypes(string contentType, Type objectType) + { + if (GetMergePatchDocumentModelType(objectType) != null || + (GetEnumerableElementType(objectType) is Type elementType && GetMergePatchDocumentModelType(elementType) != null)) + { + return base.GetSupportedContentTypes(contentType, objectType); + } + return Array.Empty(); + } + public override async Task ReadRequestBodyAsync(InputFormatterContext context) { var patchContext = new InputFormatterContext(