4
votes

I want to generate the AWS API Gateway swagger (http://docs.aws.amazon.com/apigateway/latest/developerguide/api-as-lambda-proxy-export-swagger-with-extensions.html) from the ASP.NET Core web API using Swashbuckle.AspNetCore.

Please let me know if someone have solution for this.

NOTE: I came across this How to generate Options(CORS) with Swagger. and want to do the same thing. Could you please guide me on this?

Thanks, Abidali

2

2 Answers

4
votes

AWS Api gateway uses x-amazon-apigateway-* extensions in swagger to configure your REST Api.

I don't think there is already a library that will manage it for you via Swashbuckle but Swashbuckle has some interfaces you can implement to generate custom swagger extensions as explained in Swashbuckle documentation. For example, if you want to generate the x-amazon-apigateway-integration extention you can do it by implementing Swashbuckle.AspNetCore.SwaggerGen.IOperationFilter interface:

    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"
            });
        }
    }

And configure it during swagger generator setup:

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

A more up to date version of asidis answer would look like: (using dotnet 3.0.0 and 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"),
        });

    }
}

In Startup.cs

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