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

Trying to get swagger working with my project. #335

Closed
shawnarvin opened this issue Jan 29, 2024 · 4 comments
Closed

Trying to get swagger working with my project. #335

shawnarvin opened this issue Jan 29, 2024 · 4 comments

Comments

@shawnarvin
Copy link

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

@jchannon
Copy link
Member

jchannon commented Jan 29, 2024 via email

@idusortus
Copy link

idusortus commented Jan 30, 2024

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
Copy link

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
Copy link
Member

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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants