Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

OFFI-99: Inferred parameters are not always recognized correctly causing exceptions #485

Merged
merged 1 commit into from
Aug 29, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public static IEndpointRouteBuilder AddStripeMiddlewareEndpoint(this IEndpointRo
}

private static async Task<IResult> AddStripeMiddlewareAsync(
string? shoppingCartId,
IStripePaymentService stripePaymentService,
[FromRoute] string? shoppingCartId,
[FromServices] IStripePaymentService stripePaymentService,
[FromQuery(Name = "payment_intent")] string? paymentIntent = null
)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public static IEndpointRouteBuilder AddFreeEndpoint(this IEndpointRouteBuilder b

[Authorize(AuthenticationSchemes = "Api")]
private static async Task<IResult> AddFreeAsync(
string? shoppingCartId,
IAuthorizationService authorizationService,
HttpContext httpContext,
IPaymentService paymentService
[FromRoute] string? shoppingCartId,
Copy link
Member

Choose a reason for hiding this comment

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

Why not use them as properties decorated with FromRoute, FromService instead of injecting them within the constructor?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a static class. How would that work?

Copy link
Member

Choose a reason for hiding this comment

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

You can use it like this

[HttpGet("api/products/{id}")]
public IActionResult GetProduct([FromRoute] int id)
{
    // id will be populated with the value from the route
}

Copy link
Member

Choose a reason for hiding this comment

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

I remember I used [FromRoute] in the RazorPage model property

[FromRoute("id")]
public string Id { get; set; }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you tell me why this is a better approach? What's the benefit? @hishamco

Copy link
Member

Choose a reason for hiding this comment

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

Much cleaner and no need to use constructor injection

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I understand that it would be better, but how would you do it within this static minimal API class? We don't want much refactoring.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this could only be done by turning this from a minimal API endpoint class into an API controller? But as I recall OC is moving the other direction (away from API controllers) for performance reasons.

[FromServices] IAuthorizationService authorizationService,
[FromServices] HttpContext httpContext,
[FromServices] IPaymentService paymentService
)
{
if (!await authorizationService.AuthorizeAsync(httpContext.User, ApiPermissions.CommerceApi))
Expand All @@ -49,12 +49,12 @@ public static IEndpointRouteBuilder AddCallbackEndpoint(this IEndpointRouteBuild

[Authorize(AuthenticationSchemes = "Api")]
private static async Task<IResult> AddCallbackAsync(
string paymentProviderName,
string? orderId,
[FromRoute] string paymentProviderName,
[FromRoute] string? orderId,
[FromQuery] string? shoppingCartId,
IAuthorizationService authorizationService,
HttpContext httpContext,
IPaymentService paymentService
[FromServices] IAuthorizationService authorizationService,
[FromServices] HttpContext httpContext,
[FromServices] IPaymentService paymentService
)
{
if (!await authorizationService.AuthorizeAsync(httpContext.User, ApiPermissions.CommerceApi))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public static IEndpointRouteBuilder AddAddItemEndpoint(this IEndpointRouteBuilde
[Authorize(AuthenticationSchemes = "Api")]
private static async Task<IResult> AddItemAsync(
[FromBody] AddItemViewModel addItemVM,
IAuthorizationService authorizationService,
HttpContext httpContext,
IShoppingCartService shoppingCartService,
IHtmlLocalizer<AddItemViewModel> htmlLocalizer
[FromServices] IAuthorizationService authorizationService,
[FromServices] HttpContext httpContext,
[FromServices] IShoppingCartService shoppingCartService,
[FromServices] IHtmlLocalizer<AddItemViewModel> htmlLocalizer
)
{
if (!await authorizationService.AuthorizeAsync(httpContext.User, ApiPermissions.CommerceApi))
Expand Down Expand Up @@ -67,10 +67,10 @@ public static IEndpointRouteBuilder AddUpdateEndpoint(this IEndpointRouteBuilder
[Authorize(AuthenticationSchemes = "Api")]
private static async Task<IResult> UpdateAsync(
[FromBody] UpdateViewModel updateVM,
IAuthorizationService authorizationService,
HttpContext httpContext,
IShoppingCartService shoppingCartService,
IHtmlLocalizer<UpdateViewModel> htmlLocalizer
[FromServices] IAuthorizationService authorizationService,
[FromServices] HttpContext httpContext,
[FromServices] IShoppingCartService shoppingCartService,
[FromServices] IHtmlLocalizer<UpdateViewModel> htmlLocalizer
)
{
if (!await authorizationService.AuthorizeAsync(httpContext.User, ApiPermissions.CommerceApi))
Expand Down Expand Up @@ -105,10 +105,10 @@ public static IEndpointRouteBuilder AddRemoveLineEndpoint(this IEndpointRouteBui
[Authorize(AuthenticationSchemes = "Api")]
private static async Task<IResult> RemoveLineAsync(
[FromBody] RemoveLineViewModel removeLineVM,
IAuthorizationService authorizationService,
HttpContext httpContext,
IShoppingCartService shoppingCartService,
IHtmlLocalizer<RemoveLineViewModel> htmlLocalizer
[FromServices] IAuthorizationService authorizationService,
[FromServices] HttpContext httpContext,
[FromServices] IShoppingCartService shoppingCartService,
[FromServices] IHtmlLocalizer<RemoveLineViewModel> htmlLocalizer
)
{
if (!await authorizationService.AuthorizeAsync(httpContext.User, ApiPermissions.CommerceApi))
Expand Down Expand Up @@ -143,10 +143,10 @@ public static IEndpointRouteBuilder AddRetrieveAsyncEndpoint(this IEndpointRoute

[Authorize(AuthenticationSchemes = "Api")]
private static async Task<IResult> RetrieveAsync(
string? shoppingCartId,
IAuthorizationService authorizationService,
HttpContext httpContext,
IShoppingCartPersistence shoppingCartPersistence
[FromRoute] string? shoppingCartId,
[FromServices] IAuthorizationService authorizationService,
[FromServices] HttpContext httpContext,
[FromServices] IShoppingCartPersistence shoppingCartPersistence
)
{
if (!await authorizationService.AuthorizeAsync(httpContext.User, ApiPermissions.CommerceApi))
Expand Down
Loading