Skip to content

Commit b88d8e7

Browse files
committed
chore: add more unit tests
1 parent db7157b commit b88d8e7

25 files changed

+486
-436
lines changed

src/Examples/JsonApiDotNetCoreExample/Properties/launchSettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"launchUrl": "http://localhost:5000/api/values",
2323
"environmentVariables": {
2424
"ASPNETCORE_ENVIRONMENT": "Development"
25-
}
25+
},
26+
"applicationUrl": "http://localhost:5000/"
2627
}
2728
}
2829
}

src/Examples/NoEntityFrameworkExample/Properties/launchSettings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
},
1818
"NoEntityFrameworkExample": {
1919
"commandName": "Project",
20-
"environmentVariables": {}
20+
"launchBrowser": true,
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development"
23+
},
24+
"applicationUrl": "http://localhost:5000/"
2125
}
2226
}
2327
}

src/Examples/ReportsExample/Properties/launchSettings.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
}
99
},
1010
"profiles": {
11-
"IIS Express": {
12-
"commandName": "IISExpress",
13-
"launchBrowser": true,
14-
"environmentVariables": {
15-
"ASPNETCORE_ENVIRONMENT": "Development"
16-
}
17-
},
1811
"ReportsExample": {
1912
"commandName": "Project",
2013
"launchBrowser": true,
2114
"environmentVariables": {
2215
"ASPNETCORE_ENVIRONMENT": "Development"
2316
},
2417
"applicationUrl": "http://localhost:55654/"
18+
},
19+
"IIS Express": {
20+
"commandName": "IISExpress",
21+
"launchBrowser": true,
22+
"environmentVariables": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
}
2525
}
2626
}
2727
}

src/JsonApiDotNetCore/QueryParameters/Common/IQueryParameterService.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace JsonApiDotNetCore.Query
1+
using System.Collections.Generic;
2+
using Microsoft.Extensions.Primitives;
3+
4+
namespace JsonApiDotNetCore.Query
25
{
36
/// <summary>
47
/// Base interface that all query parameter services should inherit.
@@ -8,8 +11,8 @@ public interface IParsableQueryParameter
811
/// <summary>
912
/// Parses the value of the query parameter. Invoked in the middleware.
1013
/// </summary>
11-
/// <param name="value">the value of the query parameter as parsed from the url</param>
12-
void Parse(string key, string value);
14+
/// <param name="queryParameter">the value of the query parameter as parsed from the url</param>
15+
void Parse(KeyValuePair<string, StringValues> queryParameter);
1316
/// <summary>
1417
/// The name of the query parameter as matched in the URL.
1518
/// </summary>

src/JsonApiDotNetCore/QueryParameters/Common/QueryParameterService.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Text.RegularExpressions;
34
using JsonApiDotNetCore.Internal;
45
using JsonApiDotNetCore.Internal.Contracts;
56
using JsonApiDotNetCore.Managers.Contracts;
67
using JsonApiDotNetCore.Models;
8+
using Microsoft.Extensions.Primitives;
79

810
namespace JsonApiDotNetCore.Query
911
{
10-
public abstract class QueryParameterService : IParsableQueryParameter
12+
public abstract class QueryParameterService
1113
{
1214
protected readonly IContextEntityProvider _contextEntityProvider;
1315
protected readonly ContextEntity _requestResource;
@@ -30,7 +32,8 @@ protected QueryParameterService() { }
3032
public virtual string Name { get { return GetParameterNameFromType(); } }
3133

3234
/// <inheritdoc/>
33-
public abstract void Parse(string key, string value);
35+
public abstract void Parse(KeyValuePair<string, StringValues> queryParameter);
36+
3437

3538
/// <summary>
3639
/// Gets the query parameter name from the implementing class name. Trims "Service"

src/JsonApiDotNetCore/QueryParameters/FilterService.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using JsonApiDotNetCore.Internal.Query;
77
using JsonApiDotNetCore.Managers.Contracts;
88
using JsonApiDotNetCore.Models;
9+
using Microsoft.Extensions.Primitives;
910

1011
namespace JsonApiDotNetCore.Query
1112
{
@@ -26,9 +27,9 @@ public List<FilterQueryContext> Get()
2627
return _filters;
2728
}
2829

29-
public override void Parse(string key, string value)
30+
public override void Parse(KeyValuePair<string, StringValues> queryParameter)
3031
{
31-
var queries = GetFilterQueries(key, value);
32+
var queries = GetFilterQueries(queryParameter);
3233
_filters.AddRange(queries.Select(GetQueryContexts));
3334
}
3435

@@ -52,23 +53,23 @@ private FilterQueryContext GetQueryContexts(FilterQuery query)
5253
}
5354

5455
/// todo: this could be simplified a bunch
55-
private List<FilterQuery> GetFilterQueries(string key, string value)
56+
private List<FilterQuery> GetFilterQueries(KeyValuePair<string, StringValues> queryParameter)
5657
{
5758
// expected input = filter[id]=1
5859
// expected input = filter[id]=eq:1
59-
var propertyName = key.Split(QueryConstants.OPEN_BRACKET, QueryConstants.CLOSE_BRACKET)[1];
60+
var propertyName = queryParameter.Key.Split(QueryConstants.OPEN_BRACKET, QueryConstants.CLOSE_BRACKET)[1];
6061
var queries = new List<FilterQuery>();
6162
// InArray case
62-
string op = GetFilterOperation(value);
63+
string op = GetFilterOperation(queryParameter.Value);
6364
if (string.Equals(op, FilterOperation.@in.ToString(), StringComparison.OrdinalIgnoreCase)
6465
|| string.Equals(op, FilterOperation.nin.ToString(), StringComparison.OrdinalIgnoreCase))
6566
{
66-
(var _, var filterValue) = ParseFilterOperation(value);
67+
(var _, var filterValue) = ParseFilterOperation(queryParameter.Value);
6768
queries.Add(new FilterQuery(propertyName, filterValue, op));
6869
}
6970
else
7071
{
71-
var values = value.Split(QueryConstants.COMMA);
72+
var values = ((string)queryParameter.Value).Split(QueryConstants.COMMA);
7273
foreach (var val in values)
7374
{
7475
(var operation, var filterValue) = ParseFilterOperation(val);
@@ -105,7 +106,7 @@ private string GetFilterOperation(string value)
105106

106107
var operation = values[0];
107108
// remove prefix from value
108-
if (Enum.TryParse(operation, out FilterOperation op) == false)
109+
if (Enum.TryParse(operation, out FilterOperation op) == false)
109110
return string.Empty;
110111

111112
return operation;

src/JsonApiDotNetCore/QueryParameters/IncludeService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using JsonApiDotNetCore.Internal.Query;
66
using JsonApiDotNetCore.Managers.Contracts;
77
using JsonApiDotNetCore.Models;
8+
using Microsoft.Extensions.Primitives;
89

910
namespace JsonApiDotNetCore.Query
1011
{
@@ -26,8 +27,9 @@ public List<List<RelationshipAttribute>> Get()
2627
}
2728

2829
/// <inheritdoc/>
29-
public override void Parse(string _, string value)
30+
public override void Parse(KeyValuePair<string, StringValues> queryParameter)
3031
{
32+
var value = (string)queryParameter.Value;
3133
if (string.IsNullOrWhiteSpace(value))
3234
throw new JsonApiException(400, "Include parameter must not be empty if provided");
3335

src/JsonApiDotNetCore/QueryParameters/OmitDefaultService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System.Collections.Generic;
12
using JsonApiDotNetCore.Configuration;
3+
using Microsoft.Extensions.Primitives;
24

35
namespace JsonApiDotNetCore.Query
46
{
@@ -14,12 +16,12 @@ public OmitDefaultService(IJsonApiOptions options)
1416

1517
public bool Config { get; private set; }
1618

17-
public override void Parse(string key, string value)
19+
public override void Parse(KeyValuePair<string, StringValues> queryParameter)
1820
{
1921
if (!_options.DefaultAttributeResponseBehavior.AllowClientOverride)
2022
return;
2123

22-
if (!bool.TryParse(value, out var config))
24+
if (!bool.TryParse(queryParameter.Value, out var config))
2325
return;
2426

2527
Config = config;

src/JsonApiDotNetCore/QueryParameters/OmitNullService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using JsonApiDotNetCore.Configuration;
34
using JsonApiDotNetCore.Internal;
5+
using Microsoft.Extensions.Primitives;
46

57
namespace JsonApiDotNetCore.Query
68
{
@@ -16,12 +18,12 @@ public OmitNullService(IJsonApiOptions options)
1618

1719
public bool Config { get; private set; }
1820

19-
public override void Parse(string key, string value)
21+
public override void Parse(KeyValuePair<string, StringValues> queryParameter)
2022
{
2123
if (!_options.NullAttributeResponseBehavior.AllowClientOverride)
2224
return;
2325

24-
if (!bool.TryParse(value, out var config))
26+
if (!bool.TryParse(queryParameter.Value, out var config))
2527
return;
2628

2729
Config = config;

src/JsonApiDotNetCore/QueryParameters/PageService.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
23
using JsonApiDotNetCore.Configuration;
34
using JsonApiDotNetCore.Internal;
45
using JsonApiDotNetCore.Internal.Query;
6+
using Microsoft.Extensions.Primitives;
57

68
namespace JsonApiDotNetCore.Query
79
{
@@ -26,28 +28,28 @@ public PageService(IJsonApiOptions options)
2628
/// <inheritdoc/>
2729
public int TotalPages => (TotalRecords == null) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords.Value, PageSize));
2830

29-
public override void Parse(string key, string value)
31+
public override void Parse(KeyValuePair<string, StringValues> queryParameter)
3032
{
3133
// expected input = page[size]=10
3234
// page[number]=1
33-
var propertyName = key.Split(QueryConstants.OPEN_BRACKET, QueryConstants.CLOSE_BRACKET)[1];
35+
var propertyName = queryParameter.Key.Split(QueryConstants.OPEN_BRACKET, QueryConstants.CLOSE_BRACKET)[1];
3436

3537
const string SIZE = "size";
3638
const string NUMBER = "number";
3739

3840
if (propertyName == SIZE)
3941
{
40-
if (int.TryParse(value, out var size))
42+
if (int.TryParse(queryParameter.Value, out var size))
4143
PageSize = size;
4244
else
43-
throw new JsonApiException(400, $"Invalid page size '{value}'");
45+
throw new JsonApiException(400, $"Invalid page size '{queryParameter.Value}'");
4446
}
4547
else if (propertyName == NUMBER)
4648
{
47-
if (int.TryParse(value, out var size))
49+
if (int.TryParse(queryParameter.Value, out var size))
4850
CurrentPage = size;
4951
else
50-
throw new JsonApiException(400, $"Invalid page number '{value}'");
52+
throw new JsonApiException(400, $"Invalid page number '{queryParameter.Value}'");
5153
}
5254
}
5355

src/JsonApiDotNetCore/QueryParameters/SortService.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using JsonApiDotNetCore.Internal.Query;
66
using JsonApiDotNetCore.Managers.Contracts;
77
using JsonApiDotNetCore.Models;
8+
using Microsoft.Extensions.Primitives;
89

910
namespace JsonApiDotNetCore.Query
1011
{
@@ -30,12 +31,10 @@ private void CheckIfProcessed()
3031
_isProcessed = true;
3132
}
3233

33-
public override void Parse(string key, string value)
34+
public override void Parse(KeyValuePair<string, StringValues> queryParameter)
3435
{
35-
3636
CheckIfProcessed();
37-
38-
var queries = BuildQueries(value);
37+
var queries = BuildQueries(queryParameter.Value);
3938

4039
_queries = queries.Select(BuildQueryContext).ToList();
4140

src/JsonApiDotNetCore/QueryParameters/SparseFieldsService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using JsonApiDotNetCore.Internal.Query;
77
using JsonApiDotNetCore.Managers.Contracts;
88
using JsonApiDotNetCore.Models;
9+
using Microsoft.Extensions.Primitives;
910

1011
namespace JsonApiDotNetCore.Query
1112
{
@@ -40,17 +41,17 @@ public List<AttrAttribute> Get(RelationshipAttribute relationship = null)
4041
}
4142

4243
/// <inheritdoc/>
43-
public override void Parse(string key, string value)
44+
public override void Parse(KeyValuePair<string, StringValues> queryParameter)
4445
{
4546
// expected: fields[TYPE]=prop1,prop2
46-
var typeName = key.Split(QueryConstants.OPEN_BRACKET, QueryConstants.CLOSE_BRACKET)[1];
47+
var typeName = queryParameter.Key.Split(QueryConstants.OPEN_BRACKET, QueryConstants.CLOSE_BRACKET)[1];
4748
var includedFields = new List<string> { nameof(Identifiable.Id) };
4849

4950
var relationship = _requestResource.Relationships.SingleOrDefault(a => a.Is(typeName));
5051
if (relationship == null && string.Equals(typeName, _requestResource.EntityName, StringComparison.OrdinalIgnoreCase) == false)
5152
throw new JsonApiException(400, $"fields[{typeName}] is invalid");
5253

53-
var fields = value.Split(QueryConstants.COMMA);
54+
var fields = ((string)queryParameter.Value).Split(QueryConstants.COMMA);
5455
foreach (var field in fields)
5556
{
5657
if (relationship != default)

src/JsonApiDotNetCore/Services/Contract/IResourceDefinitionProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
namespace JsonApiDotNetCore.Query
55
{
66
/// <summary>
7-
/// Service to retrieve resource definitions. Goal is to encapsulate
8-
/// the service provider that needs to be injected for this purpose.
7+
/// Retrieves a <see cref="ResourceDefinition{TResource}"/> from the DI container.
8+
/// Abstracts away the creation of the corresponding generic type and usage
9+
/// of the service provider to do so.
910
/// </summary>
1011
public interface IResourceDefinitionProvider
1112
{

src/JsonApiDotNetCore/Services/QueryParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public virtual void Parse(IQueryCollection query, DisableQueryAttribute disabled
3131
if (pair.Key.ToLower().StartsWith(service.Name, StringComparison.Ordinal))
3232
{
3333
if (disabledQuery == null || !IsDisabled(disabledQuery, service))
34-
service.Parse(pair.Key, pair.Value);
34+
service.Parse(pair);
3535
parsed = true;
3636
break;
3737
}

src/JsonApiDotNetCore/Services/ResourceDefinitionProvider.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,22 @@
55

66
namespace JsonApiDotNetCore.Query
77
{
8-
/// <summary>
9-
/// Abstracts away the creation of the corresponding generic type and usage
10-
/// of the service provider in order to get a <see cref="ResourceDefinition{TResource}"/>
11-
/// service.
12-
/// </summary>
8+
/// <inheritdoc/>
139
internal class ResourceDefinitionProvider : IResourceDefinitionProvider
1410
{
15-
private readonly IScopedServiceProvider _sp;
16-
private readonly IContextEntityProvider _rcp;
11+
private readonly IContextEntityProvider _resourceContextProvider;
12+
private readonly IScopedServiceProvider _serviceProvider;
1713

1814
public ResourceDefinitionProvider(IContextEntityProvider resourceContextProvider, IScopedServiceProvider serviceProvider)
1915
{
20-
_sp = serviceProvider;
21-
_rcp = resourceContextProvider;
16+
_resourceContextProvider = resourceContextProvider;
17+
_serviceProvider = serviceProvider;
2218
}
2319

2420
/// <inheritdoc/>
2521
public IResourceDefinition Get(Type resourceType)
2622
{
27-
return (IResourceDefinition)_sp.GetService(_rcp.GetContextEntity(resourceType).ResourceType);
23+
return (IResourceDefinition)_serviceProvider.GetService(_resourceContextProvider.GetContextEntity(resourceType).ResourceType);
2824
}
2925
}
3026
}

0 commit comments

Comments
 (0)