我如何告诉 Swashbuckle 5 在 dotnetcore 3.0 中需要主体内容?
How can I tell Swashbuckle 5 that the body content is required in dotnetcore 3.0?
我正在尝试实施 Swashbuckle 5,我有几个方法可以像这样读取请求正文:
var requestBody = await Request.GetRawBodyStringAsync();
如何让 Swashbuckle/Swagger 将其作为参数读取,以便人们可以测试我的 API?我看到了一个非常 但那是针对二进制内容和早期版本的 Swashbuckle。
如有任何帮助,我们将不胜感激!
正如您已经在 Swashbuckle 5 中发现的那样,它是不同的,因为它切换到使用 Microsoft OpenApi.NET SDK。这就是对象模型不同的原因。否则它仍然以与您链接的 post 中的示例相同的方式工作。我已将案例翻译成您要发送原始文本字符串的场景。
创建一个自定义属性来标记读取原始字符串的方法。例如:
public class RawTextRequestAttribute : Attribute
{
public RawTextRequestAttribute()
{
MediaType = "text/plain";
}
public string MediaType { get; set; }
}
要修改 Swagger 定义,您需要一个 Swashbuckle operation filter 来检查此属性,如果找到则将请求正文自定义为纯字符串。这是执行此操作的示例实现:
public class RawTextRequestOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
RawTextRequestAttribute rawTextRequestAttribute = context.MethodInfo.GetCustomAttributes(true)
.SingleOrDefault((attribute) => attribute is RawTextRequestAttribute) as RawTextRequestAttribute;
if (rawTextRequestAttribute != null)
{
operation.RequestBody = new OpenApiRequestBody();
operation.RequestBody.Content.Add(rawTextRequestAttribute.MediaType, new OpenApiMediaType()
{
Schema = new OpenApiSchema()
{
Type = "string"
}
});
}
}
}
要使用的过滤器需要在配置Swagger时在启动时注册。
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
c.OperationFilter<RawTextRequestOperationFilter>();
});
}
然后将属性添加到读取原始请求的方法中。例如:
[HttpPost]
[RawTextRequest]
public async Task Post()
{
var requestBody = await Request.GetRawBodyStringAsync();
_logger.LogDebug(requestBody);
}
结果是您在 Swagger 中获得了请求正文的文本输入框 UI。
我正在尝试实施 Swashbuckle 5,我有几个方法可以像这样读取请求正文:
var requestBody = await Request.GetRawBodyStringAsync();
如何让 Swashbuckle/Swagger 将其作为参数读取,以便人们可以测试我的 API?我看到了一个非常
如有任何帮助,我们将不胜感激!
正如您已经在 Swashbuckle 5 中发现的那样,它是不同的,因为它切换到使用 Microsoft OpenApi.NET SDK。这就是对象模型不同的原因。否则它仍然以与您链接的 post 中的示例相同的方式工作。我已将案例翻译成您要发送原始文本字符串的场景。
创建一个自定义属性来标记读取原始字符串的方法。例如:
public class RawTextRequestAttribute : Attribute
{
public RawTextRequestAttribute()
{
MediaType = "text/plain";
}
public string MediaType { get; set; }
}
要修改 Swagger 定义,您需要一个 Swashbuckle operation filter 来检查此属性,如果找到则将请求正文自定义为纯字符串。这是执行此操作的示例实现:
public class RawTextRequestOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
RawTextRequestAttribute rawTextRequestAttribute = context.MethodInfo.GetCustomAttributes(true)
.SingleOrDefault((attribute) => attribute is RawTextRequestAttribute) as RawTextRequestAttribute;
if (rawTextRequestAttribute != null)
{
operation.RequestBody = new OpenApiRequestBody();
operation.RequestBody.Content.Add(rawTextRequestAttribute.MediaType, new OpenApiMediaType()
{
Schema = new OpenApiSchema()
{
Type = "string"
}
});
}
}
}
要使用的过滤器需要在配置Swagger时在启动时注册。
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
c.OperationFilter<RawTextRequestOperationFilter>();
});
}
然后将属性添加到读取原始请求的方法中。例如:
[HttpPost]
[RawTextRequest]
public async Task Post()
{
var requestBody = await Request.GetRawBodyStringAsync();
_logger.LogDebug(requestBody);
}
结果是您在 Swagger 中获得了请求正文的文本输入框 UI。