From ce22777f375d845100f95d3356992e2daeb03105 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sat, 20 Sep 2025 09:55:17 +0000 Subject: [PATCH 1/2] refactor: use equality operator for nullable bool checks This PR refactors the authentication check, replacing a null-coalescing default with an explicit boolean comparison for improved readability and intent. - Consider using the equality operators when evaluating `bool?`: The `principal?.Identity?.IsAuthenticated` property was previously unwrapped with `?? false`, potentially conflating `null` and `false` values. By switching to `== true`, the code now explicitly returns true only when the property is true, while treating null or false as false. > This Autofix was generated by AI. Please review the change before merging. --- Shared/Middleware/CorrelationIdMiddleware.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Shared/Middleware/CorrelationIdMiddleware.cs b/Shared/Middleware/CorrelationIdMiddleware.cs index 76507ef..aeb72ac 100644 --- a/Shared/Middleware/CorrelationIdMiddleware.cs +++ b/Shared/Middleware/CorrelationIdMiddleware.cs @@ -90,7 +90,7 @@ public static class ClaimsPrincipalExtensions { public static Boolean IsAuthenticated(this ClaimsPrincipal principal) { - return principal?.Identity?.IsAuthenticated ?? false; + return principal?.Identity?.IsAuthenticated == true; } } From e7fa1fc6aa4d14ad896259fe54307b6aa15c5b43 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sat, 20 Sep 2025 09:55:45 +0000 Subject: [PATCH 2/2] style: format code with dotnet-format This commit fixes the style issues introduced in ce22777 according to the output from dotnet-format. Details: https://github.com/TransactionProcessing/Shared/pull/371 --- Shared/Middleware/CorrelationIdMiddleware.cs | 114 ++++++++++--------- 1 file changed, 60 insertions(+), 54 deletions(-) diff --git a/Shared/Middleware/CorrelationIdMiddleware.cs b/Shared/Middleware/CorrelationIdMiddleware.cs index aeb72ac..687daad 100644 --- a/Shared/Middleware/CorrelationIdMiddleware.cs +++ b/Shared/Middleware/CorrelationIdMiddleware.cs @@ -16,75 +16,76 @@ namespace Shared.Middleware; - public class TenantMiddleware - { - #region Fields +public class TenantMiddleware +{ + #region Fields - private readonly RequestDelegate Next; + private readonly RequestDelegate Next; - #endregion + #endregion - #region Constructors + #region Constructors - public TenantMiddleware(RequestDelegate next) { - this.Next = next; - } + public TenantMiddleware(RequestDelegate next) + { + this.Next = next; + } - public const String KeyNameCorrelationId = "correlationId"; + public const String KeyNameCorrelationId = "correlationId"; - #endregion + #endregion - public async Task InvokeAsync(HttpContext context, TenantContext tenantContext) - { - Stopwatch watch = Stopwatch.StartNew(); + public async Task InvokeAsync(HttpContext context, TenantContext tenantContext) + { + Stopwatch watch = Stopwatch.StartNew(); - // Detect the tenant from the incoming request - TenantIdentifiers tenantIdentifiers = await this.GetIdentifiersFromContext(context); + // Detect the tenant from the incoming request + TenantIdentifiers tenantIdentifiers = await this.GetIdentifiersFromContext(context); - Boolean.TryParse(ConfigurationReader.GetValueOrDefault("AppSettings","LogsPerTenantEnabled", "false"), out Boolean logPerTenantEnabled); + Boolean.TryParse(ConfigurationReader.GetValueOrDefault("AppSettings", "LogsPerTenantEnabled", "false"), out Boolean logPerTenantEnabled); - // Check the headers for a correlationId - context.Request.Headers.TryGetValue(KeyNameCorrelationId, out StringValues correlationIdHeader); - Guid.TryParse(correlationIdHeader, out Guid correlationId); + // Check the headers for a correlationId + context.Request.Headers.TryGetValue(KeyNameCorrelationId, out StringValues correlationIdHeader); + Guid.TryParse(correlationIdHeader, out Guid correlationId); - if (correlationId != Guid.Empty) - { - tenantContext.SetCorrelationId(correlationId); - context.Items[KeyNameCorrelationId] = correlationId.ToString(); // make it accessible to HttpClient handlers - } - - tenantContext.Initialise(tenantIdentifiers, logPerTenantEnabled); + if (correlationId != Guid.Empty) + { + tenantContext.SetCorrelationId(correlationId); + context.Items[KeyNameCorrelationId] = correlationId.ToString(); // make it accessible to HttpClient handlers + } - // Set the current tenant in the TenantContext - TenantContext.CurrentTenant = tenantContext; + tenantContext.Initialise(tenantIdentifiers, logPerTenantEnabled); - String clientIp = context.Connection.RemoteIpAddress?.ToString(); + // Set the current tenant in the TenantContext + TenantContext.CurrentTenant = tenantContext; - //Makes sense to start our correlation audit trace here - String logMessage = $"Receiving from {clientIp} => {context.Request.Method} {context.Request.Host}{context.Request.Path}"; + String clientIp = context.Connection.RemoteIpAddress?.ToString(); - Logger.Logger.LogInformation(logMessage); + //Makes sense to start our correlation audit trace here + String logMessage = $"Receiving from {clientIp} => {context.Request.Method} {context.Request.Host}{context.Request.Path}"; - // Call the next middleware - await this.Next(context); + Logger.Logger.LogInformation(logMessage); - watch.Stop(); - String afterMessage = $"{context.Response.StatusCode} {logMessage} Duration: {watch.ElapsedMilliseconds}ms"; - Logger.Logger.LogInformation(afterMessage); - } + // Call the next middleware + await this.Next(context); - private async Task GetIdentifiersFromContext(HttpContext context) => - context switch - { - _ when context.GetIdentifiersFromToken() is var identifiersFromToken && identifiersFromToken != TenantIdentifiers.Default() => identifiersFromToken, - _ when context.GetIdentifiersFromHeaders() is var identifiersFromHeaders && identifiersFromHeaders != TenantIdentifiers.Default() => identifiersFromHeaders, - _ when context.GetIdentifiersFromRoute() is var identifiersFromHeaders && identifiersFromHeaders != TenantIdentifiers.Default() => identifiersFromHeaders, - //_ when await context.GetIdentifiersFromPayload() is var identifiersFromPayload && identifiersFromPayload != TenantIdentifiers.Default() => - //identifiersFromPayload, - _ => TenantIdentifiers.Default(), - }; + watch.Stop(); + String afterMessage = $"{context.Response.StatusCode} {logMessage} Duration: {watch.ElapsedMilliseconds}ms"; + Logger.Logger.LogInformation(afterMessage); } + private async Task GetIdentifiersFromContext(HttpContext context) => + context switch + { + _ when context.GetIdentifiersFromToken() is var identifiersFromToken && identifiersFromToken != TenantIdentifiers.Default() => identifiersFromToken, + _ when context.GetIdentifiersFromHeaders() is var identifiersFromHeaders && identifiersFromHeaders != TenantIdentifiers.Default() => identifiersFromHeaders, + _ when context.GetIdentifiersFromRoute() is var identifiersFromHeaders && identifiersFromHeaders != TenantIdentifiers.Default() => identifiersFromHeaders, + //_ when await context.GetIdentifiersFromPayload() is var identifiersFromPayload && identifiersFromPayload != TenantIdentifiers.Default() => + //identifiersFromPayload, + _ => TenantIdentifiers.Default(), + }; +} + public static class ClaimsPrincipalExtensions { @@ -94,9 +95,12 @@ public static Boolean IsAuthenticated(this ClaimsPrincipal principal) } } -public static class HttpContextExtensionMethods { - public static TenantIdentifiers GetIdentifiersFromToken(this HttpContext context) { - if (!context.User.IsAuthenticated()) { +public static class HttpContextExtensionMethods +{ + public static TenantIdentifiers GetIdentifiersFromToken(this HttpContext context) + { + if (!context.User.IsAuthenticated()) + { return TenantIdentifiers.Default(); } @@ -109,7 +113,8 @@ public static TenantIdentifiers GetIdentifiersFromToken(this HttpContext context return estateId == Guid.Empty ? TenantIdentifiers.Default() : new TenantIdentifiers(estateId, merchantId); } - public static TenantIdentifiers GetIdentifiersFromHeaders(this HttpContext context) { + public static TenantIdentifiers GetIdentifiersFromHeaders(this HttpContext context) + { // Get the org Id context.Request.Headers.TryGetValue("estateId", out StringValues estateIdHeader); Guid.TryParse(estateIdHeader, out Guid estateId); @@ -121,7 +126,8 @@ public static TenantIdentifiers GetIdentifiersFromHeaders(this HttpContext conte return estateId == Guid.Empty ? TenantIdentifiers.Default() : new TenantIdentifiers(estateId, merchantId); } - public static TenantIdentifiers GetIdentifiersFromRoute(this HttpContext context) { + public static TenantIdentifiers GetIdentifiersFromRoute(this HttpContext context) + { // Get the org Id context.Request.RouteValues.TryGetValue("estateId", out object estateIdRouteValue);