Skip to content

fix: sparse fields set not applied to query #498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions JsonApiDotnetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ Global
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|x86.ActiveCfg = Release|Any CPU
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|x86.Build.0 = Release|Any CPU
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 2 additions & 2 deletions src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ public virtual IQueryable<TEntity> Sort(IQueryable<TEntity> entities, List<SortQ
/// <inheritdoc />
public virtual async Task<TEntity> GetAsync(TId id)
{
return await GetQueryable().SingleOrDefaultAsync(e => e.Id.Equals(id));
return await Select(GetQueryable(), _jsonApiContext.QuerySet?.Fields).SingleOrDefaultAsync(e => e.Id.Equals(id));
}

/// <inheritdoc />
public virtual async Task<TEntity> GetAndIncludeAsync(TId id, string relationshipName)
{
_logger?.LogDebug($"[JADN] GetAndIncludeAsync({id}, {relationshipName})");

var includedSet = Include(GetQueryable(), relationshipName);
var includedSet = Include(Select(GetQueryable(), _jsonApiContext.QuerySet?.Fields), relationshipName);
var result = await includedSet.SingleOrDefaultAsync(e => e.Id.Equals(id));

return result;
Expand Down
5 changes: 2 additions & 3 deletions src/JsonApiDotNetCore/Services/EntityResourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public virtual async Task<IEnumerable<TResource>> GetAsync()
if (_jsonApiContext.Options.IncludeTotalRecordCount)
_jsonApiContext.PageManager.TotalRecords = await _entities.CountAsync(entities);

if (_jsonApiContext.QuerySet?.Fields?.Count > 0)
entities = _entities.Select(entities, _jsonApiContext.QuerySet.Fields);
entities = _entities.Select(entities, _jsonApiContext.QuerySet?.Fields);

// pagination should be done last since it will execute the query
var pagedEntities = await ApplyPageQueryAsync(entities);
Expand Down Expand Up @@ -243,7 +242,7 @@ protected virtual IQueryable<TEntity> IncludeRelationships(IQueryable<TEntity> e

private async Task<TResource> GetWithRelationshipsAsync(TId id)
{
var query = _entities.GetQueryable().Where(e => e.Id.Equals(id));
var query = _entities.Select(_entities.GetQueryable(), _jsonApiContext.QuerySet?.Fields).Where(e => e.Id.Equals(id));

_jsonApiContext.QuerySet.IncludedRelationships.ForEach(r =>
{
Expand Down