Skip to content

Restructured eager loading tests and fixed two bugs #882

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 3 commits into from
Nov 19, 2020
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

This file was deleted.

6 changes: 0 additions & 6 deletions src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public sealed class AppDbContext : DbContext

public DbSet<TodoItem> TodoItems { get; set; }
public DbSet<Passport> Passports { get; set; }
public DbSet<Visa> Visas { get; set; }
public DbSet<Person> People { get; set; }
public DbSet<TodoItemCollection> TodoItemCollections { get; set; }
public DbSet<KebabCasedModel> KebabCasedModels { get; set; }
Expand Down Expand Up @@ -69,11 +68,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasForeignKey<Person>("PassportKey")
.OnDelete(DeleteBehavior.SetNull);

modelBuilder.Entity<Passport>()
.HasMany(passport => passport.GrantedVisas)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<TodoItem>()
.HasOne(p => p.OneToOnePerson)
.WithOne(p => p.OneToOneTodoItem)
Expand Down
9 changes: 0 additions & 9 deletions src/Examples/JsonApiDotNetCoreExample/Models/Passport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,6 @@ public string BirthCountryName
[EagerLoad]
public Country BirthCountry { get; set; }

[Attr(Capabilities = AttrCapabilities.All & ~(AttrCapabilities.AllowCreate | AttrCapabilities.AllowChange))]
[NotMapped]
public string GrantedVisaCountries => GrantedVisas == null || !GrantedVisas.Any()
? null
: string.Join(", ", GrantedVisas.Select(v => v.TargetCountry.Name));

[EagerLoad]
public ICollection<Visa> GrantedVisas { get; set; }

public Passport(AppDbContext appDbContext)
{
_systemClock = appDbContext.SystemClock;
Expand Down
18 changes: 0 additions & 18 deletions src/Examples/JsonApiDotNetCoreExample/Models/Visa.cs

This file was deleted.

43 changes: 25 additions & 18 deletions src/JsonApiDotNetCore/Queries/Internal/QueryLayerComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class QueryLayerComposer : IQueryLayerComposer
public QueryLayerComposer(
IEnumerable<IQueryConstraintProvider> constraintProviders,
IResourceContextProvider resourceContextProvider,
IResourceDefinitionAccessor resourceDefinitionAccessor,
IResourceDefinitionAccessor resourceDefinitionAccessor,
IJsonApiOptions options,
IPaginationContext paginationContext,
ITargetedFields targetedFields)
Expand Down Expand Up @@ -93,7 +93,8 @@ private IncludeExpression ComposeChildren(QueryLayer topLayer, ICollection<Expre
{
var include = constraints
.Where(constraint => constraint.Scope == null)
.Select(constraint => constraint.Expression).OfType<IncludeExpression>()
.Select(constraint => constraint.Expression)
.OfType<IncludeExpression>()
.FirstOrDefault() ?? IncludeExpression.Empty;

var includeElements =
Expand All @@ -104,7 +105,7 @@ private IncludeExpression ComposeChildren(QueryLayer topLayer, ICollection<Expre
: include;
}

private IReadOnlyCollection<IncludeElementExpression> ProcessIncludeSet(IReadOnlyCollection<IncludeElementExpression> includeElements,
private IReadOnlyCollection<IncludeElementExpression> ProcessIncludeSet(IReadOnlyCollection<IncludeElementExpression> includeElements,
QueryLayer parentLayer, ICollection<RelationshipAttribute> parentRelationshipChain, ICollection<ExpressionInScope> constraints)
{
includeElements = GetIncludeElements(includeElements, parentLayer.ResourceContext) ?? Array.Empty<IncludeElementExpression>();
Expand Down Expand Up @@ -135,7 +136,7 @@ private IReadOnlyCollection<IncludeElementExpression> ProcessIncludeSet(IReadOnl
{
Filter = GetFilter(expressionsInCurrentScope, resourceContext),
Sort = GetSort(expressionsInCurrentScope, resourceContext),
Pagination = ((JsonApiOptions) _options).DisableChildrenPagination
Pagination = ((JsonApiOptions)_options).DisableChildrenPagination
? null
: GetPagination(expressionsInCurrentScope, resourceContext),
Projection = GetSparseFieldSetProjection(expressionsInCurrentScope, resourceContext)
Expand Down Expand Up @@ -186,7 +187,10 @@ public QueryLayer ComposeForGetById<TId>(TId id, ResourceContext resourceContext

if (fieldSelection == TopFieldSelection.OnlyIdAttribute)
{
queryLayer.Projection = new Dictionary<ResourceFieldAttribute, QueryLayer> {{idAttribute, null}};
queryLayer.Projection = new Dictionary<ResourceFieldAttribute, QueryLayer>
{
[idAttribute] = null
};
}
else if (fieldSelection == TopFieldSelection.WithAllAttributes && queryLayer.Projection != null)
{
Expand Down Expand Up @@ -234,9 +238,9 @@ public QueryLayer WrapLayerForSecondaryEndpoint<TId>(QueryLayer secondaryLayer,
secondaryLayer.Include = null;

var primaryIdAttribute = GetIdAttribute(primaryResourceContext);
var sparseFieldSet = new SparseFieldSetExpression(new[] { primaryIdAttribute });
var sparseFieldSet = new SparseFieldSetExpression(new[] {primaryIdAttribute});

var primaryProjection = GetSparseFieldSetProjection(new[] { sparseFieldSet }, primaryResourceContext) ?? new Dictionary<ResourceFieldAttribute, QueryLayer>();
var primaryProjection = GetSparseFieldSetProjection(new[] {sparseFieldSet}, primaryResourceContext) ?? new Dictionary<ResourceFieldAttribute, QueryLayer>();
primaryProjection[secondaryRelationship] = secondaryLayer;
primaryProjection[primaryIdAttribute] = null;

Expand Down Expand Up @@ -276,10 +280,10 @@ private FilterExpression CreateFilterByIds<TId>(ICollection<TId> ids, AttrAttrib
filter = new EqualsAnyOfExpression(idChain, constants);
}

return filter == null
? existingFilter
: existingFilter == null
? filter
return filter == null
? existingFilter
: existingFilter == null
? filter
: new LogicalExpression(LogicalOperator.And, new[] {filter, existingFilter});
}

Expand All @@ -294,7 +298,7 @@ public QueryLayer ComposeForUpdate<TId>(TId id, ResourceContext primaryResource)
var primaryIdAttribute = GetIdAttribute(primaryResource);

var primaryLayer = ComposeTopLayer(Array.Empty<ExpressionInScope>(), primaryResource);
primaryLayer.Include = includeElements.Any() ? new IncludeExpression(includeElements) : null;
primaryLayer.Include = includeElements.Any() ? new IncludeExpression(includeElements) : IncludeExpression.Empty;
primaryLayer.Sort = null;
primaryLayer.Pagination = null;
primaryLayer.Filter = CreateFilterByIds(new[] {id}, primaryIdAttribute, primaryLayer.Filter);
Expand Down Expand Up @@ -332,6 +336,7 @@ public QueryLayer ComposeForGetRelationshipRightIds(RelationshipAttribute relati

return new QueryLayer(rightResourceContext)
{
Include = IncludeExpression.Empty,
Filter = filter,
Projection = new Dictionary<ResourceFieldAttribute, QueryLayer>
{
Expand Down Expand Up @@ -386,10 +391,12 @@ protected virtual FilterExpression GetFilter(IReadOnlyCollection<QueryExpression
if (resourceContext == null) throw new ArgumentNullException(nameof(resourceContext));

var filters = expressionsInScope.OfType<FilterExpression>().ToArray();
var filter = filters.Length > 1 ? new LogicalExpression(LogicalOperator.And, filters) : filters.FirstOrDefault();

filter = _resourceDefinitionAccessor.OnApplyFilter(resourceContext.ResourceType, filter);
return filter;
var filter = filters.Length > 1
? new LogicalExpression(LogicalOperator.And, filters)
: filters.FirstOrDefault();

return _resourceDefinitionAccessor.OnApplyFilter(resourceContext.ResourceType, filter);
}

protected virtual SortExpression GetSort(IReadOnlyCollection<QueryExpression> expressionsInScope, ResourceContext resourceContext)
Expand All @@ -398,7 +405,7 @@ protected virtual SortExpression GetSort(IReadOnlyCollection<QueryExpression> ex
if (resourceContext == null) throw new ArgumentNullException(nameof(resourceContext));

var sort = expressionsInScope.OfType<SortExpression>().FirstOrDefault();

sort = _resourceDefinitionAccessor.OnApplySort(resourceContext.ResourceType, sort);

if (sort == null)
Expand All @@ -416,7 +423,7 @@ protected virtual PaginationExpression GetPagination(IReadOnlyCollection<QueryEx
if (resourceContext == null) throw new ArgumentNullException(nameof(resourceContext));

var pagination = expressionsInScope.OfType<PaginationExpression>().FirstOrDefault();

pagination = _resourceDefinitionAccessor.OnApplyPagination(resourceContext.ResourceType, pagination);

pagination ??= new PaginationExpression(PageNumber.ValueOne, _options.DefaultPageSize);
Expand Down Expand Up @@ -444,7 +451,7 @@ protected virtual IDictionary<ResourceFieldAttribute, QueryLayer> GetSparseField
var idAttribute = GetIdAttribute(resourceContext);
attributes.Add(idAttribute);

return attributes.Cast<ResourceFieldAttribute>().ToDictionary(key => key, value => (QueryLayer) null);
return attributes.Cast<ResourceFieldAttribute>().ToDictionary(key => key, value => (QueryLayer)null);
}

private static AttrAttribute GetIdAttribute(ResourceContext resourceContext)
Expand Down
Loading