Skip to content

Add optional params to control middleware #665

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 5 commits into from
Feb 12, 2020
Merged
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
46 changes: 37 additions & 9 deletions src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,27 @@ namespace JsonApiDotNetCore.Extensions
public static class IApplicationBuilderExtensions
{
/// <summary>
/// Adds necessary components such as routing to your application
/// Runs several internal JsonApiDotNetCore services to ensure proper configuration and registers required middlewares.
/// The <paramref name="skipRegisterMiddleware"/> can be used to skip any middleware registration, in which case the developer is
/// is responsible for registering middleware that are required for JsonApiDotNetCore.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"is" twice

/// </summary>
/// <param name="app"></param>
/// <returns></returns>
public static void UseJsonApi(this IApplicationBuilder app)
/// <param name="skipRegisterMiddleware">Indicates if JsonApiDotNetCore should skip middleware registration. This enabl.</param>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

half sentence.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one I noticed too, did a hotfix in ebaa091. If you could squeeze the removal of the 2nd "is" in one of your upcoming PRs would be appreciated :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #684.

/// <param name="useAuthentication">Indicates if .NET Core authentication middleware should be registered. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
/// <param name="useAuthorization">Indicates if .NET Core authentication middleware should be registered. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
/// <example>
/// This example illustrate which required middlewares should be registered when using the <paramref name="skipRegisterMiddleware"/> option.
/// <code>
/// app.UseJsonApi(skipRegisterMiddleware: true);
/// // JADNC requires routing
/// app.UseRouting();
/// // JADNC requires CurrentRequestMiddleware
/// app.UseMiddleware<CurrentRequestMiddleware>();
/// // JANDC requires the endpoint feature enabled as follows
/// app.UseEndpoints(endpoints => endpoints.MapControllers());
/// </code>
/// </example>
public static void UseJsonApi(this IApplicationBuilder app, bool skipRegisterMiddleware = false, bool useAuthentication = false, bool useAuthorization = false)
{
LogResourceGraphValidations(app);
using (var scope = app.ApplicationServices.CreateScope())
Expand All @@ -28,14 +44,26 @@ public static void UseJsonApi(this IApplicationBuilder app)
inverseRelationshipResolver?.Resolve();
}

// An endpoint is selected and set on the HttpContext if a match is found
app.UseRouting();
if (!skipRegisterMiddleware) {
// An endpoint is selected and set on the HttpContext if a match is found
app.UseRouting();

// middleware to run after routing occurs.
app.UseMiddleware<CurrentRequestMiddleware>();
if (useAuthentication)
{
app.UseAuthentication();
};

// Executes the endpoints that was selected by routing.
app.UseEndpoints(endpoints => endpoints.MapControllers());
if (useAuthorization)
{
app.UseAuthorization();
};

// middleware to run after routing occurs.
app.UseMiddleware<CurrentRequestMiddleware>();

// Executes the endpoints that was selected by routing.
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
}

/// <summary>
Expand Down