Skip to content

Commit 10628aa

Browse files
committed
feat(AttrAttribute): add isSortable and isFilterable
1 parent eb2c309 commit 10628aa

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

src/JsonApiDotNetCore/AssemblyInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using System.Runtime.CompilerServices;
2+
[assembly:InternalsVisibleTo("UnitTests")]
3+
[assembly:InternalsVisibleTo("JsonApiDotNetCoreExampleTests")]
4+
[assembly:InternalsVisibleTo("NoEntityFrameworkTests")]

src/JsonApiDotNetCore/Internal/Query/AttrFilterQuery.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ public AttrFilterQuery(
1717

1818
var attribute = GetAttribute(filterQuery.Attribute);
1919

20-
FilteredAttribute = attribute ?? throw new JsonApiException(400, $"'{filterQuery.Attribute}' is not a valid attribute.");
20+
if(attribute == null)
21+
throw new JsonApiException(400, $"'{filterQuery.Attribute}' is not a valid attribute.");
22+
23+
if(attribute.IsFilterable == false)
24+
throw new JsonApiException(400, $"Filter is not allowed for attribute '{attribute.PublicAttributeName}'.");
25+
26+
FilteredAttribute = attribute;
2127
PropertyValue = filterQuery.Value;
2228
FilterOperation = GetFilterOperation(filterQuery.Operation);
2329
}
2430

25-
public AttrAttribute FilteredAttribute { get; set; }
26-
public string PropertyValue { get; set; }
27-
public FilterOperations FilterOperation { get; set; }
31+
public AttrAttribute FilteredAttribute { get; }
32+
public string PropertyValue { get; }
33+
public FilterOperations FilterOperation { get; }
2834

2935
private AttrAttribute GetAttribute(string attribute) =>
3036
_jsonApiContext.RequestEntity.Attributes.FirstOrDefault(

src/JsonApiDotNetCore/Internal/Query/RelatedAttrFilterQuery.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ public RelatedAttrFilterQuery(
1919

2020
var relationship = GetRelationship(relationshipArray[0]);
2121
if (relationship == null)
22-
throw new JsonApiException(400, $"{relationshipArray[0]} is not a valid relationship.");
22+
throw new JsonApiException(400, $"{relationshipArray[1]} is not a valid relationship on {relationshipArray[0]}.");
2323

2424
var attribute = GetAttribute(relationship, relationshipArray[1]);
25+
26+
if(attribute == null)
27+
throw new JsonApiException(400, $"'{filterQuery.Attribute}' is not a valid attribute.");
28+
29+
if(attribute.IsFilterable == false)
30+
throw new JsonApiException(400, $"Filter is not allowed for attribute '{attribute.PublicAttributeName}'.");
2531

2632
FilteredRelationship = relationship;
27-
FilteredAttribute = attribute ?? throw new JsonApiException(400, $"{relationshipArray[1]} is not a valid attribute on {relationshipArray[0]}.");
33+
FilteredAttribute = attribute;
2834
PropertyValue = filterQuery.Value;
2935
FilterOperation = GetFilterOperation(filterQuery.Operation);
3036
}

src/JsonApiDotNetCore/Models/AttrAttribute.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
using System;
2-
using System.Reflection;
32
using JsonApiDotNetCore.Internal;
43

54
namespace JsonApiDotNetCore.Models
65
{
76
public class AttrAttribute : Attribute
87
{
9-
public AttrAttribute(string publicName, bool isImmutable = false)
8+
public AttrAttribute(string publicName, bool isImmutable = false, bool isFilterable = true, bool isSortable = true)
109
{
1110
PublicAttributeName = publicName;
1211
IsImmutable = isImmutable;
12+
IsFilterable = isFilterable;
13+
IsSortable = isSortable;
1314
}
1415

15-
public AttrAttribute(string publicName, string internalName, bool isImmutable = false)
16+
internal AttrAttribute(string publicName, string internalName, bool isImmutable = false)
1617
{
1718
PublicAttributeName = publicName;
1819
InternalAttributeName = internalName;
1920
IsImmutable = isImmutable;
2021
}
2122

22-
public string PublicAttributeName { get; set; }
23-
public string InternalAttributeName { get; set; }
24-
public bool IsImmutable { get; set; }
23+
public string PublicAttributeName { get; }
24+
public string InternalAttributeName { get; }
25+
public bool IsImmutable { get; }
26+
public bool IsFilterable { get; }
27+
public bool IsSortable { get; }
2528

2629
public object GetValue(object entity)
2730
{

src/JsonApiDotNetCore/Models/RelationshipAttribute.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ protected RelationshipAttribute(string publicName, Link documentLinks)
1010
DocumentLinks = documentLinks;
1111
}
1212

13-
public string PublicRelationshipName { get; set; }
14-
public string InternalRelationshipName { get; set; }
15-
public Type Type { get; set; }
13+
public string PublicRelationshipName { get; }
14+
public string InternalRelationshipName { get; }
15+
public Type Type { get; }
1616
public bool IsHasMany => GetType() == typeof(HasManyAttribute);
1717
public bool IsHasOne => GetType() == typeof(HasOneAttribute);
18-
public Link DocumentLinks { get; set; } = Link.All;
18+
public Link DocumentLinks { get; } = Link.All;
1919

2020
public abstract void SetValue(object entity, object newValue);
2121

src/JsonApiDotNetCore/Services/QueryParser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ protected virtual List<SortQuery> ParseSortParameters(string value)
159159

160160
var attribute = GetAttribute(propertyName);
161161

162+
if(attribute.IsSortable == false)
163+
throw new JsonApiException(400, $"Sort is not allowed for attribute '{attribute.PublicAttributeName}'.");
164+
162165
sortParameters.Add(new SortQuery(direction, attribute));
163166
};
164167

0 commit comments

Comments
 (0)