FastEndpoints / FastEndpoints

A light-weight REST API development framework for ASP.NET 6 and newer.
https://fast-endpoints.com
MIT License
4.74k stars 282 forks source link

`SendCreatedAt` Method Does Not Redirect to the Correct Endpoint #717

Closed JKamsker closed 5 months ago

JKamsker commented 5 months ago

Description

The SendCreatedAt method in the FastEndpoints library does not properly redirect to the intended endpoint. The issue arises because the typeof(TEndpoint).EndpointName(verb?.ToString("F"), routeNumber) does not resolve to the correct endpoint name. For instance, in my case, it incorrectly resolves to MyAppApplicationFeaturesTestsEntitiesGetTestEntityEndpoint but should rather be something like Get TestEntity

Steps to Reproduce

  1. Call the SendCreatedAt method in a FastEndpoints endpoint.
  2. Observe that the redirection does not lead to the intended endpoint.

Code References

Problem

The linkGen object attempts to look up the endpoint name in its dictionary and returns null because the dictionary entries only contain items like [Get TestEntity, Microsoft.AspNetCore.Http.Endpoint[]]. However, the lookup is performed using the incorrectly resolved endpoint name MyAppApplicationFeaturesTestsEntitiesGetTestEntityEndpoint.

Expected Behavior

The SendCreatedAt method should correctly resolve the endpoint name to match the entries in the EndpointNameAddressScheme dictionary and perform the redirection to the intended endpoint.

Additional Information

dj-nitehawk commented 5 months ago

just tried the following and it works as expected.

sealed class GetEndpoint : EndpointWithoutRequest
{
    public override void Configure()
    {
        Get("something/{id}");
        AllowAnonymous();
    }

    public override async Task HandleAsync(CancellationToken c)
    {
        await SendAsync(Route<int>("id"));
    }
}

sealed class CreateEndpoint : EndpointWithoutRequest
{
    public override void Configure()
    {
        Post("create");
        AllowAnonymous();
    }

    public override async Task HandleAsync(CancellationToken c)
    {
        await SendCreatedAtAsync<GetEndpoint>(new { id = 100 }, "created!", generateAbsoluteUrl: true);
    }
}

does not properly redirect to the intended endpoint

fyi, the only responsibility of the endpoint is to correctly add a location header to the response. it does not automatically redirect the web browser. typically, your client app will read the location header for 201 responses and do the redirection itself.

also, if you're customizing endpoint names via the .WithName() extension method, you need to use the given custom names as explained here in the pink note section.

if i'm off base, pls attach a repro project i can debug and understand what exactly you're trying to do.

JKamsker commented 5 months ago

Oh thats why, thanks! All of my endpoints have a custom name to be easier readable in swagger/redoc. Would it be hard to make fastendpoints aware of my custom names automatically?

dj-nitehawk commented 5 months ago

we've tried to come up with a solution for that, but have not succeeded yet.