Skip to content

Issue when using AddApiExplorer and JsonTranscoding #1109

Open
@mcastagna-sa

Description

@mcastagna-sa

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Hey guys, i'm having a problem with a project i'm working on and i wanted to see if i'm missing anything or maybe i've stumbled on a bug.

I have created a minimal project where i'm able to reproduce the behaviour, I'm goint to try to guide you pasting snippets of code here.

When i run the program, and try to hit the endpoint v2 with postman everything goes as expected.

image

But, instead, if i call v1, which should be responded by the jsontranscode, i get 404.

image

I've tracked this down to the call of AddApiExplorer. If i remove that call i'm able to get the v1 as i should.

What do you think about this? Is this the expected behaviour? Am i doing something wrong? Is this a bug? If it is, is a bug in the jsontranscoding or asp.versioning library?

Thank you so much for your time and sorry the lenght of this post. Let me know if you need anything else that could help you answering this.

Expected Behavior

Endpoint is found and executed.

Steps To Reproduce

Program.cs

static void Main(string[] args)
 {
     var app = CreateHostBuilder(args).Build();

     app.UseRouting();

     app.MapControllers();

     app.MapGrpcService<GreeterService>();

     app.Run();
 }
  
 private static WebApplicationBuilder CreateHostBuilder(string[] args)
 {
     var builder = WebApplication.CreateBuilder(args);

     builder
         .WebHost.ConfigureKestrel((context, options) =>
         {
             options.ListenLocalhost(8080);
         });

     builder.Services.AddControllers();

     builder.Services.AddApiVersioning(options =>
          {
              options.ReportApiVersions = true;
          })
          .AddApiExplorer(options =>
          {
              options.GroupNameFormat = "'v'VVV";
              options.SubstituteApiVersionInUrl = true;                     
          });

     builder.Services
         .AddGrpc()
         .AddJsonTranscoding();

     return builder;
 }

V2Controller.cs

[ApiController]
[ApiVersion("2.0")]
[Produces("application/json")]
[Route("api/v{version:apiVersion}/controller/{param}")]
public class V2Controller : Controller
{
    [HttpGet]
    [Route("route")]
    public async Task<IActionResult> GetEndpoint()
    {
        return Ok("ok get v2");
    }
}

And greet.proto (This is the wiki example as you can see, just added the route i need to demostrate my point)

syntax = "proto3";

option csharp_namespace = "TestVersioningGRPC.Proto";
import "google/api/annotations.proto";

package greet;

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {
    option (google.api.http) = {        
      get: "/api/v1/controller/{name}/route"
    };
  }
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

Exceptions (if any)

No response

.NET Version

8.0.304

Anything else?

No response

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions