Skip to content

Commit

Permalink
Implement InputFormatter.GetSupportedContentTypes on SystemTextJsonMe…
Browse files Browse the repository at this point in the history
…rgePatchInputFormatter (#67)
  • Loading branch information
poizan42 authored Jan 15, 2025
1 parent 09d5971 commit ba24e85
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -67,6 +79,16 @@ private object ConvertToPatch(object o, IList container, Type jsonMergePatchType
}
}

public override IReadOnlyList<string> 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<string>();
}

public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
{
var patchContext = new InputFormatterContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -67,6 +79,16 @@ private object ConvertToPatch(object o, IList container, Type jsonMergePatchType
}
}

public override IReadOnlyList<string> 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<string>();
}

public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
{
var patchContext = new InputFormatterContext(
Expand Down

0 comments on commit ba24e85

Please sign in to comment.