Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement InputFormatter.GetSupportedContentTypes on SystemTextJsonMergePatchInputFormatter #67

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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