Skip to content

Make InputFormatters and OutputFormatters implement IApiRequestFormatMetadataProvider #967

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

Closed
wants to merge 10 commits into from
38 changes: 38 additions & 0 deletions src/JsonApiDotNetCore/Middleware/JsonApiFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using JsonApiDotNetCore.Resources;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Net.Http.Headers;

namespace JsonApiDotNetCore.Middleware
{
/// <summary>
/// Application-wide entry point for writing JSON:API response bodies.
/// </summary>
public abstract class JsonApiFormatter : IApiRequestFormatMetadataProvider
{
private readonly Type _operationContainerType = typeof(OperationContainer);

/// <inheritdoc />
public IReadOnlyList<string> GetSupportedContentTypes(string contentType, Type objectType)
{
ArgumentGuard.NotNull(contentType, nameof(contentType));
ArgumentGuard.NotNull(objectType, nameof(objectType));

string mediaType = IsAtomicOperationsType(objectType) ? HeaderConstants.AtomicOperationsMediaType : HeaderConstants.MediaType;

return new MediaTypeCollection
{
new MediaTypeHeaderValue(mediaType)
};
}

private bool IsAtomicOperationsType(Type objectType)
{
return objectType.GetInterface(nameof(IEnumerable)) != null && objectType.GetGenericArguments().First() == _operationContainerType;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to try why such complex logic is needed, instead of the obvious typeof(IList<OperationContainer>), but there are no tests. Add some tests first, please.

}
}
}
4 changes: 2 additions & 2 deletions src/JsonApiDotNetCore/Middleware/JsonApiInputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace JsonApiDotNetCore.Middleware
{
/// <inheritdoc />
public sealed class JsonApiInputFormatter : IJsonApiInputFormatter
/// <inheritdoc cref="JsonApiDotNetCore.Middleware.IJsonApiInputFormatter" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the cref is to disambiguate between the docs on JsonApiFormatter and IJsonApiInputFormatter. We haven't used this before, but perhaps we should start doing that. Does this work correctly in:

  • the generated documentation website?
  • IntelliSense in IDE when consuming our NuGet?

public sealed class JsonApiInputFormatter : JsonApiFormatter, IJsonApiInputFormatter
{
/// <inheritdoc />
public bool CanRead(InputFormatterContext context)
Expand Down
4 changes: 2 additions & 2 deletions src/JsonApiDotNetCore/Middleware/JsonApiOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace JsonApiDotNetCore.Middleware
{
/// <inheritdoc />
public sealed class JsonApiOutputFormatter : IJsonApiOutputFormatter
/// <inheritdoc cref="JsonApiDotNetCore.Middleware.IJsonApiInputFormatter" />
public sealed class JsonApiOutputFormatter : JsonApiFormatter, IJsonApiOutputFormatter
{
/// <inheritdoc />
public bool CanWriteResult(OutputFormatterCanWriteContext context)
Expand Down