我有的:
我有一个 API,其中所有不成功的响应都返回“IApiException”
public class ApiException : Exception, IApiException
{
public ExceptionType Type { get; set; }
public int ErrorCode { get; set; }
public HttpStatusCode Status { get; set; }
public string Code { get; set; }
public List<Error> Errors { get; set; } = new List<Error>();
public ApiException(ExceptionType type, Reason reason, string message, HttpStatusCode httpStatus)
{
Type = type;
Errors.Add(new Error { Message = message, Reason = reason.ToString() });
Status = httpStatus;
Code = httpStatus.ToString();
}
}
如果我通过 ISchemaFilter 在 swashbuckle 中设置示例,它将在所有模型示例上放置相同的示例,因此 401 将返回与 429 相同的示例。
我想要的是:
我希望能够根据状态码返回不同的示例。
我试过什么:
我尝试创建一个 IOperationsFilter 来查找响应键,然后设置响应模式示例或响应示例。但它根本没有显示模型。
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
foreach (var response in operation.responses)
{
if (response.Key == "401")
{
response.Value.description = "Error";
response.Value.schema.example = new
{
Type = ExceptionType.Error.ToString(),
Errors = new {Message = "Unauthorized", Reason = Reason.Unauthorized},
Status = HttpStatusCode.Unauthorized,
Code = HttpStatusCode.Unauthorized.ToString()
};
}
}
}