Skip to content

Commit 54942de

Browse files
committed
feat: draft of reflectively retrieving all query parameters
1 parent 82766b5 commit 54942de

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/JsonApiDotNetCore/Query/Common/IQueryParameterService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
/// <summary>
44
/// Base interface that all query parameter services should inherit.
55
/// </summary>
6-
public interface IQueryParameterService
6+
internal interface IQueryParameterService
77
{
88
/// <summary>
99
/// Parses the value of the query parameter. Invoked in the middleware.
1010
/// </summary>
1111
/// <param name="value">the value of the query parameter as parsed from the url</param>
1212
void Parse(string value);
1313
/// <summary>
14-
/// Name of the parameter as appearing in the url, used internally for matching.
15-
/// Case sensitive.
14+
/// The name of the query parameter as matched in the URL.
1615
/// </summary>
1716
string Name { get; }
1817
}

src/JsonApiDotNetCore/Query/IncludeService.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using System.Text.RegularExpressions;
34
using JsonApiDotNetCore.Internal;
45
using JsonApiDotNetCore.Internal.Contracts;
56
using JsonApiDotNetCore.Internal.Query;
67
using JsonApiDotNetCore.Managers.Contracts;
78
using JsonApiDotNetCore.Models;
8-
using JsonApiDotNetCore.Query;
99

1010
namespace JsonApiDotNetCore.Query
1111
{
12-
public class IncludeService : IIncludeService, IQueryParameterService
12+
13+
public abstract class QueryParameterService : IQueryParameterService
14+
{
15+
16+
/// <summary>
17+
/// By default, the name is derived from the implementing type.
18+
/// </summary>
19+
/// <example>
20+
/// The following query param service will match the query displayed in URL
21+
/// `?include=some-relationship`
22+
/// <code>public class IncludeService : QueryParameterService { /* ... */ } </code>
23+
/// </example>
24+
public virtual string Name { get { return GetParameterNameFromType(); } }
25+
26+
/// <inheritdoc/>
27+
public abstract void Parse(string value);
28+
29+
/// <summary>
30+
/// Gets the query parameter name from the implementing class name. Trims "Service"
31+
/// from the name if present.
32+
/// </summary>
33+
private string GetParameterNameFromType() => new Regex("Service$").Replace(GetType().Name, string.Empty);
34+
}
35+
36+
public class IncludeService : QueryParameterService, IIncludeService
1337
{
1438
/// todo: make readonly
1539
private readonly List<List<RelationshipAttribute>> _includedChains;
@@ -29,16 +53,14 @@ public IncludeService(ICurrentRequest currentRequest,
2953
/// </summary>
3054
internal IncludeService() : this(null, null) { }
3155

32-
public string Name => QueryConstants.INCLUDE;
33-
3456
/// <inheritdoc/>
3557
public List<List<RelationshipAttribute>> Get()
3658
{
3759
return _includedChains.Select(chain => chain.ToList()).ToList();
3860
}
3961

4062
/// <inheritdoc/>
41-
public void Parse(string value)
63+
public override void Parse(string value)
4264
{
4365
if (string.IsNullOrWhiteSpace(value))
4466
throw new JsonApiException(400, "Include parameter must not be empty if provided");

src/JsonApiDotNetCore/Services/QueryParser.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using JsonApiDotNetCore.Configuration;
55
using JsonApiDotNetCore.Controllers;
6+
using JsonApiDotNetCore.Extensions;
67
using JsonApiDotNetCore.Internal;
78
using JsonApiDotNetCore.Internal.Contracts;
89
using JsonApiDotNetCore.Internal.Query;
@@ -27,6 +28,7 @@ public class QueryParser : IQueryParser
2728
private readonly ICurrentRequest _currentRequest;
2829
private readonly IContextEntityProvider _provider;
2930
private readonly IJsonApiOptions _options;
31+
private readonly IServiceProvider _sp;
3032
private ContextEntity _primaryResource;
3133

3234
public QueryParser(IIncludeService includeService,
@@ -46,6 +48,13 @@ public QueryParser(IIncludeService includeService,
4648

4749
public virtual QuerySet Parse(IQueryCollection query)
4850
{
51+
var type = typeof(IQueryParameterService);
52+
var types = AppDomain.CurrentDomain.GetAssemblies()
53+
.SelectMany(a => a.GetTypes())
54+
.Where(t => t.IsInterface && t.Inherits(type))
55+
.Select(t => (IQueryParameterService)_sp.GetService(t));
56+
57+
4958
_primaryResource = _currentRequest.GetRequestResource();
5059
var querySet = new QuerySet();
5160
var disabledQueries = _currentRequest.DisabledQueryParams;

0 commit comments

Comments
 (0)