Skip to content

Repro for issue 988 #990

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

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<PropertyGroup>
<NetCoreAppVersion>netcoreapp3.1</NetCoreAppVersion>
<AspNetCoreVersion>3.1.*</AspNetCoreVersion>
<EFCoreVersion>3.1.*</EFCoreVersion>
<NpgsqlPostgreSQLVersion>3.1.*</NpgsqlPostgreSQLVersion>
<EFCoreVersion>5.0.*</EFCoreVersion>
<NpgsqlPostgreSQLVersion>5.0.*</NpgsqlPostgreSQLVersion>
<CodeAnalysisRuleSet>$(SolutionDir)CodingGuidelines.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ private ICollection<PropertySelector> ToPropertySelectors(IDictionary<ResourceFi
foreach (EagerLoadAttribute eagerLoad in resourceContext.EagerLoads)
{
var propertySelector = new PropertySelector(eagerLoad.Property);
propertySelectors[propertySelector.Property] = propertySelector;

// When an entity navigation property is decorated with both EagerLoadAttribute and RelationshipAttribute,
// it may already exist with a sub-layer. So do not overwrite in that case.
if (!propertySelectors.ContainsKey(propertySelector.Property))
{
propertySelectors[propertySelector.Property] = propertySelector;
}
}

return propertySelectors.Values;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using JetBrains.Annotations;
using JsonApiDotNetCore.Resources.Annotations;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Issue988
{
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public sealed class Engagement : EntityBase<Guid>
{
// Data

[Attr]
public string Name { get; set; }

// Navigation Properties

[HasMany]
[EagerLoad]
public ICollection<EngagementParty> Parties { get; set; } //= new List<EngagementParty>();

[HasMany]
[NotMapped]
public ICollection<EngagementParty> FirstParties =>
Parties.Where(party => party.Role == ModelConstants.FirstPartyRoleName).OrderBy(party => party.ShortName).ToList();

[HasMany]
[NotMapped]
public ICollection<EngagementParty> SecondParties =>
Parties.Where(party => party.Role == ModelConstants.SecondPartyRoleName).OrderBy(party => party.ShortName).ToList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Issue988
{
public sealed class EngagementPartiesController : JsonApiController<EngagementParty, Guid>
{
public EngagementPartiesController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<EngagementParty, Guid> resourceService)
: base(options, loggerFactory, resourceService)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using JetBrains.Annotations;
using JsonApiDotNetCore.Resources.Annotations;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Issue988
{
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public sealed class EngagementParty : EntityBase<Guid>
{
// Data (simplified)

[Attr]
public string Role { get; set; }

[Attr]
public string ShortName { get; set; }

// Foreign Keys (simplified)

[HasOne]
public Engagement Engagement { get; set; }

//public Guid EngagementId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.ComponentModel;
using JetBrains.Annotations;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Queries.Expressions;
using JsonApiDotNetCore.Resources;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Issue988
{
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
public sealed class EngagementPartyResourceDefinition : JsonApiResourceDefinition<EngagementParty, Guid>
{
/// <inheritdoc />
public EngagementPartyResourceDefinition(IResourceGraph resourceGraph)
: base(resourceGraph)
{
}

/// <inheritdoc />
public override SortExpression OnApplySort(SortExpression existingSort)
{
if (existingSort != null)
{
return existingSort;
}

return CreateSortExpressionFromLambda(new PropertySortOrder
{
(ep => ep.Role, ListSortDirection.Ascending),
(ep => ep.ShortName, ListSortDirection.Ascending)
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers;
using JsonApiDotNetCore.Services;
using Microsoft.Extensions.Logging;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Issue988
{
public sealed class EngagementsController : JsonApiController<Engagement, Guid>
{
public EngagementsController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Engagement, Guid> resourceService)
: base(options, loggerFactory, resourceService)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.ComponentModel.DataAnnotations;
using JetBrains.Annotations;
using JsonApiDotNetCore.Resources;

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Issue988
{
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public abstract class EntityBase<TId> : Identifiable<TId>
{
public DateTimeOffset? DateCreated { get; set; }

public DateTimeOffset? DateModified { get; set; }

[Timestamp]
public byte[] RowVersion { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;

// @formatter:wrap_chained_method_calls chop_always

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Issue988
{
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public sealed class IssueDbContext : DbContext
{
public DbSet<Engagement> Engagements { get; set; }
public DbSet<EngagementParty> EngagementParties { get; set; }

public IssueDbContext(DbContextOptions<IssueDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<EngagementParty>()
.HasOne(engagementParty => engagementParty.Engagement)
.WithMany(engagement => engagement.Parties)
.IsRequired()
.OnDelete(DeleteBehavior.Restrict);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using Bogus;
using TestBuildingBlocks;

// @formatter:wrap_chained_method_calls chop_always
// @formatter:keep_existing_linebreaks true

namespace JsonApiDotNetCoreExampleTests.IntegrationTests.Issue988
{
internal sealed class IssueFakers : FakerContainer
{
private readonly Lazy<Faker<Engagement>> _lazyEngagementFaker = new Lazy<Faker<Engagement>>(() =>
new Faker<Engagement>()
.UseSeed(GetFakerSeed())
.RuleFor(engagement => engagement.Name, faker => faker.Lorem.Word()));

private readonly Lazy<Faker<EngagementParty>> _lazyEngagementPartyFaker = new Lazy<Faker<EngagementParty>>(() =>
new Faker<EngagementParty>()
.UseSeed(GetFakerSeed())
.RuleFor(party => party.Role, faker => faker.Lorem.Word())
.RuleFor(party => party.ShortName, faker => faker.Lorem.Word()));

public Faker<Engagement> Engagement => _lazyEngagementFaker.Value;
public Faker<EngagementParty> EngagementParty => _lazyEngagementPartyFaker.Value;
}
}
Loading