API Gateway
De Banane Atomic
Aller à la navigationAller à la recherche
Links
Extend the swagger
x-amazon-apigateway-integration
Program.cs |
// Assembly Swashbuckle.AspNetCore.SwaggerGen services.AddSwaggerGen(c => { c.DocumentFilter<ApiGatewayDocumentFilter>(); }; |
ApiGatewayDocumentFilter.cs |
public class ApiGatewayDocumentFilter : IDocumentFilter { private static OpenApiObject AddGatewayExtension(KeyValuePair<string, OpenApiPathItem> path, OperationType operationType) { var pathParameters = path.Value.Operations.First().Value.Parameters.Where(p => p.In.HasValue && p.In.Value.ToString().Equals("Path")).ToList(); var requestParam = new OpenApiObject(); foreach (var parameter in pathParameters) { requestParam.Add($"integration.request.path.{parameter.Name}", new OpenApiString($"method.request.path.{parameter.Name}")); } return new OpenApiObject { ["type"] = new OpenApiString("${type}"), ["requestParameters"] = requestParam, ["uri"] = new OpenApiString("${uri}" + path.Key), ["httpMethod"] = new OpenApiString(operationType.ToString().ToUpper()), ["connectionId"] = new OpenApiString("${vpc_link_id}"), ["connectionType"] = new OpenApiString("${connection_type}"), ["passthroughBehavior"] = new OpenApiString("when_no_match") }; } private static void BuildCorsOperation(OpenApiOperation operation, KeyValuePair<string, OpenApiPathItem> path, OperationType operationType) { var response = new OpenApiResponse { Description = "Success", }; var corsOperation = new OpenApiOperation { Summary = operation.Summary, Parameters = operation.Parameters.ToList(), Tags = operation.Tags.OrderBy(x => x.Name).ToList(), Responses = new OpenApiResponses { { "204", response } }, }; corsOperation.Extensions.Add("x-amazon-apigateway-integration", AddGatewayExtension(path, operationType)); path.Value.Operations.Add(operationType, corsOperation); } public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) { foreach (var path in swaggerDoc.Paths) { foreach (var operation in path.Value.Operations) { operation.Value.Extensions.Add("x-amazon-apigateway-integration", AddGatewayExtension(path, operation.Key)); } BuildCorsOperation(path.Value.Operations.FirstOrDefault().Value, path, OperationType.Options); } } } |