|
| 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 | +} |
0 commit comments