-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
fix(ContextGraphBuilder): don't throw if DbContext contains non json:api resource
- Loading branch information
Showing
10 changed files
with
181 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/Examples/JsonApiDotNetCoreExample/Models/NonJsonApiResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace JsonApiDotNetCoreExample.Models | ||
{ | ||
public class NonJsonApiResource | ||
{ | ||
public int Id { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
38 changes: 32 additions & 6 deletions
38
src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,55 @@ | ||
using JsonApiDotNetCore.Builders; | ||
using JsonApiDotNetCore.Configuration; | ||
using JsonApiDotNetCore.Internal; | ||
using JsonApiDotNetCore.Middleware; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace JsonApiDotNetCore.Extensions | ||
{ | ||
// ReSharper disable once InconsistentNaming | ||
public static class IApplicationBuilderExtensions | ||
{ | ||
public static IApplicationBuilder UseJsonApi(this IApplicationBuilder app, bool useMvc = true) | ||
{ | ||
DisableDetailedErrorsIfProduction(app); | ||
LogContextGraphValidations(app); | ||
|
||
app.UseMiddleware<RequestMiddleware>(); | ||
|
||
if (useMvc) | ||
app.UseMvc(); | ||
|
||
return app; | ||
} | ||
|
||
private static void DisableDetailedErrorsIfProduction(IApplicationBuilder app) | ||
{ | ||
var environment = (IHostingEnvironment)app.ApplicationServices.GetService(typeof(IHostingEnvironment)); | ||
|
||
if(environment.IsProduction()) | ||
if (environment.IsProduction()) | ||
{ | ||
JsonApiOptions.DisableErrorStackTraces = true; | ||
JsonApiOptions.DisableErrorSource = true; | ||
} | ||
} | ||
|
||
app.UseMiddleware<RequestMiddleware>(); | ||
|
||
if (useMvc) | ||
app.UseMvc(); | ||
private static void LogContextGraphValidations(IApplicationBuilder app) | ||
{ | ||
var logger = app.ApplicationServices.GetService(typeof(ILogger<ContextGraphBuilder>)) as ILogger; | ||
var contextGraph = app.ApplicationServices.GetService(typeof(IContextGraph)) as ContextGraph; | ||
|
||
return app; | ||
if (logger != null && contextGraph != null) | ||
{ | ||
contextGraph.ValidationResults.ForEach((v) => | ||
logger.Log( | ||
v.LogLevel, | ||
new EventId(), | ||
v.Message, | ||
exception: null, | ||
formatter: (m, e) => m)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace JsonApiDotNetCore.Internal | ||
{ | ||
internal class ValidationResult | ||
{ | ||
public ValidationResult(LogLevel logLevel, string message) | ||
{ | ||
LogLevel = logLevel; | ||
Message = message; | ||
} | ||
|
||
public LogLevel LogLevel { get; set; } | ||
public string Message { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using JsonApiDotNetCore.Builders; | ||
using JsonApiDotNetCore.Internal; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
|
||
namespace UnitTests.Internal | ||
{ | ||
public class ContextGraphBuilder_Tests | ||
{ | ||
[Fact] | ||
public void AddDbContext_Does_Not_Throw_If_Context_Contains_Members_That_DoNot_Implement_IIdentifiable() | ||
{ | ||
// arrange | ||
var contextGraphBuilder = new ContextGraphBuilder(); | ||
|
||
// act | ||
contextGraphBuilder.AddDbContext<TestContext>(); | ||
var contextGraph = contextGraphBuilder.Build() as ContextGraph; | ||
|
||
// assert | ||
Assert.Empty(contextGraph.Entities); | ||
} | ||
|
||
[Fact] | ||
public void Adding_DbContext_Members_That_DoNot_Implement_IIdentifiable_Creates_Warning() | ||
{ | ||
// arrange | ||
var contextGraphBuilder = new ContextGraphBuilder(); | ||
|
||
// act | ||
contextGraphBuilder.AddDbContext<TestContext>(); | ||
var contextGraph = contextGraphBuilder.Build() as ContextGraph; | ||
|
||
// assert | ||
Assert.Equal(1, contextGraph.ValidationResults.Count); | ||
Assert.Contains(contextGraph.ValidationResults, v => v.LogLevel == LogLevel.Warning); | ||
} | ||
|
||
private class Foo { } | ||
|
||
private class TestContext : DbContext | ||
{ | ||
public DbSet<Foo> Foos { get; set; } | ||
} | ||
} | ||
|
||
} |