« API Gateway » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
(Page créée avec « Category:AWS = Links = * [https://aws.amazon.com/api-gateway Amazon API Gateway] = Extend the swagger = == x-amazon-apigateway-integration == <filebox fn='Program.cs'> // Assembly Swashbuckle.AspNetCore.SwaggerGen services.AddSwaggerGen(c => { c.DocumentFilter<ApiGatewayDocumentFilter>(); }; </filebox> <filebox fn='ApiGatewayDocumentFilter.cs'> public class ApiGatewayDocumentFilter : IDocumentFilter { private static OpenApiObject AddGatewayExtension... »)
 
Ligne 16 : Ligne 16 :
public class ApiGatewayDocumentFilter : IDocumentFilter
public class ApiGatewayDocumentFilter : IDocumentFilter
{
{
     private static OpenApiObject AddGatewayExtension(KeyValuePair<string, OpenApiPathItem> path, OperationType operationType)
     public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
     {
     {
        var pathParameters = path.Value.Operations.First().Value.Parameters.Where(p => p.In.HasValue && p.In.Value.ToString().Equals("Path")).ToList();
         foreach (var path in swaggerDoc.Paths)
        var requestParam = new OpenApiObject();
         foreach (var parameter in pathParameters)
         {
         {
             requestParam.Add($"integration.request.path.{parameter.Name}", new OpenApiString($"method.request.path.{parameter.Name}"));
             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);
         }
         }
        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")
        };
     }
     }


Ligne 56 : Ligne 47 :
         corsOperation.Extensions.Add("x-amazon-apigateway-integration", AddGatewayExtension(path, operationType));
         corsOperation.Extensions.Add("x-amazon-apigateway-integration", AddGatewayExtension(path, operationType));
         path.Value.Operations.Add(operationType, corsOperation);
         path.Value.Operations.Add(operationType, corsOperation);
     }
     }


     public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
     private static OpenApiObject AddGatewayExtension(KeyValuePair<string, OpenApiPathItem> path, OperationType operationType)
     {
     {
         foreach (var path in swaggerDoc.Paths)
        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)
         {
         {
             foreach (var operation in path.Value.Operations)
             requestParam.Add($"integration.request.path.{parameter.Name}", new OpenApiString($"method.request.path.{parameter.Name}"));
            {
                operation.Value.Extensions.Add("x-amazon-apigateway-integration", AddGatewayExtension(path, operation.Key));
            }
            BuildCorsOperation(path.Value.Operations.FirstOrDefault().Value, path, OperationType.Options);
         }
         }
        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")
        };
     }
     }
}
}
</filebox>
</filebox>

Version du 26 juin 2024 à 08:40

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

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

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