Skip to content

Commit b3cdc7a

Browse files
committed
Removed JsonApiInputFormatter workaround
1 parent ef2753e commit b3cdc7a

File tree

3 files changed

+56
-49
lines changed

3 files changed

+56
-49
lines changed

src/JsonApiDotNetCore.OpenApi/JsonApiInputFormatterWithMetadata.cs

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using JsonApiDotNetCore.Middleware;
6+
using JsonApiDotNetCore.OpenApi.JsonApiObjects.Documents;
7+
using JsonApiDotNetCore.OpenApi.JsonApiObjects.RelationshipData;
8+
using Microsoft.AspNetCore.Mvc.ApiExplorer;
9+
using Microsoft.AspNetCore.Mvc.Formatters;
10+
using Microsoft.Net.Http.Headers;
11+
12+
namespace JsonApiDotNetCore.OpenApi
13+
{
14+
internal sealed class JsonApiRequestFormatMetadataProvider : IInputFormatter, IApiRequestFormatMetadataProvider
15+
{
16+
private static readonly Type[] JsonApiRequestObjectOpenType =
17+
{
18+
typeof(ToManyRelationshipRequestData<>),
19+
typeof(ToOneRelationshipRequestData<>),
20+
typeof(ResourcePostRequestDocument<>),
21+
typeof(ResourcePatchRequestDocument<>)
22+
};
23+
24+
/// <inheritdoc />
25+
public bool CanRead(InputFormatterContext context)
26+
{
27+
return false;
28+
}
29+
30+
/// <inheritdoc />
31+
public Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
32+
{
33+
throw new UnreachableCodeException();
34+
}
35+
36+
/// <inheritdoc />
37+
public IReadOnlyList<string> GetSupportedContentTypes(string contentType, Type objectType)
38+
{
39+
ArgumentGuard.NotNullNorEmpty(contentType, nameof(contentType));
40+
ArgumentGuard.NotNull(objectType, nameof(objectType));
41+
42+
if (contentType == HeaderConstants.MediaType && objectType.IsGenericType &&
43+
JsonApiRequestObjectOpenType.Contains(objectType.GetGenericTypeDefinition()))
44+
{
45+
return new MediaTypeCollection
46+
{
47+
new MediaTypeHeaderValue(HeaderConstants.MediaType)
48+
};
49+
}
50+
51+
return new MediaTypeCollection();
52+
}
53+
}
54+
}

src/JsonApiDotNetCore.OpenApi/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public static void AddOpenApi(this IServiceCollection services, IMvcCoreBuilder
3232
AddSwaggerGenerator(scope, services, setupSwaggerGenAction);
3333
AddSwashbuckleCliCompatibility(scope, mvcBuilder);
3434
AddOpenApiEndpointConvention(scope, mvcBuilder);
35-
36-
AddJsonApiInputFormatterWorkaround(mvcBuilder);
3735
}
3836

3937
private static void AddCustomApiExplorer(IServiceCollection services, IMvcCoreBuilder mvcBuilder)
@@ -52,6 +50,8 @@ private static void AddCustomApiExplorer(IServiceCollection services, IMvcCoreBu
5250
});
5351

5452
mvcBuilder.AddApiExplorer();
53+
54+
mvcBuilder.AddMvcOptions(options => options.InputFormatters.Add(new JsonApiRequestFormatMetadataProvider()));
5555
}
5656

5757
private static void AddSwaggerGenerator(IServiceScope scope, IServiceCollection services, Action<SwaggerGenOptions> setupSwaggerGenAction)
@@ -132,11 +132,5 @@ private static void AddOpenApiEndpointConvention(IServiceScope scope, IMvcCoreBu
132132

133133
mvcBuilder.AddMvcOptions(options => options.Conventions.Add(new OpenApiEndpointConvention(resourceContextProvider, controllerResourceMapping)));
134134
}
135-
136-
private static void AddJsonApiInputFormatterWorkaround(IMvcCoreBuilder mvcBuilder)
137-
{
138-
// See https://github.com/json-api-dotnet/JsonApiDotNetCore/pull/972 for why this is needed.
139-
mvcBuilder.AddMvcOptions(options => options.InputFormatters.Add(new JsonApiInputFormatterWithMetadata()));
140-
}
141135
}
142136
}

0 commit comments

Comments
 (0)