From 879234fbf77e1ab615eb851d8a331b9f6e494a47 Mon Sep 17 00:00:00 2001 From: Erwin Kramer Date: Sun, 29 Dec 2024 22:08:32 +0100 Subject: [PATCH] simplify dictionary handling --- .../Defaults/Transformer.ComponentHeaders.cs | 6 ++--- .../Transformer.ComponentResponses.cs | 26 +++++++++---------- .../Defaults/Transformer.ComponentSchemas.cs | 12 ++++----- .../Defaults/Transformer.SecurityScheme.cs | 2 +- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/BankApi.Core/Defaults/Transformer.ComponentHeaders.cs b/BankApi.Core/Defaults/Transformer.ComponentHeaders.cs index eaee1b5..d31876f 100644 --- a/BankApi.Core/Defaults/Transformer.ComponentHeaders.cs +++ b/BankApi.Core/Defaults/Transformer.ComponentHeaders.cs @@ -6,14 +6,14 @@ class TransformerComponentHeaders() : IOpenApiDocumentTransformer public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken) { document.Components ??= new OpenApiComponents(); - document.Components.Headers.Add("GenericStringHeader", OpenApiFactory.CreateHeaderString()); + document.Components.Headers["GenericStringHeader"] = OpenApiFactory.CreateHeaderString(); //because of a bug in the spectral OWASP linter, //using the Access-Control-Allow-Origin header instead of `GenericStringHeader` //please see https://github.com/stoplightio/spectral-owasp-ruleset/issues/71 - document.Components.Headers.Add("Access-Control-Allow-Origin", OpenApiFactory.CreateHeaderString()); + document.Components.Headers["Access-Control-Allow-Origin"] = OpenApiFactory.CreateHeaderString(); - document.Components.Headers.Add("X-RateLimit-Limit", OpenApiFactory.CreateHeaderInt($"The maximum number of requests you're permitted to make in a window of {GlobalConfiguration.ApiSettings!.FixedWindowRateLimit.Window.Minutes} minutes.")); + document.Components.Headers["X-RateLimit-Limit"] = OpenApiFactory.CreateHeaderInt($"The maximum number of requests you're permitted to make in a window of {GlobalConfiguration.ApiSettings!.FixedWindowRateLimit.Window.Minutes} minutes."); return Task.CompletedTask; } diff --git a/BankApi.Core/Defaults/Transformer.ComponentResponses.cs b/BankApi.Core/Defaults/Transformer.ComponentResponses.cs index 68950b1..6a81914 100644 --- a/BankApi.Core/Defaults/Transformer.ComponentResponses.cs +++ b/BankApi.Core/Defaults/Transformer.ComponentResponses.cs @@ -6,34 +6,34 @@ class TransformerComponentResponses() : IOpenApiDocumentTransformer public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken) { document.Components ??= new OpenApiComponents(); - document.Components.Responses.Add("500", new OpenApiResponse + document.Components.Responses["500"] = new OpenApiResponse { Description = "Internal server error.", Content = new Dictionary { { "application/problem+json", new OpenApiMediaType { Schema = OpenApiFactory.CreateSchemaRef("Problem") } } } - }); + }; - document.Components.Responses.Add("400", new OpenApiResponse + document.Components.Responses["400"] = new OpenApiResponse { Description = "Bad request.", Content = new Dictionary { { "application/problem+json", new OpenApiMediaType { Schema = OpenApiFactory.CreateSchemaRef("Problem") } } } - }); + }; - document.Components.Responses.Add("422", new OpenApiResponse + document.Components.Responses["422"] = new OpenApiResponse { Description = "Unprocessable Entity.", Content = new Dictionary { { "application/problem+json", new OpenApiMediaType { Schema = OpenApiFactory.CreateSchemaRef("Problem") } } } - }); + }; - document.Components.Responses.Add("401", new OpenApiResponse + document.Components.Responses["401"] = new OpenApiResponse { Description = "Unauthorized request.", Content = new Dictionary @@ -44,9 +44,9 @@ public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerC { { "WWW-Authenticate", OpenApiFactory.CreateHeaderRef("GenericStringHeader") } } - }); + }; - document.Components.Responses.Add("429", new OpenApiResponse + document.Components.Responses["429"] = new OpenApiResponse { Description = "Too many requests.", Content = new Dictionary @@ -57,7 +57,7 @@ public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerC { { "Retry-After", OpenApiFactory.CreateHeaderInt("The number of seconds to wait before retrying the request.") } } - }); + }; AddHeadersToResponses(document.Components); @@ -68,12 +68,12 @@ private void AddHeadersToResponses(OpenApiComponents components) { foreach (var response in components.Responses) { - response.Value.Headers.Add("Access-Control-Allow-Origin", OpenApiFactory.CreateHeaderRef("Access-Control-Allow-Origin")); - response.Value.Headers.Add("Access-Control-Expose-Headers", OpenApiFactory.CreateHeaderRef("GenericStringHeader")); + response.Value.Headers["Access-Control-Allow-Origin"] = OpenApiFactory.CreateHeaderRef("Access-Control-Allow-Origin"); + response.Value.Headers["Access-Control-Expose-Headers"] = OpenApiFactory.CreateHeaderRef("GenericStringHeader"); if (response.Key[0] is '2' or '4') { - response.Value.Headers.Add("X-RateLimit-Limit", OpenApiFactory.CreateHeaderRef("X-RateLimit-Limit")); + response.Value.Headers["X-RateLimit-Limit"] = OpenApiFactory.CreateHeaderRef("X-RateLimit-Limit"); } } } diff --git a/BankApi.Core/Defaults/Transformer.ComponentSchemas.cs b/BankApi.Core/Defaults/Transformer.ComponentSchemas.cs index d2d5f3d..c77f832 100644 --- a/BankApi.Core/Defaults/Transformer.ComponentSchemas.cs +++ b/BankApi.Core/Defaults/Transformer.ComponentSchemas.cs @@ -6,22 +6,22 @@ class TransformerComponentSchemas() : IOpenApiDocumentTransformer public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken) { document.Components ??= new OpenApiComponents(); - document.Components.Schemas.Add("GenericString", new OpenApiSchema + document.Components.Schemas["GenericString"] = new OpenApiSchema { Type = "string", Pattern = GlobalConfiguration.ApiSettings!.GenericBoundaries.Regex, MaxLength = GlobalConfiguration.ApiSettings!.GenericBoundaries.Maximum - }); + }; - document.Components.Schemas.Add("GenericInt", new OpenApiSchema + document.Components.Schemas["GenericInt"] = new OpenApiSchema { Type = "integer", Format = "int32", Minimum = GlobalConfiguration.ApiSettings!.GenericBoundaries.Minimum, Maximum = GlobalConfiguration.ApiSettings!.GenericBoundaries.Maximum, - }); + }; - document.Components.Schemas.Add("Problem", new OpenApiSchema + document.Components.Schemas["Problem"] = new OpenApiSchema { Type = "object", Properties = new Dictionary @@ -34,7 +34,7 @@ public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerC ["traceId"] = OpenApiFactory.CreateSchemaRef("GenericString"), ["requestId"] = OpenApiFactory.CreateSchemaRef("GenericString") } - }); + }; return Task.CompletedTask; } diff --git a/BankApi.Core/Defaults/Transformer.SecurityScheme.cs b/BankApi.Core/Defaults/Transformer.SecurityScheme.cs index caf9183..7027251 100644 --- a/BankApi.Core/Defaults/Transformer.SecurityScheme.cs +++ b/BankApi.Core/Defaults/Transformer.SecurityScheme.cs @@ -61,7 +61,7 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf foreach (var securityScheme in securitySchemes) { - document.Components.SecuritySchemes.Add(securityScheme.Reference.Id, securityScheme); + document.Components.SecuritySchemes[securityScheme.Reference.Id] = securityScheme; document.SecurityRequirements.Add(new OpenApiSecurityRequirement() { { securityScheme, new List() } }); } }