Nswag 在同一 Http 状态代码中指定多个响应示例?
Nswag specify multiple Response Example in Same Http Status Code?
我需要通过 C# 设置一些具有相同 http 状态代码的响应示例。
但我还是不知道。
我该怎么办?
请给我一些提示...
// i want to create json , like this.
responses:
'404':
description: please , let It worked...
content:
application/json:
schema:
$ref: '#/components/schemas/TestModel'
examples:
success :
summary: Example of a successful response
value:
code : 1
message : "test message 1"
success2 :
summary: Example of a successful response2
value:
code : 2
message: "test message 2"
更新:
我使用 NSwag,像这样
我需要让代码 404 有两个示例响应
/// <summary></summary>
/// <remarks></remarks>
/// <response code="200"></response>
/// <response code="404">Message 1</response>
/// <response code="404">Message 2</response> // <---- error will occur ,
[HttpPost]
[ProducesResponseType(typeof(CreateIdentificationResponse), 200)]
[ProducesResponseType(typeof(ErrorResponse), 404)]
[ProducesResponseType(typeof(ErrorResponse), 404)]
public IActionResult Post(){
}
您可以这样设置 SwaggerResponse
属性:
[HttpGet("")]
[SwaggerResponse(200, "Myfile content", typeof(MyFile))]
[SwaggerResponse(404, "Could not find file", typeof(MyFile))]
public async Task<IActionResult> YouAction()
它将记录 2 个可能的响应状态代码:200
和 404
。
您可以根据需要应用任意数量的这些属性
我得到答案了~~~
// using
// [ExampleAttribute (nameof(MyError.MyErrorFieldName))]
public class ExampleAttribute : OpenApiOperationProcessorAttribute
{
public ExampleAttribute(string errorField)
: base(typeof(ExampleAppender))
{
var type = typeof(GeneralError);
var field = type.GetField(errorField, BindingFlags.Public | BindingFlags.Static);
var error = field?.GetValue(null) as IMyError;
Parameters = new object[] { error };
}
private class ExampleAppender : IOperationProcessor
{
private readonly IMyError _error;
public ExampleAppender(IMyError error)
{
_error = error ?? throw new ArgumentNullException(nameof(error));
}
public bool Process(OperationProcessorContext context)
{
var openApiResponses = context.OperationDescription.Operation.Responses;
openApiResponses.TryAdd(((int)_error.StatusCode).ToString(), new OpenApiResponse());
var apiResponse = openApiResponses[((int)_error.StatusCode).ToString()];
if (!string.IsNullOrEmpty(_error.StatusCode.ToString()))
{
apiResponse.Description = _error.StatusCode.ToString();
}
apiResponse.Content.TryAdd(MediaTypeNames.Application.Json, new OpenApiMediaType() { Schema = JsonSchema.FromType<MyErrorResponse>() });
var openApiMediaType = apiResponse.Content[MediaTypeNames.Application.Json];
openApiMediaType.Examples.TryAdd(
_error.Description ?? "",
new OpenApiExample()
{
Description = _error.Description,
Value = new MyErrorResponse(_error.Message)
});
return true;
}
}
}
follow the OpenApiOperationProcessorAttribute
NSwag Tutorial: Implement a custom operation processor to define ReDoc code samples
我需要通过 C# 设置一些具有相同 http 状态代码的响应示例。
但我还是不知道。
我该怎么办?
请给我一些提示...
// i want to create json , like this.
responses:
'404':
description: please , let It worked...
content:
application/json:
schema:
$ref: '#/components/schemas/TestModel'
examples:
success :
summary: Example of a successful response
value:
code : 1
message : "test message 1"
success2 :
summary: Example of a successful response2
value:
code : 2
message: "test message 2"
更新:
我使用 NSwag,像这样
我需要让代码 404 有两个示例响应
/// <summary></summary>
/// <remarks></remarks>
/// <response code="200"></response>
/// <response code="404">Message 1</response>
/// <response code="404">Message 2</response> // <---- error will occur ,
[HttpPost]
[ProducesResponseType(typeof(CreateIdentificationResponse), 200)]
[ProducesResponseType(typeof(ErrorResponse), 404)]
[ProducesResponseType(typeof(ErrorResponse), 404)]
public IActionResult Post(){
}
您可以这样设置 SwaggerResponse
属性:
[HttpGet("")]
[SwaggerResponse(200, "Myfile content", typeof(MyFile))]
[SwaggerResponse(404, "Could not find file", typeof(MyFile))]
public async Task<IActionResult> YouAction()
它将记录 2 个可能的响应状态代码:200
和 404
。
您可以根据需要应用任意数量的这些属性
我得到答案了~~~
// using
// [ExampleAttribute (nameof(MyError.MyErrorFieldName))]
public class ExampleAttribute : OpenApiOperationProcessorAttribute
{
public ExampleAttribute(string errorField)
: base(typeof(ExampleAppender))
{
var type = typeof(GeneralError);
var field = type.GetField(errorField, BindingFlags.Public | BindingFlags.Static);
var error = field?.GetValue(null) as IMyError;
Parameters = new object[] { error };
}
private class ExampleAppender : IOperationProcessor
{
private readonly IMyError _error;
public ExampleAppender(IMyError error)
{
_error = error ?? throw new ArgumentNullException(nameof(error));
}
public bool Process(OperationProcessorContext context)
{
var openApiResponses = context.OperationDescription.Operation.Responses;
openApiResponses.TryAdd(((int)_error.StatusCode).ToString(), new OpenApiResponse());
var apiResponse = openApiResponses[((int)_error.StatusCode).ToString()];
if (!string.IsNullOrEmpty(_error.StatusCode.ToString()))
{
apiResponse.Description = _error.StatusCode.ToString();
}
apiResponse.Content.TryAdd(MediaTypeNames.Application.Json, new OpenApiMediaType() { Schema = JsonSchema.FromType<MyErrorResponse>() });
var openApiMediaType = apiResponse.Content[MediaTypeNames.Application.Json];
openApiMediaType.Examples.TryAdd(
_error.Description ?? "",
new OpenApiExample()
{
Description = _error.Description,
Value = new MyErrorResponse(_error.Message)
});
return true;
}
}
}
follow the OpenApiOperationProcessorAttribute
NSwag Tutorial: Implement a custom operation processor to define ReDoc code samples