我正在使用具有最少 API 和功能文件夹方法的 .NET 8。
Features
Delete
Handler.cs
JsonRequest.cs
JsonResponse.cs
Get
Handler.cs
JsonRequest.cs
JsonResponse.cs
Post
Handler.cs
JsonRequest.cs
JsonResponse.cs
Put
Handler.cs
JsonRequest.cs
JsonResponse.cs
如果我保留如图所示的命名,则 Swagger 会弹出 500 错误。 并没有真正说明原因,但我已经追踪到它具有与参数相同的对象名称,但每个对象都是不同的。
public static async Task<IResult> PostUser(HttpContext httpContext, IMediator mediator, [FromBody] Post.JsonRequest jsonRequest)
{
var request = new Post.Handler.Request(httpContext, jsonRequest);
var response = await mediator.Send(request);
return response.ResultForHttp;
}
public static async Task<IResult> PutUser(HttpContext httpContext,IMediator mediator,[FromBody] Put.JsonRequest jsonRequest)
{
var request = new Put.Handler.Request(httpContext, jsonRequest);
var response = await mediator.Send(request);
return response.ResultForHttp;
}
public static async Task<IResult> DeleteUser(HttpContext httpContext, IMediator mediator, [FromBody] Delete.JsonRequest jsonRequest)
{
var request = new Delete.Handler.Request(httpContext, jsonRequest);
var response = await mediator.Send(request);
return response.ResultForHttp;
}
如果我将类名更改为唯一名称,那么 swagger 将按预期工作:
public static async Task<IResult> PostUser(HttpContext httpContext, IMediator mediator, [FromBody] Post.PostJsonRequest jsonRequest)
{...}
public static async Task<IResult> PutUser(HttpContext httpContext,IMediator mediator,[FromBody] Put.PutJsonRequest jsonRequest)
{...}
public static async Task<IResult> DeleteUser(HttpContext httpContext, IMediator mediator, [FromBody] Delete.DeleteJsonRequest jsonRequest)
{...}
有没有办法配置 swagger 以允许在最小的 API 中使用相同名称但不同的对象?
注意:整个项目仅处理一种资源 - 用户。
注意:添加 3 个 JSON 类以帮助澄清。
删除:
namespace Api.Users.Features.Delete;
public class DeleteJsonRequest : BaseJsonRequest
{
[JsonPropertyName("channel")] public ChannelDataJson ChannelData { get; set; } = new();
[JsonPropertyName("userIdentifiers")] public List<string> UserIdentifiers { get; set; } = [];
}
帖子:
namespace Api.Users.Features.Post;
public class PostJsonRequest : BaseJsonRequest
{
[JsonPropertyName("channel")] public ChannelDataJson ChannelData { get; set; } = new();
[JsonPropertyName("users")] public List<FullUser> Users { get; set; } = [];
}
放置:
namespace Api.Users.Features.Put;
public class PutJsonRequest : BaseJsonRequest
{
[JsonPropertyName("channel")] public ChannelDataJson ChannelData { get; set; } = new();
[JsonPropertyName("users")] public List<FullUser> Users { get; set; } = [];
[JsonPropertyName("lockAction")] public EnumLockAction Action { get; set; } = EnumLockAction.None;
}
重申一下:如果我将这 3 个类中的每一个的类名更改为 RequestJson swagger 中断,即使它们都位于不同的命名空间中。
我被引导至 stackoverflow.com/a/57665848 寻求可能的答案。 经过测试,我可以证明它确实回答了我的问题。
主持人:您能否将其标记为与上述条目重复?