« API Gateway » : différence entre les versions
Apparence
Aucun résumé des modifications |
|||
Ligne 4 : | Ligne 4 : | ||
= Extend the swagger = | = Extend the swagger = | ||
== securityDefinitions == | |||
== x-amazon-apigateway-integration == | == x-amazon-apigateway-integration == | ||
<filebox fn='Program.cs'> | <filebox fn='Program.cs'> |
Version du 26 juin 2024 à 08:45
Links
Extend the swagger
securityDefinitions
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")
};
}
}
|
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"
}
|