使用Swashbuckle的AWS API Gateway Swagger

4
2个回答

5
AWS Api网关使用Swagger中的x-amazon-apigateway-*扩展来配置您的REST API。
我认为目前还没有一个库可以通过Swashbuckle来管理它,但是Swashbuckle有一些接口可以实现以生成自定义的Swagger扩展,如在Swashbuckle文档中所述。例如,如果您想要生成x-amazon-apigateway-integration扩展,可以通过实现Swashbuckle.AspNetCore.SwaggerGen.IOperationFilter接口来完成。
    public class AwsApiGatewayIntegrationFilter : IOperationFilter
    {
        public void Apply(Operation operation, OperationFilterContext context)
        {
            operation.Extensions.Add("x-amazon-apigateway-integration",new
            {
                type = "aws",
                uri = "arn_to_your_aws_resource"
            });
        }
    }

在Swagger生成器设置期间配置它:

services.AddSwaggerGen(c =>
{
     c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "My API",
                Description = "",
                TermsOfService = "",
            });
    c.OperationFilter<AwsApiGatewayIntegrationFilter>();
});

谢谢@asidis。完美地工作着。 - Abidali

3
更为现代的asidis答案应该如下所示:(使用dotnet 3.0.0和swashbuckle 5.0.0-rc4)
public class AwsApiGatewayIntegrationFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        operation.Extensions.Add("x-amazon-apigateway-integration", new OpenApiObject
        {
            ["type"] = new OpenApiString("aws"),
            ["uri"] = new OpenApiString("uri_to_your_aws_resource"),
        });

    }
}

在 Startup.cs 文件中。
services.AddSwaggerGen(c =>
{
      c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
      c.OperationFilter<AwsApiGatewayIntegrationFilter>();
);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接