dotnet-sdk icon indicating copy to clipboard operation
dotnet-sdk copied to clipboard

[Topic] Attribute doesn't support dynamic routes

Open fabistb opened this issue 2 years ago • 4 comments

Currently the [Topic] cant be used if the route contains a dynamic part. A example here is a version in the route.

Example dynamic route:

[ApiController]
[ApiVersion("1.0")]
[Route("api/v{v:apiVersion}/example]
public class ExampleController : ControllerBase
{
  [HttpPost()]
  [Topic("messagebus", "topic")
  public async Task<IActionResult> PubSub([FromBody] object message)
  {
  ...
  }
}

Example static route:

[ApiController]
[Route("api/v1.0/example]
public class ExampleController : ControllerBase
{
  [HttpPost()]
  [Topic("messagebus", "topic")
  public async Task<IActionResult> PubSub([FromBody] object message)
  {
  ...
  }
}

Is their a specific reason why the dynamic routes aren't supported?

fabistb avatar Aug 01 '23 09:08 fabistb

As far as I'm aware, there is no direct reason for it not working. We just parse the attributes and add them to an endpoint collection.

I'm not super familiar with this space though, does this work for other attributes that are nested like this? I'm worried the reason it wouldn't work is because we're taking the literal and don't have the information to interpret the path when we read the attributes.

halspang avatar Aug 17 '23 17:08 halspang

Thanks for the explanation.

Not sure here since I also can't think about an attribute currently which is as nested as this one.

fabistb avatar Sep 11 '23 17:09 fabistb

It looks like the Dapr route building logic doesn't support routes with parameters (i.e. the logic assumes the parts are all literals):

https://github.com/dapr/dotnet-sdk/blob/99d874a2b138af020df099a0fc0a09a7d0597fae/src/Dapr.AspNetCore/DaprEndpointRouteBuilderExtensions.cs#L201

As @halspang suggests, we'd need to investigate whether we can get the resolved values out of ASP.NET in order to build the route strings.

philliphoff avatar Sep 15 '23 17:09 philliphoff

I believe this is the same issue as #791 and #882. As was mentioned in those issues, there is a fundamental question of how Dapr subscriptions should work in a multi-versioned API (the likely reason for having dynamic routes). For example, if two controllers representing two versions of the same API have the same topics (i.e. what's the likelihood of these changing between API versions), how should messages be delivered?

philliphoff avatar Sep 15 '23 20:09 philliphoff