« 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... »)
 
 
(4 versions intermédiaires par le même utilisateur non affichées)
Ligne 4 : Ligne 4 :


= Extend the swagger =
= Extend the swagger =
== x-amazon-apigateway-integration ==
* [https://swagger.io/docs/specification/openapi-extensions OpenAPI Extensions] allows to add custom properties to the swagger.
<filebox fn='Program.cs'>
<filebox fn='Program.cs'>
// Assembly Swashbuckle.AspNetCore.SwaggerGen
// Assembly Swashbuckle.AspNetCore.SwaggerGen
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 fn='swagger.json'>
"x-amazon-apigateway-integration": {
  "type": "${type}",
  "requestParameters": {
    "integration.request.path.id": "method.request.path.id"
  },
  "uri": "${uri}/MyResource/{id}",
  "httpMethod": "GET",
  "connectionId": "${vpc_link_id}",
  "connectionType": "${connection_type}",
  "passthroughBehavior": "when_no_match"
}
}
</filebox>
</filebox>

Dernière version du 26 juin 2024 à 11:34

Links

Extend the swagger

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")
        };
    }
}
swagger.json
"x-amazon-apigateway-integration": {
  "type": "${type}",
  "requestParameters": {
    "integration.request.path.id": "method.request.path.id"
  },
  "uri": "${uri}/MyResource/{id}",
  "httpMethod": "GET",
  "connectionId": "${vpc_link_id}",
  "connectionType": "${connection_type}",
  "passthroughBehavior": "when_no_match"
}