CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.05k stars 172 forks source link

Trying to get swagger working with my project. #335

Closed shawnarvin closed 1 month ago

shawnarvin commented 5 months ago

The swagger UI page does not show any operations after installing and configuring swagger using the Swashbuckle.AspNetCore project. screenshot

jchannon commented 5 months ago

Does this help. https://github.com/CarterCommunity/Carter/blob/main/samples/CarterSample/Features/Actors/ActorsModule.cs

On Mon, 29 Jan 2024 at 14:04, Shawn @.***> wrote:

The swagger UI page does not show any operations after installing and configuring swagger using the Swashbuckle.AspNetCore project. screenshot.png (view on web) https://github.com/CarterCommunity/Carter/assets/14608669/d4ccf7a5-0595-4691-9926-bc65391fd0f5

— Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/335, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJQL25Z75GJFNJ3NGXDYQ6T6RAVCNFSM6AAAAABCPP3XESVHI2DSMVQWIX3LMV43ASLTON2WKOZSGEYDKNJXGU4TCMQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

idusortus commented 5 months ago

Similar issue here. I followed your suggestion, @jchannon, but I was unable to get the endpoints to show up in SwaggerUI.

This works fine w/o Carter:

(Program.cs)
builder.Services.AddSwaggerGen();
...
CreateArticle.MapEndpoint(app);

(CreateArticle.cs)
public static void MapEndpoint(this IEndpointRouteBuilder endpoints)
  {
      endpoints.MapPost("api/articles", async (CreateArticle.Command command, ISender sender) =>
      {
          var result = await sender.Send(command);
          if (result.IsFailure)
          {
              return Results.BadRequest(result.Error);
          }

          return Results.Ok(result.Value);
      });
  }

This does not:

(Program.cs)
...
builder.Services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new OpenApiInfo
    {
        Description = "Carter Sample API",
        Version = "v1",
        Title = "A Carter API to manage Actors/Films/Crew etc"
    });

    options.DocInclusionPredicate((s, description) =>
    {
        foreach (var metaData in description.ActionDescriptor.EndpointMetadata)
        {
            if (metaData is IIncludeOpenApi) return true;            
        }
        return false;
    });
});
...
builder.Services.AddCarter();
...
app.MapCarter();

(CreateArticle.cs)
  public class Endpoint : ICarterModule
  {
      public void AddRoutes(IEndpointRouteBuilder app)
      {
          app.MapPost("api/articles", async (CreateArticle.Command command, ISender sender) =>
          {
              var result = await sender.Send(command);
              if (result.IsFailure)
              {
                  return Results.BadRequest(result.Error);
              }
              return Results.Ok(result.Value);
          })
          .WithTags("SomeTag")
          .WithName("Something")
          .IncludeInOpenApi();
      }
  }

The endpoint functions as expected in both examples. The Carter version isn't like the namesake's Daddy... way too shy for Swagger 😄

I haven't dug into the issue, was looking around for solutions and came across this. End of my night, I'll look again tomorrow to check for obvious oversights on my part.

Happy to push a repro repo if it helps.

Thanks!

idusortus commented 5 months ago

Whoops... I had the Endpoint : ICarterModule class nested within a static class.

The RouteEndpoint defined in my : ICarterModule class now appears as expected in the SwaggerUI.

Note that it works fine without the inclusion of .IncludeInOpenApi();

Thanks!

jchannon commented 1 month ago

I can't reproduce this. If you run the CarterSample app you'll see CastMembers doesn't appear in SwaggerUI. Once you add IncludeInOpenApi they appear.

If you'd like to push up a repro then let me know 👍