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(