< Summary - AsyncResponse (Release / net8.0+net10.0 / unit+integration)

Information
Class: AsyncResponse.Transports.AzureServiceBus.AzureServiceBusCorrelationIdExtractor
Assembly: AsyncResponse.Transports.AzureServiceBus
File(s): /home/runner/work/AsyncResponse/AsyncResponse/src/Transports/AsyncResponse.Transports.AzureServiceBus/AzureServiceBusCorrelationIdExtractor.cs
Line coverage
98%
Covered lines: 63
Uncovered lines: 1
Coverable lines: 64
Total lines: 122
Line coverage: 98.4%
Branch coverage
98%
Covered branches: 57
Total branches: 58
Branch coverage: 98.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Extract(...)100%2222100%
TryConvertProperty(...)87.5%8888.89%
TryReadPath(...)100%1212100%
TryGetProperty(...)100%66100%
UnwrapJsonString(...)100%1010100%

File(s)

/home/runner/work/AsyncResponse/AsyncResponse/src/Transports/AsyncResponse.Transports.AzureServiceBus/AzureServiceBusCorrelationIdExtractor.cs

#LineLine coverage
 1using System.Text;
 2using System.Text.Json;
 3using System.Text.Json.Nodes;
 4
 5namespace AsyncResponse.Transports.AzureServiceBus;
 6
 7internal static class AzureServiceBusCorrelationIdExtractor
 8{
 9    /// <summary>Extracts the correlation id from the supplied Service Bus delivery.</summary>
 10    public static string? Extract(
 11        AzureServiceBusTransportDelivery delivery,
 12        string messageJson,
 13        AzureServiceBusAsyncResponseOptions options)
 14    {
 315        if (!string.IsNullOrWhiteSpace(delivery.CorrelationId))
 316            return delivery.CorrelationId;
 17
 318        if (!string.IsNullOrWhiteSpace(options.CorrelationIdProperty)
 319            && delivery.ApplicationProperties.TryGetValue(options.CorrelationIdProperty, out var property)
 320            && TryConvertProperty(property) is { } propertyValue
 321            && !string.IsNullOrWhiteSpace(propertyValue))
 22        {
 323            return propertyValue;
 24        }
 25
 326        var jsonPaths = options.CorrelationIdJsonPaths;
 327        if (jsonPaths is null || jsonPaths.Length == 0 || string.IsNullOrWhiteSpace(messageJson))
 328            return null;
 29
 30        JsonNode? root;
 31        try
 32        {
 333            root = JsonNode.Parse(messageJson);
 334        }
 335        catch (JsonException)
 36        {
 337            return null;
 38        }
 39
 340        if (root is null)
 341            return null;
 42
 343        foreach (var path in jsonPaths)
 44        {
 345            var value = TryReadPath(root, path);
 346            if (!string.IsNullOrWhiteSpace(value))
 347                return value;
 48        }
 49
 250        return null;
 251    }
 52
 53    internal static string? TryConvertProperty(object? property)
 254        => property switch
 255        {
 056            null => null,
 257            string s => s,
 258            byte[] bytes => Encoding.UTF8.GetString(bytes),
 259            ReadOnlyMemory<byte> memory => Encoding.UTF8.GetString(memory.Span),
 260            BinaryData data => data.ToString(),
 261            _ => property.ToString()
 262        };
 63
 64    private static string? TryReadPath(JsonNode root, string path)
 65    {
 366        if (string.IsNullOrWhiteSpace(path))
 367            return null;
 68
 369        var current = root;
 370        foreach (var segment in path.Split('.', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
 71        {
 372            current = UnwrapJsonString(current);
 373            if (current is not JsonObject obj)
 374                return null;
 75
 376            current = TryGetProperty(obj, segment);
 377            if (current is null)
 378                return null;
 79        }
 80
 381        current = UnwrapJsonString(current);
 382        return current switch
 383        {
 384            JsonValue value when value.TryGetValue<string>(out var s) => s,
 285            JsonValue value => value.ToString(),
 386            _ => null
 387        };
 88    }
 89
 90    private static JsonNode? TryGetProperty(JsonObject obj, string name)
 91    {
 392        if (obj.TryGetPropertyValue(name, out var exact))
 393            return exact;
 94
 295        foreach (var property in obj)
 96        {
 297            if (string.Equals(property.Key, name, StringComparison.OrdinalIgnoreCase))
 298                return property.Value;
 99        }
 100
 2101        return null;
 3102    }
 103
 104    private static JsonNode? UnwrapJsonString(JsonNode? node)
 105    {
 3106        if (node is not JsonValue value || !value.TryGetValue<string>(out var text))
 3107            return node;
 108
 3109        var trimmed = text.AsSpan().TrimStart();
 3110        if (trimmed.Length == 0 || (trimmed[0] != '{' && trimmed[0] != '['))
 3111            return node;
 112
 113        try
 114        {
 2115            return JsonNode.Parse(text);
 116        }
 2117        catch (JsonException)
 118        {
 2119            return node;
 120        }
 3121    }
 122}