I am using Fastendpoints to make my api but the issue I am getting is my request is not bounding to query params in GET request in Swagger. Here is my code.
Program.cs
builder.Services.Configure<JsonOptions>(options =>
{
var enumConverter = new JsonStringEnumConverter();
options.SerializerOptions.Converters.Add(enumConverter);
});
// Add services to the container.
builder.Services
.AddAuthenticationJwtBearer(s =>
{
s.SigningKey = builder.Configuration["JwtSettings:Secret"];
},
o =>
{
o.TokenValidationParameters.ValidAudience = builder.Configuration["JwtSettings:Audience"];
o.TokenValidationParameters.ValidIssuer = builder.Configuration["JwtSettings:Issuer"];
})
.AddAuthorization()
.AddFastEndpoints()
.AddSwaggerDocument(c =>
{
c.DocumentName = "v1";
c.Title = "OctuFit API documentation";
c.Version = "v1";
c.Description = "OctuFit API documentation";
// Enable JWT Bearer Authentication
c.AddSecurity("JWT", new NSwag.OpenApiSecurityScheme
{
Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
Name = "Authorization",
In = NSwag.OpenApiSecurityApiKeyLocation.Header,
Description = "Type 'Bearer {your JWT token}' to authenticate"
});
// Set Security Requirement
c.OperationProcessors.Add(new NSwag.Generation.Processors.Security.AspNetCoreOperationSecurityScopeProcessor("JWT"));
});
-----------------
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseDefaultExceptionHandler();
app.UseFastEndpoints(c =>
{
c.Endpoints.RoutePrefix = "api";
});
app.UseSwaggerGen(uiConfig: c =>
{
c.DefaultModelsExpandDepth = -1;
});
// Custom middlewares
app.UseMiddleware<PopulateUserDataMiddleware>();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<OctuFitContext>();
context.Database.Migrate();
await Seeder.Seed(services);
}
app.Run();
My endpoint class is
public static class GetRolesEndpoint
{
public class GetRolesRequest : PaginationRequestBase
{
}
public class GetRoles : Endpoint<GetRolesRequest, PaginationResponse<IEnumerable<GetRolesResponse>>>
{
private readonly IUnitOfWork unitOfWork;
private readonly IHasher hasher;
private readonly RoleManager roleManager;
public GetRoles(IUnitOfWork unitOfWork,
IHasher hasher,
RoleManager roleManager)
{
this.unitOfWork = unitOfWork;
this.hasher = hasher;
this.roleManager = roleManager;
}
public override void Configure()
{
Get("roles");
Options(o => o.WithTags("Roles"));
}
public override async Task HandleAsync(GetRolesRequest req, CancellationToken ct)
{
var rolesQuery = roleManager.Roles
.AsNoTracking()
.Include(i => i.CreatedBy)
.Include(i => i.UpdatedBy)
.OrderByDescending(o => o.CreatedAt)
.Where(x => string.IsNullOrWhiteSpace(req.Search) || x.Name == null || x.Name.Contains(req.Search))
.Where(x => x.TenantId == unitOfWork.TenantId)
.AsQueryable();
Response = new PaginationResponse<IEnumerable<GetRolesResponse>>
{
TotalRecords = await rolesQuery.CountAsync(),
Data = await rolesQuery
.Skip((req.Page - 1) * req.PageSize)
.Take(req.PageSize)
.Select(role => new GetRolesResponse(
hasher.Hash(role.Id),
role.Name,
role.ToModifier()))
.ToListAsync()
};
}
}
public record GetRolesResponse(string Id, string? Name, Modifier Modifier);
}
And my base class is
public class PaginationRequestBase
{
private int _page = 1;
private int _pageSize = 10;
[Required]
[QueryParam]
public int Page
{
get => _page;
set
{
if (value < 1)
{
_page = 1;
}
else if (value > int.MaxValue)
{
_page = 1;
}
else
{
_page = value;
}
}
}
[Required]
[QueryParam]
public int PageSize
{
get => _pageSize;
set
{
if (value < 1)
{
_pageSize = 1;
}
else if (value > int.MaxValue)
{
_pageSize = 10;
}
else
{
_pageSize = value;
}
}
}
[QueryParam]
public string? Search { get; set; }
}
The issue I am getting is, in Swagger, for GET request, I am getting the body instead of query params as shown:
Any help will be appreciated.
Thank you.
2