Skip to content

Commit a2ab5bd

Browse files
committed
feat(query-set): parse fields parameter
1 parent d50fb51 commit a2ab5bd

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/JsonApiDotNetCore/Internal/Query/QuerySet.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ public QuerySet(
1919
_jsonApiContext = jsonApiContext;
2020
PageQuery = new PageQuery();
2121
Filters = new List<FilterQuery>();
22+
Fields = new List<string>();
2223
BuildQuerySet(query);
2324
}
2425

2526
public List<FilterQuery> Filters { get; set; }
2627
public PageQuery PageQuery { get; set; }
2728
public List<SortQuery> SortParameters { get; set; }
2829
public List<string> IncludedRelationships { get; set; }
30+
public List<string> Fields { get; set; }
2931

3032
private void BuildQuerySet(IQueryCollection query)
3133
{
@@ -55,6 +57,12 @@ private void BuildQuerySet(IQueryCollection query)
5557
continue;
5658
}
5759

60+
if (pair.Key.StartsWith("fields"))
61+
{
62+
Fields = ParseFieldsQuery(pair.Key, pair.Value);
63+
continue;
64+
}
65+
5866
throw new JsonApiException("400", $"{pair} is not a valid query.");
5967
}
6068
}
@@ -160,6 +168,29 @@ private List<string> ParseIncludedRelationships(string value)
160168
.ToList();
161169
}
162170

171+
private List<string> ParseFieldsQuery(string key, string value)
172+
{
173+
// expected: fields[TYPE]=prop1,prop2
174+
var typeName = key.Split('[', ']')[1];
175+
176+
var includedFields = new List<string> { "Id" };
177+
178+
if(typeName != _jsonApiContext.RequestEntity.EntityName.Dasherize())
179+
return includedFields;
180+
181+
var fields = value.Split(',');
182+
foreach(var field in fields)
183+
{
184+
var internalAttrName = _jsonApiContext.RequestEntity
185+
.Attributes
186+
.SingleOrDefault(attr => attr.PublicAttributeName == field)
187+
.InternalAttributeName;
188+
includedFields.Add(internalAttrName);
189+
}
190+
191+
return includedFields;
192+
}
193+
163194
private AttrAttribute GetAttribute(string propertyName)
164195
{
165196
return _jsonApiContext.RequestEntity.Attributes

0 commit comments

Comments
 (0)