• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lucaslorentz / durabletask-extensions / 5835751495

pending completion
5835751495

push

github

lucaslorentz
Add husky and apply some code fixes

2502 of 2502 new or added lines in 91 files covered. (100.0%)

2286 of 2792 relevant lines covered (81.88%)

143.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.21
/src/LLL.DurableTask.Api/Extensions/HttpContextExtensions.cs
1
using System;
2
using System.Collections;
3
using System.Collections.Generic;
4
using System.IO;
5
using System.Linq;
6
using System.Reflection;
7
using System.Text;
8
using System.Threading.Tasks;
9
using Microsoft.AspNetCore.Http;
10
using Newtonsoft.Json;
11
using Newtonsoft.Json.Converters;
12
using Newtonsoft.Json.Serialization;
13

14
namespace LLL.DurableTask.Api.Extensions;
15

16
static class HttpContextExtensions
17
{
18
    private static readonly JsonSerializerSettings _serializerSettings = new()
1✔
19
    {
1✔
20
        DateTimeZoneHandling = DateTimeZoneHandling.Utc,
1✔
21
        ContractResolver = new DefaultContractResolver
1✔
22
        {
1✔
23
            NamingStrategy = new CamelCaseNamingStrategy()
1✔
24
        },
1✔
25
        Converters = { new StringEnumConverter() }
1✔
26
    };
1✔
27

28
    public static T ParseQuery<T>(this HttpContext context)
29
        where T : new()
30
    {
31
        var obj = new T();
2✔
32
        foreach (var property in typeof(T).GetProperties())
56✔
33
        {
34
            if (context.Request.Query.TryGetValue(property.Name, out var values))
26✔
35
            {
36
                if (property.PropertyType.IsGenericType && (
3✔
37
                    property.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable<>)
3✔
38
                    || property.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>)))
3✔
39
                {
40
                    var elementType = property.PropertyType.GenericTypeArguments[0];
×
41
                    var array = Array.CreateInstance(elementType, values.Count);
×
42
                    for (var i = 0; i < values.Count; i++)
×
43
                    {
44
                        var convertedValue = ConvertQueryValue(elementType, values[i]);
×
45
                        array.SetValue(convertedValue, i);
×
46
                    }
47
                    SetProperty(property, obj, array);
×
48
                }
49
                else
50
                {
51
                    var firstValue = values.First();
3✔
52
                    var convertedValue = ConvertQueryValue(property.PropertyType, firstValue);
3✔
53
                    SetProperty(property, obj, convertedValue);
3✔
54
                }
55
            }
56
        }
57
        return obj;
2✔
58
    }
59

60
    static void SetProperty(PropertyInfo property, object target, object value)
61
    {
62
        if (property.CanWrite)
3✔
63
        {
64
            property.SetValue(target, value);
3✔
65
            return;
3✔
66
        }
67

68
        var propertyValue = property.GetValue(target);
×
69
        if (propertyValue is IDictionary targetDict && value is IDictionary dictValue)
×
70
        {
71
            foreach (var key in dictValue.Keys)
×
72
                targetDict.Add(key, dictValue[key]);
×
73
            return;
×
74
        }
75

76
        throw new Exception($"Property {property.Name} cannot be written");
×
77
    }
78

79
    static object ConvertQueryValue(Type type, string value)
80
    {
81
        type = Nullable.GetUnderlyingType(type) ?? type;
3✔
82

83
        if (type.IsEnum)
3✔
84
        {
85
            return Enum.Parse(type, value);
×
86
        }
87
        else
88
        {
89
            var typeCode = Type.GetTypeCode(type);
3✔
90
            switch (typeCode)
91
            {
92
                case TypeCode.Object:
93
                    return JsonConvert.DeserializeObject(value, type);
×
94
                default:
95
                    return Convert.ChangeType(value, type);
3✔
96
            }
97

98
        }
99
    }
100

101
    public static async Task<T> ParseBody<T>(this HttpContext context)
102
    {
103
        using var reader = new StreamReader(context.Request.Body, Encoding.UTF8);
5✔
104
        var json = await reader.ReadToEndAsync();
5✔
105
        return JsonConvert.DeserializeObject<T>(json, _serializerSettings);
5✔
106
    }
107

108
    public static async Task RespondJson(
109
        this HttpContext context,
110
        object data,
111
        int statusCode = 200)
112
    {
113
        var json = JsonConvert.SerializeObject(data, _serializerSettings);
11✔
114
        context.Response.StatusCode = statusCode;
11✔
115
        context.Response.ContentType = "application/json; charset=utf-8";
11✔
116
        await context.Response.WriteAsync(json);
11✔
117
    }
118
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc