Skip to content
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

Fix exception thrown by EF Core 8 preview #1289

Merged
merged 2 commits into from
Jul 23, 2023
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
4 changes: 2 additions & 2 deletions src/JsonApiDotNetCore.SourceGenerators/SourceCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private void WriteNullableEnable()

private void WriteNamespaceImports(INamedTypeSymbol loggerFactoryInterface, INamedTypeSymbol resourceType, string? controllerNamespace)
{
_sourceBuilder.AppendLine($@"using {loggerFactoryInterface.ContainingNamespace};");
_sourceBuilder.AppendLine($"using {loggerFactoryInterface.ContainingNamespace};");

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

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

WriteOpenCurly();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,18 @@ protected async Task UpdateRelationshipAsync(RelationshipAttribute relationship,
private bool RequireLoadOfInverseRelationship(RelationshipAttribute relationship, [NotNullWhen(true)] object? trackedValueToAssign)
{
// See https://github.com/json-api-dotnet/JsonApiDotNetCore/issues/502.
return trackedValueToAssign != null && relationship is HasOneAttribute { IsOneToOne: true };
if (trackedValueToAssign != null && relationship is HasOneAttribute { IsOneToOne: true })
{
IEntityType? leftEntityType = _dbContext.Model.FindEntityType(relationship.LeftType.ClrType);
INavigation? navigation = leftEntityType?.FindNavigation(relationship.Property.Name);

if (navigation != null && navigation.ForeignKey.DeclaringEntityType.ClrType == relationship.LeftType.ClrType)
{
return true;
}
}

return false;
}

protected virtual async Task SaveChangesAsync(CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace JsonApiDotNetCoreTests.IntegrationTests.AtomicOperations;
[Resource(ControllerNamespace = "JsonApiDotNetCoreTests.IntegrationTests.AtomicOperations")]
public sealed class MusicTrack : Identifiable<Guid>
{
[RegularExpression(@"(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$")]
[RegularExpression("(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$")]
public override Guid Id { get; set; }

[Attr]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ public async Task Get_skips_middleware_and_formatters()
public async Task Post_skips_middleware_and_formatters()
{
// Arrange
using var request = new HttpRequestMessage(HttpMethod.Post, "/NonJsonApi")
using var request = new HttpRequestMessage(HttpMethod.Post, "/NonJsonApi");

request.Content = new StringContent("Jack")
{
Content = new StringContent("Jack")
Headers =
{
Headers =
{
ContentType = new MediaTypeHeaderValue("text/plain")
}
ContentType = new MediaTypeHeaderValue("text/plain")
}
};

Expand Down Expand Up @@ -90,14 +89,13 @@ public async Task Post_skips_error_handler()
public async Task Put_skips_middleware_and_formatters()
{
// Arrange
using var request = new HttpRequestMessage(HttpMethod.Put, "/NonJsonApi")
using var request = new HttpRequestMessage(HttpMethod.Put, "/NonJsonApi");

request.Content = new StringContent("\"Jane\"")
{
Content = new StringContent("\"Jane\"")
Headers =
{
Headers =
{
ContentType = new MediaTypeHeaderValue("application/json")
}
ContentType = new MediaTypeHeaderValue("application/json")
}
};

Expand Down
2 changes: 1 addition & 1 deletion test/SourceGeneratorTests/ControllerGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ public sealed class Item : Identifiable<string?>
GeneratorDriverRunResult runResult = driver.GetRunResult();
runResult.Should().NotHaveDiagnostics();

runResult.Should().HaveProducedSourceCodeContaining(@"#nullable enable");
runResult.Should().HaveProducedSourceCodeContaining("#nullable enable");
}

[Fact]
Expand Down