Skip to content

Commit 9083b62

Browse files
committed
Feed includes from query string to the serializer when custom resource service is used
1 parent 2c7fe52 commit 9083b62

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

benchmarks/Serialization/OperationsSerializationBenchmarks.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using BenchmarkDotNet.Attributes;
33
using JsonApiDotNetCore.Configuration;
44
using JsonApiDotNetCore.Middleware;
5+
using JsonApiDotNetCore.Queries;
56
using JsonApiDotNetCore.Queries.Internal;
67
using JsonApiDotNetCore.Resources;
78
using JsonApiDotNetCore.Serialization.Objects;
@@ -130,6 +131,6 @@ protected override JsonApiRequest CreateJsonApiRequest(IResourceGraph resourceGr
130131

131132
protected override IEvaluatedIncludeCache CreateEvaluatedIncludeCache(IResourceGraph resourceGraph)
132133
{
133-
return new EvaluatedIncludeCache();
134+
return new EvaluatedIncludeCache(Array.Empty<IQueryConstraintProvider>());
134135
}
135136
}

benchmarks/Serialization/ResourceSerializationBenchmarks.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using JsonApiDotNetCore;
55
using JsonApiDotNetCore.Configuration;
66
using JsonApiDotNetCore.Middleware;
7+
using JsonApiDotNetCore.Queries;
78
using JsonApiDotNetCore.Queries.Expressions;
89
using JsonApiDotNetCore.Queries.Internal;
910
using JsonApiDotNetCore.Resources.Annotations;
@@ -142,7 +143,7 @@ protected override IEvaluatedIncludeCache CreateEvaluatedIncludeCache(IResourceG
142143
}.ToImmutableHashSet())
143144
}.ToImmutableHashSet());
144145

145-
var cache = new EvaluatedIncludeCache();
146+
var cache = new EvaluatedIncludeCache(Array.Empty<IQueryConstraintProvider>());
146147
cache.Set(include);
147148
return cache;
148149
}

src/JsonApiDotNetCore/Queries/Internal/EvaluatedIncludeCache.cs

+29
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,48 @@ namespace JsonApiDotNetCore.Queries.Internal;
55
/// <inheritdoc />
66
internal sealed class EvaluatedIncludeCache : IEvaluatedIncludeCache
77
{
8+
private readonly IEnumerable<IQueryConstraintProvider> _constraintProviders;
89
private IncludeExpression? _include;
10+
private bool _isAssigned;
11+
12+
public EvaluatedIncludeCache(IEnumerable<IQueryConstraintProvider> constraintProviders)
13+
{
14+
ArgumentGuard.NotNull(constraintProviders);
15+
16+
_constraintProviders = constraintProviders;
17+
}
918

1019
/// <inheritdoc />
1120
public void Set(IncludeExpression include)
1221
{
1322
ArgumentGuard.NotNull(include);
1423

1524
_include = include;
25+
_isAssigned = true;
1626
}
1727

1828
/// <inheritdoc />
1929
public IncludeExpression? Get()
2030
{
31+
if (!_isAssigned)
32+
{
33+
// In case someone has replaced the built-in JsonApiResourceService with their own that "forgets" to populate the cache,
34+
// then as a fallback, we feed the requested includes from query string to the response serializer.
35+
36+
// @formatter:wrap_chained_method_calls chop_always
37+
// @formatter:keep_existing_linebreaks true
38+
39+
_include = _constraintProviders.SelectMany(provider => provider.GetConstraints())
40+
.Where(constraint => constraint.Scope == null)
41+
.Select(constraint => constraint.Expression)
42+
.OfType<IncludeExpression>()
43+
.FirstOrDefault();
44+
45+
// @formatter:keep_existing_linebreaks restore
46+
// @formatter:wrap_chained_method_calls restore
47+
_isAssigned = true;
48+
}
49+
2150
return _include;
2251
}
2352
}

test/JsonApiDotNetCoreTests/UnitTests/Serialization/Response/IncompleteResourceGraphTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void Fails_when_derived_type_is_missing_in_resource_graph()
3434
var resourceDefinitionAccessor = new FakeResourceDefinitionAccessor();
3535
var sparseFieldSetCache = new SparseFieldSetCache(Array.Empty<IQueryConstraintProvider>(), resourceDefinitionAccessor);
3636
var requestQueryStringAccessor = new FakeRequestQueryStringAccessor();
37-
var evaluatedIncludeCache = new EvaluatedIncludeCache();
37+
var evaluatedIncludeCache = new EvaluatedIncludeCache(Array.Empty<IQueryConstraintProvider>());
3838

3939
var responseModelAdapter = new ResponseModelAdapter(request, options, linkBuilder, metaBuilder, resourceDefinitionAccessor, evaluatedIncludeCache,
4040
sparseFieldSetCache, requestQueryStringAccessor);

test/JsonApiDotNetCoreTests/UnitTests/Serialization/Response/ResponseModelAdapterTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ private ResponseModelAdapter CreateAdapter(IJsonApiOptions options, string? prim
569569
PrimaryId = primaryId
570570
};
571571

572-
var evaluatedIncludeCache = new EvaluatedIncludeCache();
572+
var evaluatedIncludeCache = new EvaluatedIncludeCache(Array.Empty<IQueryConstraintProvider>());
573573
var linkBuilder = new FakeLinkBuilder();
574574
var metaBuilder = new FakeMetaBuilder();
575575
var resourceDefinitionAccessor = new FakeResourceDefinitionAccessor();

0 commit comments

Comments
 (0)