« API Gateway » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications |
|||
(Une version intermédiaire par le même utilisateur non affichée) | |||
Ligne 4 : | Ligne 4 : | ||
= Extend the swagger = | = Extend the swagger = | ||
* [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 |
Dernière version du 26 juin 2024 à 11:34
Links
Extend the swagger
- OpenAPI Extensions allows to add custom properties to 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" } |