Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS API Gateway Swagger using swashbuckle

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

like image 438
Abidali Avatar asked Mar 06 '23 21:03

Abidali


2 Answers

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>();
});
like image 50
asidis Avatar answered Mar 15 '23 18:03

asidis


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>();
);
like image 38
Hudvoy Avatar answered Mar 15 '23 17:03

Hudvoy