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

[Bug]: http-server-csharp - Incorrect Route Generation in ASP.NET Core Controller Base Classes #5484

Open
4 tasks done
mario-guerra opened this issue Jan 3, 2025 · 0 comments
Assignees
Labels
bug Something isn't working emitter:service:csharp

Comments

@mario-guerra
Copy link
Member

mario-guerra commented Jan 3, 2025

The TypeSpec code generator for ASP.NET Core controllers incorrectly generates absolute route templates in the base controller classes, leading to routing issues in derived controllers. This prevents the framework from correctly combining base routes defined on derived controllers with the action-specific route templates in the base class.

Description:

When generating code for ASP.NET Core controllers with inheritance, the generator incorrectly applies [Route] attributes with absolute paths (starting with /) to the action methods in the base controller class. This overrides the base route defined on the derived controller and prevents the framework from correctly matching requests to the intended endpoints.

Expected Behavior:

The generated code should:

  1. Define a base route on the derived controller using the [Route] attribute (e.g., [Route("projects")]).
  2. Use relative route templates in the HTTP verb attributes (e.g., [HttpGet("{projectId}")], [HttpPost("{projectId}")], [HttpDelete("{projectId}")]) on the action methods in the base controller class.
  3. Allow the framework to correctly combine the base route from the derived controller with the relative route templates from the base controller to form the complete URL path.

Actual Behavior:

The generated code:

  1. Defines a base route on the derived controller using the [Route] attribute (e.g., [Route("projects")]).
  2. Incorrectly applies [Route] attributes with absolute paths (e.g., [Route("/projects/{projectId}")]) to the action methods in the base controller class.
  3. This results in the framework ignoring the base route defined on the derived controller and failing to match requests to the intended endpoints.

Example:

  • Incorrect Generated Code (in base controller):

    [HttpGet]
    [Route("/projects/{projectId}")]
    public virtual async Task<IActionResult> GetProject(string projectId) { ... }
  • Correct Generated Code (in base controller):

    [HttpGet("{projectId}")]
    public virtual async Task<IActionResult> GetProject(string projectId) { ... }
  • Derived Controller:

    [Route("projects")]
    [ApiController]
    public partial class ProjectOpsController : ProjectOpsOperationsControllerBase { ... }

Reproduction

Steps to Reproduce:

  1. Use TypeSpec to generate code for an ASP.NET Core controller with inheritance.
  2. Observe the generated code in the base controller class.
  3. Run the generated service and attempt to call an endpoint that uses a path parameter.

Checklist

@mario-guerra mario-guerra added the bug Something isn't working label Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working emitter:service:csharp
Projects
None yet
Development

No branches or pull requests

2 participants