Skip to content

Commit 644912d

Browse files
committed
Added array parsing to AddObject with []
1 parent 39e6e7d commit 644912d

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

src/RestSharp/Parameters/ObjectParser.cs

+24-9
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,45 @@ static class ObjectParser {
2323
var type = obj.GetType();
2424
var props = type.GetProperties();
2525

26+
var properties = new List<(string Name, string? Value)>();
27+
2628
foreach (var prop in props.Where(x => IsAllowedProperty(x.Name))) {
2729
var val = prop.GetValue(obj, null);
2830

2931
if (val == null) continue;
3032

31-
yield return prop.PropertyType.IsArray
32-
? GetArray(prop, val)
33-
: GetValue(prop, val);
33+
if (prop.PropertyType.IsArray)
34+
properties.AddRange(GetArray(prop, val));
35+
else
36+
properties.Add(GetValue(prop, val));
3437
}
3538

3639
string? ParseValue(string? format, object? value) => format == null ? value?.ToString() : string.Format($"{{0:{format}}}", value);
3740

38-
(string, string?) GetArray(PropertyInfo propertyInfo, object? value) {
41+
IEnumerable<(string, string?)> GetArray(PropertyInfo propertyInfo, object? value) {
3942
var elementType = propertyInfo.PropertyType.GetElementType();
4043
var array = (Array)value!;
4144

4245
var attribute = propertyInfo.GetCustomAttribute<RequestPropertyAttribute>();
4346
var name = attribute?.Name ?? propertyInfo.Name;
4447

48+
var queryType = attribute?.ArrayQueryType ?? RequestArrayQueryType.CommaSeparated;
49+
4550
if (array.Length > 0 && elementType != null) {
4651
// convert the array to an array of strings
4752
var values = array
4853
.Cast<object>()
4954
.Select(item => ParseValue(attribute?.Format, item));
50-
return (name, string.Join(",", values));
55+
56+
return queryType switch {
57+
RequestArrayQueryType.CommaSeparated => new (string, string?)[] { (name, string.Join(",", values)) },
58+
RequestArrayQueryType.ArrayParameters => values.Select(x => ($"{name}[]", x)),
59+
_ => throw new ArgumentOutOfRangeException()
60+
};
61+
5162
}
5263

53-
return (name, null);
64+
return new (string, string?)[] { (name, null) };
5465
}
5566

5667
(string, string?) GetValue(PropertyInfo propertyInfo, object? value) {
@@ -62,12 +73,16 @@ static class ObjectParser {
6273

6374
bool IsAllowedProperty(string propertyName)
6475
=> includedProperties.Length == 0 || includedProperties.Length > 0 && includedProperties.Contains(propertyName);
76+
77+
return properties;
6578
}
6679
}
6780

6881
[AttributeUsage(AttributeTargets.Property)]
6982
public class RequestPropertyAttribute : Attribute {
70-
public string? Name { get; set; }
71-
72-
public string? Format { get; set; }
83+
public string? Name { get; set; }
84+
public string? Format { get; set; }
85+
public RequestArrayQueryType ArrayQueryType { get; set; } = RequestArrayQueryType.CommaSeparated;
7386
}
87+
88+
public enum RequestArrayQueryType { CommaSeparated, ArrayParameters }

test/RestSharp.Tests/ObjectParserTests.cs

+19-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class ObjectParserTests {
77
public void ShouldUseRequestProperty() {
88
var now = DateTime.Now;
99
var dates = new[] { now, now.AddDays(1), now.AddDays(2) };
10+
1011
var request = new TestObject {
1112
SomeData = "test",
1213
SomeDate = now,
@@ -24,17 +25,33 @@ public void ShouldUseRequestProperty() {
2425
parsed["dates"].Should().Be(string.Join(",", dates.Select(x => x.ToString("d"))));
2526
}
2627

28+
[Fact]
29+
public void ShouldProduceMultipleParametersForArray() {
30+
var request = new AnotherTestObject {
31+
SomeIds = new[] { 1, 2, 3 }
32+
};
33+
var expected = request.SomeIds.Select(x => ("ids[]", x.ToString()));
34+
var parsed = request.GetProperties();
35+
36+
parsed.Should().BeEquivalentTo(expected);
37+
}
38+
39+
class AnotherTestObject {
40+
[RequestProperty(Name = "ids", ArrayQueryType = RequestArrayQueryType.ArrayParameters)]
41+
public int[] SomeIds { get; set; }
42+
}
43+
2744
class TestObject {
2845
[RequestProperty(Name = "some_data")]
2946
public string SomeData { get; set; }
3047

3148
[RequestProperty(Format = "d")]
3249
public DateTime SomeDate { get; set; }
33-
50+
3451
[RequestProperty(Name = "dates", Format = "d")]
3552
public DateTime[] DatesArray { get; set; }
3653

37-
public int Plain { get; set; }
54+
public int Plain { get; set; }
3855
public DateTime[] PlainArray { get; set; }
3956
}
4057
}

0 commit comments

Comments
 (0)