Skip to content

Commit 1c96572

Browse files
committed
feat(FilterQuery / QueryAccessor): use public attribute names for keys
Closes #197
1 parent 865cbba commit 1c96572

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Linq;
23
using JsonApiDotNetCore.Models;
34
using JsonApiDotNetCore.Services;
@@ -9,14 +10,14 @@ public class AttrFilterQuery : BaseFilterQuery
910
private readonly IJsonApiContext _jsonApiContext;
1011

1112
public AttrFilterQuery(
12-
IJsonApiContext jsonApiCopntext,
13+
IJsonApiContext jsonApiContext,
1314
FilterQuery filterQuery)
1415
{
15-
_jsonApiContext = jsonApiCopntext;
16+
_jsonApiContext = jsonApiContext;
1617

17-
var attribute = GetAttribute(filterQuery.Key);
18+
var attribute = GetAttribute(filterQuery.Attribute);
1819

19-
FilteredAttribute = attribute ?? throw new JsonApiException(400, $"{filterQuery.Key} is not a valid property.");
20+
FilteredAttribute = attribute ?? throw new JsonApiException(400, $"'{filterQuery.Attribute}' is not a valid attribute.");
2021
PropertyValue = filterQuery.Value;
2122
FilterOperation = GetFilterOperation(filterQuery.Operation);
2223
}
@@ -25,12 +26,9 @@ public AttrFilterQuery(
2526
public string PropertyValue { get; set; }
2627
public FilterOperations FilterOperation { get; set; }
2728

28-
private AttrAttribute GetAttribute(string propertyName)
29-
{
30-
return _jsonApiContext.RequestEntity.Attributes
31-
.FirstOrDefault(attr =>
32-
attr.InternalAttributeName.ToLower() == propertyName.ToLower()
29+
private AttrAttribute GetAttribute(string attribute) =>
30+
_jsonApiContext.RequestEntity.Attributes.FirstOrDefault(
31+
attr => string.Equals(attr.PublicAttributeName, attribute, StringComparison.OrdinalIgnoreCase)
3332
);
34-
}
3533
}
36-
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1+
using System;
2+
using JsonApiDotNetCore.Extensions;
3+
14
namespace JsonApiDotNetCore.Internal.Query
25
{
36
public class FilterQuery
47
{
5-
public FilterQuery(string key, string value, string operation)
8+
public FilterQuery(string attribute, string value, string operation)
69
{
7-
Key = key;
10+
Attribute = attribute;
11+
Key = attribute.ToProperCase();
812
Value = value;
913
Operation = operation;
1014
}
1115

16+
[Obsolete("Key has been replaced by '" + nameof(Attribute) + "'. Members should be located by their public name, not by coercing the provided value to the internal name.")]
1217
public string Key { get; set; }
18+
public string Attribute { get; }
1319
public string Value { get; set; }
1420
public string Operation { get; set; }
15-
public bool IsAttributeOfRelationship => Key.Contains(".");
21+
public bool IsAttributeOfRelationship => Attribute.Contains(".");
1622
}
17-
}
23+
}

src/JsonApiDotNetCore/Services/QueryAccessor.cs

+18-5
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,22 @@ public bool TryGetValue<T>(string key, out T value)
6161
}
6262
}
6363

64-
private string GetFilterValue(string key) => _jsonApiContext.QuerySet
65-
.Filters
66-
.FirstOrDefault(f => string.Equals(f.Key, key, StringComparison.OrdinalIgnoreCase))
67-
?.Value;
64+
private string GetFilterValue(string key) {
65+
var publicValue = _jsonApiContext.QuerySet.Filters
66+
.FirstOrDefault(f => string.Equals(f.Attribute, key, StringComparison.OrdinalIgnoreCase))?.Value;
67+
68+
if(publicValue != null)
69+
return publicValue;
70+
71+
var internalValue = _jsonApiContext.QuerySet.Filters
72+
.FirstOrDefault(f => string.Equals(f.Key, key, StringComparison.OrdinalIgnoreCase))?.Value;
73+
74+
if(internalValue != null) {
75+
_logger.LogWarning("Locating filters by the internal propterty name is deprecated. You should use the public attribute name instead.");
76+
return publicValue;
77+
}
78+
79+
return null;
80+
}
6881
}
69-
}
82+
}

src/JsonApiDotNetCore/Services/QueryParser.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class QueryParser : IQueryParser {
1818
private readonly IControllerContext _controllerContext;
1919
private readonly JsonApiOptions _options;
2020

21+
private const char OPEN_BRACKET = '[';
22+
private const char CLOSE_BRACKET = ']';
2123
public QueryParser(
2224
IControllerContext controllerContext,
2325
JsonApiOptions options) {
@@ -72,7 +74,7 @@ protected virtual List<FilterQuery> ParseFilterQuery(string key, string value) {
7274
// expected input = filter[id]=eq:1
7375
var queries = new List<FilterQuery>();
7476

75-
var propertyName = key.Split('[', ']') [1].ToProperCase();
77+
var propertyName = key.Split(OPEN_BRACKET, CLOSE_BRACKET) [1];
7678

7779
var values = value.Split(',');
7880
foreach (var val in values) {

0 commit comments

Comments
 (0)