Skip to content

Commit d7840d5

Browse files
Fixes #1957 (#1964)
1 parent ebf23ed commit d7840d5

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/RestSharp/Parameters/ObjectParser.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
namespace RestSharp;
1919

2020
static class ObjectParser {
21-
public static IEnumerable<(string Name, string? Value)> GetProperties(this object obj, params string[] includedProperties) {
21+
public static IEnumerable<ParsedParameter> GetProperties(this object obj, params string[] includedProperties) {
2222
// automatically create parameters from object props
2323
var type = obj.GetType();
2424
var props = type.GetProperties();
2525

26-
var properties = new List<(string Name, string? Value)>();
26+
var properties = new List<ParsedParameter>();
2727

2828
foreach (var prop in props.Where(x => IsAllowedProperty(x.Name))) {
2929
var val = prop.GetValue(obj, null);
@@ -38,14 +38,14 @@ static class ObjectParser {
3838

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

41-
IEnumerable<(string, string?)> GetArray(PropertyInfo propertyInfo, object? value) {
41+
IEnumerable<ParsedParameter> GetArray(PropertyInfo propertyInfo, object? value) {
4242
var elementType = propertyInfo.PropertyType.GetElementType();
4343
var array = (Array)value!;
4444

4545
var attribute = propertyInfo.GetCustomAttribute<RequestPropertyAttribute>();
46-
var name = attribute?.Name ?? propertyInfo.Name;
47-
46+
var name = attribute?.Name ?? propertyInfo.Name;
4847
var queryType = attribute?.ArrayQueryType ?? RequestArrayQueryType.CommaSeparated;
48+
var encode = attribute?.Encode ?? true;
4949

5050
if (array.Length > 0 && elementType != null) {
5151
// convert the array to an array of strings
@@ -54,21 +54,20 @@ static class ObjectParser {
5454
.Select(item => ParseValue(attribute?.Format, item));
5555

5656
return queryType switch {
57-
RequestArrayQueryType.CommaSeparated => new (string, string?)[] { (name, string.Join(",", values)) },
58-
RequestArrayQueryType.ArrayParameters => values.Select(x => ($"{name}[]", x)),
57+
RequestArrayQueryType.CommaSeparated => new[] { new ParsedParameter(name, string.Join(",", values), encode) },
58+
RequestArrayQueryType.ArrayParameters => values.Select(x => new ParsedParameter($"{name}[]", x, encode)),
5959
_ => throw new ArgumentOutOfRangeException()
6060
};
61-
6261
}
6362

64-
return new (string, string?)[] { (name, null) };
63+
return new ParsedParameter[] { new(name, null, encode) };
6564
}
6665

67-
(string, string?) GetValue(PropertyInfo propertyInfo, object? value) {
66+
ParsedParameter GetValue(PropertyInfo propertyInfo, object? value) {
6867
var attribute = propertyInfo.GetCustomAttribute<RequestPropertyAttribute>();
6968
var name = attribute?.Name ?? propertyInfo.Name;
7069
var val = ParseValue(attribute?.Format, value);
71-
return (name, val);
70+
return new ParsedParameter(name, val, attribute?.Encode ?? true);
7271
}
7372

7473
bool IsAllowedProperty(string propertyName)
@@ -78,11 +77,14 @@ bool IsAllowedProperty(string propertyName)
7877
}
7978
}
8079

80+
record ParsedParameter(string Name, string? Value, bool Encode);
81+
8182
[AttributeUsage(AttributeTargets.Property)]
8283
public class RequestPropertyAttribute : Attribute {
8384
public string? Name { get; set; }
8485
public string? Format { get; set; }
8586
public RequestArrayQueryType ArrayQueryType { get; set; } = RequestArrayQueryType.CommaSeparated;
87+
public bool Encode { get; set; } = true;
8688
}
8789

8890
public enum RequestArrayQueryType { CommaSeparated, ArrayParameters }

src/RestSharp/Request/RestRequestExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,8 @@ public static RestRequest AddXmlBody<T>(this RestRequest request, T obj, string
439439
public static RestRequest AddObject<T>(this RestRequest request, T obj, params string[] includedProperties) where T : class {
440440
var props = obj.GetProperties(includedProperties);
441441

442-
foreach (var (name, value) in props) {
443-
request.AddParameter(name, value);
442+
foreach (var prop in props) {
443+
request.AddParameter(prop.Name, prop.Value, prop.Encode);
444444
}
445445

446446
return request;

src/RestSharp/RestClientExtensions.Json.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ public static partial class RestClientExtensions {
4747
object parameters,
4848
CancellationToken cancellationToken = default
4949
) {
50-
var props = parameters.GetProperties();
50+
var props = parameters.GetProperties();
5151
var request = new RestRequest(resource);
5252

53-
foreach (var (name, value) in props) {
54-
Parameter parameter = resource.Contains($"{name}") ? new UrlSegmentParameter(name, value!) : new QueryParameter(name, value);
53+
foreach (var prop in props) {
54+
Parameter parameter = resource.Contains($"{prop.Name}")
55+
? new UrlSegmentParameter(prop.Name, prop.Value!, prop.Encode)
56+
: new QueryParameter(prop.Name, prop.Value, prop.Encode);
5557
request.AddParameter(parameter);
5658
}
5759

@@ -141,4 +143,4 @@ public static async Task<HttpStatusCode> PutJsonAsync<TRequest>(
141143
var response = await client.PutAsync(restRequest, cancellationToken).ConfigureAwait(false);
142144
return response.StatusCode;
143145
}
144-
}
146+
}

0 commit comments

Comments
 (0)