From 67edb7f29d32bc1de2214bdaefd549d1fd39525a Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Sat, 21 Apr 2018 17:54:56 -0500 Subject: [PATCH 1/4] make private service methods protected virtual --- src/JsonApiDotNetCore/JsonApiDotNetCore.csproj | 4 ++-- src/JsonApiDotNetCore/Services/EntityResourceService.cs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj b/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj index c4b9a6d932..8cd4435ad8 100755 --- a/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj +++ b/src/JsonApiDotNetCore/JsonApiDotNetCore.csproj @@ -1,6 +1,6 @@  - 2.2.1 + 2.2.2 $(NetStandardVersion) JsonApiDotNetCore JsonApiDotNetCore @@ -35,4 +35,4 @@ - \ No newline at end of file + diff --git a/src/JsonApiDotNetCore/Services/EntityResourceService.cs b/src/JsonApiDotNetCore/Services/EntityResourceService.cs index 91eabb9cfd..bc7a2adb52 100644 --- a/src/JsonApiDotNetCore/Services/EntityResourceService.cs +++ b/src/JsonApiDotNetCore/Services/EntityResourceService.cs @@ -146,7 +146,7 @@ public virtual async Task DeleteAsync(TId id) return await _entities.DeleteAsync(id); } - private IQueryable ApplySortAndFilterQuery(IQueryable entities) + protected virtual IQueryable ApplySortAndFilterQuery(IQueryable entities) { var query = _jsonApiContext.QuerySet; @@ -163,7 +163,7 @@ private IQueryable ApplySortAndFilterQuery(IQueryable entities) return entities; } - private async Task> ApplyPageQueryAsync(IQueryable entities) + protected virtual async Task> ApplyPageQueryAsync(IQueryable entities) { var pageManager = _jsonApiContext.PageManager; if (!pageManager.IsPaginated) @@ -174,7 +174,7 @@ private async Task> ApplyPageQueryAsync(IQueryable entities) return await _entities.PageAsync(entities, pageManager.PageSize, pageManager.CurrentPage); } - private IQueryable IncludeRelationships(IQueryable entities, List relationships) + protected virtual IQueryable IncludeRelationships(IQueryable entities, List relationships) { _jsonApiContext.IncludedRelationships = relationships; From 1c80eb72a524a806d0fdc49b9e7aff34b681c86a Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Sat, 21 Apr 2018 18:05:11 -0500 Subject: [PATCH 2/4] allow custom attribute filters --- .../Extensions/IQueryableExtensions.cs | 2 +- .../Internal/Query/AttrFilterQuery.cs | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs b/src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs index 73e3a14a3e..4d7be752e3 100644 --- a/src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs +++ b/src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs @@ -90,7 +90,7 @@ public static IQueryable Filter(this IQueryable sourc public static IQueryable Filter(this IQueryable source, AttrFilterQuery filterQuery) { - if (filterQuery == null) + if (filterQuery == null || filterQuery.IsAttribute == false) return source; var concreteType = typeof(TSource); diff --git a/src/JsonApiDotNetCore/Internal/Query/AttrFilterQuery.cs b/src/JsonApiDotNetCore/Internal/Query/AttrFilterQuery.cs index 59bb3f0f83..c8dee16828 100644 --- a/src/JsonApiDotNetCore/Internal/Query/AttrFilterQuery.cs +++ b/src/JsonApiDotNetCore/Internal/Query/AttrFilterQuery.cs @@ -18,7 +18,9 @@ public AttrFilterQuery( var attribute = GetAttribute(filterQuery.Attribute); if (attribute == null) - throw new JsonApiException(400, $"'{filterQuery.Attribute}' is not a valid attribute."); + return; // we don't want to throw...we should allow custom filter implementations + + IsAttribute = true; if (attribute.IsFilterable == false) throw new JsonApiException(400, $"Filter is not allowed for attribute '{attribute.PublicAttributeName}'."); @@ -32,6 +34,12 @@ public AttrFilterQuery( public string PropertyValue { get; } public FilterOperations FilterOperation { get; } + /// + /// Whether or not the filter is an actual attribute on the model. + /// We use this to allow custom filters that have to be handled by the application. + /// + internal bool IsAttribute { get; set; } + private AttrAttribute GetAttribute(string attribute) => _jsonApiContext.RequestEntity.Attributes.FirstOrDefault(attr => attr.Is(attribute)); } From 3fd2e7f5f599d9ce3fdac5464baa62850de93891 Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Sat, 21 Apr 2018 18:26:05 -0500 Subject: [PATCH 3/4] add ctor without logger factory --- src/JsonApiDotNetCore/Controllers/JsonApiController.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/JsonApiDotNetCore/Controllers/JsonApiController.cs b/src/JsonApiDotNetCore/Controllers/JsonApiController.cs index 97350127ba..929e76e5aa 100644 --- a/src/JsonApiDotNetCore/Controllers/JsonApiController.cs +++ b/src/JsonApiDotNetCore/Controllers/JsonApiController.cs @@ -17,6 +17,12 @@ public JsonApiController( : base(jsonApiContext, resourceService, loggerFactory) { } + public JsonApiController( + IJsonApiContext jsonApiContext, + IResourceService resourceService) + : base(jsonApiContext, resourceService) + { } + public JsonApiController( IJsonApiContext jsonApiContext, IGetAllService getAll = null, From e65ad9200c958d09535b08e6cfbb3687f4aefc9d Mon Sep 17 00:00:00 2001 From: jaredcnance Date: Sat, 21 Apr 2018 18:05:11 -0500 Subject: [PATCH 4/4] Revert "allow custom attribute filters" This reverts commit 1c80eb72a524a806d0fdc49b9e7aff34b681c86a. --- .../Extensions/IQueryableExtensions.cs | 2 +- .../Internal/Query/AttrFilterQuery.cs | 10 +--------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs b/src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs index 4d7be752e3..73e3a14a3e 100644 --- a/src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs +++ b/src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs @@ -90,7 +90,7 @@ public static IQueryable Filter(this IQueryable sourc public static IQueryable Filter(this IQueryable source, AttrFilterQuery filterQuery) { - if (filterQuery == null || filterQuery.IsAttribute == false) + if (filterQuery == null) return source; var concreteType = typeof(TSource); diff --git a/src/JsonApiDotNetCore/Internal/Query/AttrFilterQuery.cs b/src/JsonApiDotNetCore/Internal/Query/AttrFilterQuery.cs index c8dee16828..59bb3f0f83 100644 --- a/src/JsonApiDotNetCore/Internal/Query/AttrFilterQuery.cs +++ b/src/JsonApiDotNetCore/Internal/Query/AttrFilterQuery.cs @@ -18,9 +18,7 @@ public AttrFilterQuery( var attribute = GetAttribute(filterQuery.Attribute); if (attribute == null) - return; // we don't want to throw...we should allow custom filter implementations - - IsAttribute = true; + throw new JsonApiException(400, $"'{filterQuery.Attribute}' is not a valid attribute."); if (attribute.IsFilterable == false) throw new JsonApiException(400, $"Filter is not allowed for attribute '{attribute.PublicAttributeName}'."); @@ -34,12 +32,6 @@ public AttrFilterQuery( public string PropertyValue { get; } public FilterOperations FilterOperation { get; } - /// - /// Whether or not the filter is an actual attribute on the model. - /// We use this to allow custom filters that have to be handled by the application. - /// - internal bool IsAttribute { get; set; } - private AttrAttribute GetAttribute(string attribute) => _jsonApiContext.RequestEntity.Attributes.FirstOrDefault(attr => attr.Is(attribute)); }