diff --git a/src/RestSharp/Parameters/ObjectParser.cs b/src/RestSharp/Parameters/ObjectParser.cs index 71e496798..fec2a0a85 100644 --- a/src/RestSharp/Parameters/ObjectParser.cs +++ b/src/RestSharp/Parameters/ObjectParser.cs @@ -18,12 +18,12 @@ namespace RestSharp; static class ObjectParser { - public static IEnumerable<(string Name, string? Value)> GetProperties(this object obj, params string[] includedProperties) { + public static IEnumerable GetProperties(this object obj, params string[] includedProperties) { // automatically create parameters from object props var type = obj.GetType(); var props = type.GetProperties(); - var properties = new List<(string Name, string? Value)>(); + var properties = new List(); foreach (var prop in props.Where(x => IsAllowedProperty(x.Name))) { var val = prop.GetValue(obj, null); @@ -38,14 +38,14 @@ static class ObjectParser { string? ParseValue(string? format, object? value) => format == null ? value?.ToString() : string.Format($"{{0:{format}}}", value); - IEnumerable<(string, string?)> GetArray(PropertyInfo propertyInfo, object? value) { + IEnumerable GetArray(PropertyInfo propertyInfo, object? value) { var elementType = propertyInfo.PropertyType.GetElementType(); var array = (Array)value!; var attribute = propertyInfo.GetCustomAttribute(); - var name = attribute?.Name ?? propertyInfo.Name; - + var name = attribute?.Name ?? propertyInfo.Name; var queryType = attribute?.ArrayQueryType ?? RequestArrayQueryType.CommaSeparated; + var encode = attribute?.Encode ?? true; if (array.Length > 0 && elementType != null) { // convert the array to an array of strings @@ -54,21 +54,20 @@ static class ObjectParser { .Select(item => ParseValue(attribute?.Format, item)); return queryType switch { - RequestArrayQueryType.CommaSeparated => new (string, string?)[] { (name, string.Join(",", values)) }, - RequestArrayQueryType.ArrayParameters => values.Select(x => ($"{name}[]", x)), + RequestArrayQueryType.CommaSeparated => new[] { new ParsedParameter(name, string.Join(",", values), encode) }, + RequestArrayQueryType.ArrayParameters => values.Select(x => new ParsedParameter($"{name}[]", x, encode)), _ => throw new ArgumentOutOfRangeException() }; - } - return new (string, string?)[] { (name, null) }; + return new ParsedParameter[] { new(name, null, encode) }; } - (string, string?) GetValue(PropertyInfo propertyInfo, object? value) { + ParsedParameter GetValue(PropertyInfo propertyInfo, object? value) { var attribute = propertyInfo.GetCustomAttribute(); var name = attribute?.Name ?? propertyInfo.Name; var val = ParseValue(attribute?.Format, value); - return (name, val); + return new ParsedParameter(name, val, attribute?.Encode ?? true); } bool IsAllowedProperty(string propertyName) @@ -78,11 +77,14 @@ bool IsAllowedProperty(string propertyName) } } +record ParsedParameter(string Name, string? Value, bool Encode); + [AttributeUsage(AttributeTargets.Property)] public class RequestPropertyAttribute : Attribute { public string? Name { get; set; } public string? Format { get; set; } public RequestArrayQueryType ArrayQueryType { get; set; } = RequestArrayQueryType.CommaSeparated; + public bool Encode { get; set; } = true; } public enum RequestArrayQueryType { CommaSeparated, ArrayParameters } diff --git a/src/RestSharp/Request/RestRequestExtensions.cs b/src/RestSharp/Request/RestRequestExtensions.cs index 80bfa5c13..ac041a09d 100644 --- a/src/RestSharp/Request/RestRequestExtensions.cs +++ b/src/RestSharp/Request/RestRequestExtensions.cs @@ -438,8 +438,8 @@ public static RestRequest AddXmlBody(this RestRequest request, T obj, string public static RestRequest AddObject(this RestRequest request, T obj, params string[] includedProperties) where T : class { var props = obj.GetProperties(includedProperties); - foreach (var (name, value) in props) { - request.AddParameter(name, value); + foreach (var prop in props) { + request.AddParameter(prop.Name, prop.Value, prop.Encode); } return request; diff --git a/src/RestSharp/RestClientExtensions.Json.cs b/src/RestSharp/RestClientExtensions.Json.cs index 1e4fa28f7..3f5a3cbbd 100644 --- a/src/RestSharp/RestClientExtensions.Json.cs +++ b/src/RestSharp/RestClientExtensions.Json.cs @@ -47,11 +47,13 @@ public static partial class RestClientExtensions { object parameters, CancellationToken cancellationToken = default ) { - var props = parameters.GetProperties(); + var props = parameters.GetProperties(); var request = new RestRequest(resource); - foreach (var (name, value) in props) { - Parameter parameter = resource.Contains($"{name}") ? new UrlSegmentParameter(name, value!) : new QueryParameter(name, value); + foreach (var prop in props) { + Parameter parameter = resource.Contains($"{prop.Name}") + ? new UrlSegmentParameter(prop.Name, prop.Value!, prop.Encode) + : new QueryParameter(prop.Name, prop.Value, prop.Encode); request.AddParameter(parameter); } @@ -141,4 +143,4 @@ public static async Task PutJsonAsync( var response = await client.PutAsync(restRequest, cancellationToken).ConfigureAwait(false); return response.StatusCode; } -} \ No newline at end of file +}