Skip to content

Commit

Permalink
simplify dictionary handling
Browse files Browse the repository at this point in the history
  • Loading branch information
erwinkramer committed Dec 29, 2024
1 parent 4dc7649 commit 879234f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions BankApi.Core/Defaults/Transformer.ComponentHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
26 changes: 13 additions & 13 deletions BankApi.Core/Defaults/Transformer.ComponentResponses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, OpenApiMediaType>
{
{ "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<string, OpenApiMediaType>
{
{ "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<string, OpenApiMediaType>
{
{ "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<string, OpenApiMediaType>
Expand All @@ -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<string, OpenApiMediaType>
Expand All @@ -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);

Expand All @@ -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");
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions BankApi.Core/Defaults/Transformer.ComponentSchemas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, OpenApiSchema>
Expand All @@ -34,7 +34,7 @@ public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerC
["traceId"] = OpenApiFactory.CreateSchemaRef("GenericString"),
["requestId"] = OpenApiFactory.CreateSchemaRef("GenericString")
}
});
};

return Task.CompletedTask;
}
Expand Down
2 changes: 1 addition & 1 deletion BankApi.Core/Defaults/Transformer.SecurityScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>() } });
}
}
Expand Down

0 comments on commit 879234f

Please sign in to comment.