Skip to content

Optionally disable encoding for request properties #1964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/RestSharp/Parameters/ObjectParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParsedParameter> 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<ParsedParameter>();

foreach (var prop in props.Where(x => IsAllowedProperty(x.Name))) {
var val = prop.GetValue(obj, null);
Expand All @@ -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<ParsedParameter> GetArray(PropertyInfo propertyInfo, object? value) {
var elementType = propertyInfo.PropertyType.GetElementType();
var array = (Array)value!;

var attribute = propertyInfo.GetCustomAttribute<RequestPropertyAttribute>();
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
Expand All @@ -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<RequestPropertyAttribute>();
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)
Expand All @@ -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 }
4 changes: 2 additions & 2 deletions src/RestSharp/Request/RestRequestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ public static RestRequest AddXmlBody<T>(this RestRequest request, T obj, string
public static RestRequest AddObject<T>(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;
Expand Down
10 changes: 6 additions & 4 deletions src/RestSharp/RestClientExtensions.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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