1818namespace RestSharp ;
1919
2020static 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 ) ]
8283public 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
8890public enum RequestArrayQueryType { CommaSeparated , ArrayParameters }
0 commit comments