AWS API 使用 swashbuckle 的网关 Swagger

AWS API Gateway Swagger using swashbuckle

我想使用 Swashbuckle.AspNetCore.[=13= 从 ASP.NET 核心网络 API 生成 AWS API 网关 swagger (http://docs.aws.amazon.com/apigateway/latest/developerguide/api-as-lambda-proxy-export-swagger-with-extensions.html) ]

如果有人对此有解决方案,请告诉我。

注意:我遇到了这个 。并想做同样的事情。你能指导我吗?

谢谢, 阿比达利

AWS Api 网关使用 x-amazon-apigateway-* swagger 扩展来配置您的 REST Api。

我不认为已经有一个库可以通过 Swashbuckle 为您管理它,但是 Swashbuckle 有一些您可以实现的接口来生成自定义 swagger 扩展,如 Swashbuckle documentation 中所述。例如,如果你想生成 x-amazon-apigateway-集成扩展,你可以通过实现 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 答案如下所示:(使用 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>();
);