18
18
namespace RestSharp ;
19
19
20
20
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 ) {
22
22
// automatically create parameters from object props
23
23
var type = obj . GetType ( ) ;
24
24
var props = type . GetProperties ( ) ;
25
25
26
- var properties = new List < ( string Name , string ? Value ) > ( ) ;
26
+ var properties = new List < ParsedParameter > ( ) ;
27
27
28
28
foreach ( var prop in props . Where ( x => IsAllowedProperty ( x . Name ) ) ) {
29
29
var val = prop . GetValue ( obj , null ) ;
@@ -38,14 +38,14 @@ static class ObjectParser {
38
38
39
39
string ? ParseValue ( string ? format , object ? value ) => format == null ? value ? . ToString ( ) : string . Format ( $ "{{0:{ format } }}", value ) ;
40
40
41
- IEnumerable < ( string , string ? ) > GetArray ( PropertyInfo propertyInfo , object ? value ) {
41
+ IEnumerable < ParsedParameter > GetArray ( PropertyInfo propertyInfo , object ? value ) {
42
42
var elementType = propertyInfo . PropertyType . GetElementType ( ) ;
43
43
var array = ( Array ) value ! ;
44
44
45
45
var attribute = propertyInfo . GetCustomAttribute < RequestPropertyAttribute > ( ) ;
46
- var name = attribute ? . Name ?? propertyInfo . Name ;
47
-
46
+ var name = attribute ? . Name ?? propertyInfo . Name ;
48
47
var queryType = attribute ? . ArrayQueryType ?? RequestArrayQueryType . CommaSeparated ;
48
+ var encode = attribute ? . Encode ?? true ;
49
49
50
50
if ( array . Length > 0 && elementType != null ) {
51
51
// convert the array to an array of strings
@@ -54,21 +54,20 @@ static class ObjectParser {
54
54
. Select ( item => ParseValue ( attribute ? . Format , item ) ) ;
55
55
56
56
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 ) ) ,
59
59
_ => throw new ArgumentOutOfRangeException ( )
60
60
} ;
61
-
62
61
}
63
62
64
- return new ( string , string ? ) [ ] { ( name , null ) } ;
63
+ return new ParsedParameter [ ] { new ( name , null , encode ) } ;
65
64
}
66
65
67
- ( string , string ? ) GetValue ( PropertyInfo propertyInfo , object ? value ) {
66
+ ParsedParameter GetValue ( PropertyInfo propertyInfo , object ? value ) {
68
67
var attribute = propertyInfo . GetCustomAttribute < RequestPropertyAttribute > ( ) ;
69
68
var name = attribute ? . Name ?? propertyInfo . Name ;
70
69
var val = ParseValue ( attribute ? . Format , value ) ;
71
- return ( name , val ) ;
70
+ return new ParsedParameter ( name , val , attribute ? . Encode ?? true ) ;
72
71
}
73
72
74
73
bool IsAllowedProperty ( string propertyName )
@@ -78,11 +77,14 @@ bool IsAllowedProperty(string propertyName)
78
77
}
79
78
}
80
79
80
+ record ParsedParameter ( string Name , string ? Value , bool Encode ) ;
81
+
81
82
[ AttributeUsage ( AttributeTargets . Property ) ]
82
83
public class RequestPropertyAttribute : Attribute {
83
84
public string ? Name { get ; set ; }
84
85
public string ? Format { get ; set ; }
85
86
public RequestArrayQueryType ArrayQueryType { get ; set ; } = RequestArrayQueryType . CommaSeparated ;
87
+ public bool Encode { get ; set ; } = true ;
86
88
}
87
89
88
90
public enum RequestArrayQueryType { CommaSeparated , ArrayParameters }
0 commit comments