Skip to content

Commit fe64052

Browse files
authored
Merge pull request #1289 from json-api-dotnet/fix-efcore8
Fix exception thrown by EF Core 8 preview
2 parents 28ca7d5 + 336b725 commit fe64052

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

src/JsonApiDotNetCore.SourceGenerators/SourceCodeWriter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private void WriteNullableEnable()
9898

9999
private void WriteNamespaceImports(INamedTypeSymbol loggerFactoryInterface, INamedTypeSymbol resourceType, string? controllerNamespace)
100100
{
101-
_sourceBuilder.AppendLine($@"using {loggerFactoryInterface.ContainingNamespace};");
101+
_sourceBuilder.AppendLine($"using {loggerFactoryInterface.ContainingNamespace};");
102102

103103
_sourceBuilder.AppendLine("using JsonApiDotNetCore.Configuration;");
104104
_sourceBuilder.AppendLine("using JsonApiDotNetCore.Controllers;");
@@ -123,7 +123,7 @@ private void WriteOpenClassDeclaration(string controllerName, JsonApiEndpointsCo
123123
string baseClassName = GetControllerBaseClassName(endpointsToGenerate);
124124

125125
WriteIndent();
126-
_sourceBuilder.AppendLine($@"public sealed partial class {controllerName} : {baseClassName}<{resourceType.Name}, {idType}>");
126+
_sourceBuilder.AppendLine($"public sealed partial class {controllerName} : {baseClassName}<{resourceType.Name}, {idType}>");
127127

128128
WriteOpenCurly();
129129
}

src/JsonApiDotNetCore/Repositories/EntityFrameworkCoreRepository.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,18 @@ protected async Task UpdateRelationshipAsync(RelationshipAttribute relationship,
619619
private bool RequireLoadOfInverseRelationship(RelationshipAttribute relationship, [NotNullWhen(true)] object? trackedValueToAssign)
620620
{
621621
// See https://github.com/json-api-dotnet/JsonApiDotNetCore/issues/502.
622-
return trackedValueToAssign != null && relationship is HasOneAttribute { IsOneToOne: true };
622+
if (trackedValueToAssign != null && relationship is HasOneAttribute { IsOneToOne: true })
623+
{
624+
IEntityType? leftEntityType = _dbContext.Model.FindEntityType(relationship.LeftType.ClrType);
625+
INavigation? navigation = leftEntityType?.FindNavigation(relationship.Property.Name);
626+
627+
if (navigation != null && navigation.ForeignKey.DeclaringEntityType.ClrType == relationship.LeftType.ClrType)
628+
{
629+
return true;
630+
}
631+
}
632+
633+
return false;
623634
}
624635

625636
protected virtual async Task SaveChangesAsync(CancellationToken cancellationToken)

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/MusicTrack.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace JsonApiDotNetCoreTests.IntegrationTests.AtomicOperations;
99
[Resource(ControllerNamespace = "JsonApiDotNetCoreTests.IntegrationTests.AtomicOperations")]
1010
public sealed class MusicTrack : Identifiable<Guid>
1111
{
12-
[RegularExpression(@"(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$")]
12+
[RegularExpression("(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$")]
1313
public override Guid Id { get; set; }
1414

1515
[Attr]

test/JsonApiDotNetCoreTests/IntegrationTests/NonJsonApiControllers/NonJsonApiControllerTests.cs

+10-12
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ public async Task Get_skips_middleware_and_formatters()
4141
public async Task Post_skips_middleware_and_formatters()
4242
{
4343
// Arrange
44-
using var request = new HttpRequestMessage(HttpMethod.Post, "/NonJsonApi")
44+
using var request = new HttpRequestMessage(HttpMethod.Post, "/NonJsonApi");
45+
46+
request.Content = new StringContent("Jack")
4547
{
46-
Content = new StringContent("Jack")
48+
Headers =
4749
{
48-
Headers =
49-
{
50-
ContentType = new MediaTypeHeaderValue("text/plain")
51-
}
50+
ContentType = new MediaTypeHeaderValue("text/plain")
5251
}
5352
};
5453

@@ -90,14 +89,13 @@ public async Task Post_skips_error_handler()
9089
public async Task Put_skips_middleware_and_formatters()
9190
{
9291
// Arrange
93-
using var request = new HttpRequestMessage(HttpMethod.Put, "/NonJsonApi")
92+
using var request = new HttpRequestMessage(HttpMethod.Put, "/NonJsonApi");
93+
94+
request.Content = new StringContent("\"Jane\"")
9495
{
95-
Content = new StringContent("\"Jane\"")
96+
Headers =
9697
{
97-
Headers =
98-
{
99-
ContentType = new MediaTypeHeaderValue("application/json")
100-
}
98+
ContentType = new MediaTypeHeaderValue("application/json")
10199
}
102100
};
103101

test/SourceGeneratorTests/ControllerGenerationTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ public sealed class Item : Identifiable<string?>
538538
GeneratorDriverRunResult runResult = driver.GetRunResult();
539539
runResult.Should().NotHaveDiagnostics();
540540

541-
runResult.Should().HaveProducedSourceCodeContaining(@"#nullable enable");
541+
runResult.Should().HaveProducedSourceCodeContaining("#nullable enable");
542542
}
543543

544544
[Fact]

0 commit comments

Comments
 (0)