|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using JsonApiDotNetCore.Middleware; |
| 4 | +using JsonApiDotNetCore.Resources; |
| 5 | +using Microsoft.AspNetCore.Http; |
| 6 | +using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | + |
| 9 | +namespace JsonApiDotNetCore.Configuration |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Validation filter that blocks ASP.NET Core ModelState validation on data according to the json:api spec. |
| 13 | + /// </summary> |
| 14 | + internal sealed class JsonApiValidationFilter : IPropertyValidationFilter |
| 15 | + { |
| 16 | + private readonly IRequestScopedServiceProvider _serviceProvider; |
| 17 | + |
| 18 | + public JsonApiValidationFilter(IRequestScopedServiceProvider serviceProvider) |
| 19 | + { |
| 20 | + _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); |
| 21 | + } |
| 22 | + |
| 23 | + /// <inheritdoc /> |
| 24 | + public bool ShouldValidateEntry(ValidationEntry entry, ValidationEntry parentEntry) |
| 25 | + { |
| 26 | + var request = _serviceProvider.GetRequiredService<IJsonApiRequest>(); |
| 27 | + |
| 28 | + if (IsId(entry.Key)) |
| 29 | + { |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + var isTopResourceInPrimaryRequest = string.IsNullOrEmpty(parentEntry.Key) && request.Kind == EndpointKind.Primary; |
| 34 | + if (!isTopResourceInPrimaryRequest) |
| 35 | + { |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + var httpContextAccessor = _serviceProvider.GetRequiredService<IHttpContextAccessor>(); |
| 40 | + if (httpContextAccessor.HttpContext.Request.Method == HttpMethods.Patch) |
| 41 | + { |
| 42 | + var targetedFields = _serviceProvider.GetRequiredService<ITargetedFields>(); |
| 43 | + return IsFieldTargeted(entry, targetedFields); |
| 44 | + } |
| 45 | + |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + private static bool IsId(string key) |
| 50 | + { |
| 51 | + return key == nameof(Identifiable.Id) || key.EndsWith("." + nameof(Identifiable.Id), StringComparison.Ordinal); |
| 52 | + } |
| 53 | + |
| 54 | + private static bool IsFieldTargeted(ValidationEntry entry, ITargetedFields targetedFields) |
| 55 | + { |
| 56 | + return targetedFields.Attributes.Any(attribute => attribute.Property.Name == entry.Key); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments