Skip to content

Commit e99d12d

Browse files
committed
1 parent 34c4d31 commit e99d12d

File tree

6 files changed

+149
-199
lines changed

6 files changed

+149
-199
lines changed

RestSharp.Tests/RestSharp.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,4 @@
109109
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
110110
</None>
111111
</ItemGroup>
112-
</Project>
112+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace RestSharp.Tests.SampleClasses
2+
{
3+
[Deserializers.DeserializeAs(Name = "Color")]
4+
public class ColorWithValue
5+
{
6+
public string Name { get; set; }
7+
public int Value { get; set; }
8+
}
9+
}

RestSharp.Tests/XmlDeserializerTests.cs

+78-62
Large diffs are not rendered by default.

RestSharp/Authenticators/OAuth/Extensions/StringExtensions.cs

+17-46
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,33 @@ namespace RestSharp.Authenticators.OAuth.Extensions
88
{
99
internal static class StringExtensions
1010
{
11-
public static bool IsNullOrBlank(this string value)
12-
{
13-
return string.IsNullOrEmpty(value) ||
14-
!string.IsNullOrEmpty(value) && value.Trim() == string.Empty;
15-
}
11+
public static bool IsNullOrBlank(this string value) => string.IsNullOrEmpty(value) ||
12+
!string.IsNullOrEmpty(value) &&
13+
value.Trim() == string.Empty;
1614

17-
public static bool EqualsIgnoreCase(this string left, string right)
18-
{
19-
return string.Compare(left, right, StringComparison.OrdinalIgnoreCase) == 0;
20-
}
15+
public static bool EqualsIgnoreCase(this string left, string right) =>
16+
string.Compare(left, right, StringComparison.OrdinalIgnoreCase) == 0;
2117

22-
public static bool EqualsAny(this string input, params string[] args)
23-
{
24-
return args.Aggregate(false, (current, arg) => current | input.Equals(arg));
25-
}
18+
public static bool EqualsAny(this string input, params string[] args) =>
19+
args.Aggregate(false, (current, arg) => current | input.Equals(arg));
2620

27-
public static string FormatWith(this string format, params object[] args)
28-
{
29-
return string.Format(format, args);
30-
}
21+
public static string FormatWith(this string format, params object[] args) =>
22+
string.Format(format, args);
3123

32-
public static string FormatWithInvariantCulture(this string format, params object[] args)
33-
{
34-
return string.Format(CultureInfo.InvariantCulture, format, args);
35-
}
24+
public static string FormatWithInvariantCulture(this string format, params object[] args) =>
25+
string.Format(CultureInfo.InvariantCulture, format, args);
3626

37-
public static string Then(this string input, string value)
38-
{
39-
return string.Concat(input, value);
40-
}
27+
public static string Then(this string input, string value) => string.Concat(input, value);
4128

42-
public static string UrlEncode(this string value)
43-
{
44-
// [DC] This is more correct than HttpUtility; it escapes spaces as %20, not +
45-
return Uri.EscapeDataString(value);
46-
}
29+
public static string UrlEncode(this string value) => Uri.EscapeDataString(value);
4730

48-
public static string UrlDecode(this string value)
49-
{
50-
return Uri.UnescapeDataString(value);
51-
}
31+
public static string UrlDecode(this string value) => Uri.UnescapeDataString(value);
5232

53-
public static Uri AsUri(this string value)
54-
{
55-
return new Uri(value);
56-
}
33+
public static Uri AsUri(this string value) => new Uri(value);
5734

58-
public static string ToBase64String(this byte[] input)
59-
{
60-
return Convert.ToBase64String(input);
61-
}
35+
public static string ToBase64String(this byte[] input) => Convert.ToBase64String(input);
6236

63-
public static byte[] GetBytes(this string input)
64-
{
65-
return Encoding.UTF8.GetBytes(input);
66-
}
37+
public static byte[] GetBytes(this string input) => Encoding.UTF8.GetBytes(input);
6738

6839
public static string PercentEncode(this string s)
6940
{

RestSharp/Deserializers/XmlDeserializer.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -424,17 +424,18 @@ protected virtual XElement GetElementByName(XElement root, XName name)
424424
if (root.Element(camelName) != null)
425425
return root.Element(camelName);
426426

427-
if (name == "Value".AsNamespaced(name.NamespaceName) &&
428-
(!root.HasAttributes || root.Attributes().All(x => x.Name != name)))
429-
return root;
430-
431427
// try looking for element that matches sanitized property name (Order by depth)
432-
return root.Descendants()
428+
var element = root.Descendants()
433429
.OrderBy(d => d.Ancestors().Count())
434430
.FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName) ??
435431
root.Descendants()
436432
.OrderBy(d => d.Ancestors().Count())
437433
.FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName.ToLower());
434+
435+
return element == null && name == "Value".AsNamespaced(name.NamespaceName) &&
436+
(!root.HasAttributes || root.Attributes().All(x => x.Name != name))
437+
? root
438+
: element;
438439
}
439440

440441
protected virtual XAttribute GetAttributeByName(XElement root, XName name)

RestSharp/Extensions/StringExtensions.cs

+38-85
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ namespace RestSharp.Extensions
2727
{
2828
public static class StringExtensions
2929
{
30-
public static string UrlDecode(this string input)
31-
{
32-
return HttpUtility.UrlDecode(input);
33-
}
30+
public static string UrlDecode(this string input) => HttpUtility.UrlDecode(input);
3431

3532
/// <summary>
3633
/// Uses Uri.EscapeDataString() based on recommendations on MSDN
@@ -61,46 +58,28 @@ public static string UrlEncode(this string input)
6158
return sb.ToString();
6259
}
6360

64-
public static string HtmlDecode(this string input)
65-
{
66-
return HttpUtility.HtmlDecode(input);
67-
}
61+
public static string HtmlDecode(this string input) => HttpUtility.HtmlDecode(input);
6862

69-
public static string HtmlEncode(this string input)
70-
{
71-
return HttpUtility.HtmlEncode(input);
72-
}
63+
public static string HtmlEncode(this string input) => HttpUtility.HtmlEncode(input);
7364

74-
public static string UrlEncode(this string input, Encoding encoding)
75-
{
76-
return HttpUtility.UrlEncode(input, encoding);
77-
}
65+
public static string UrlEncode(this string input, Encoding encoding) => HttpUtility.UrlEncode(input, encoding);
7866

79-
public static string HtmlAttributeEncode(this string input)
80-
{
81-
return HttpUtility.HtmlAttributeEncode(input);
82-
}
67+
public static string HtmlAttributeEncode(this string input) => HttpUtility.HtmlAttributeEncode(input);
8368

8469
/// <summary>
8570
/// Check that a string is not null or empty
8671
/// </summary>
8772
/// <param name="input">String to check</param>
8873
/// <returns>bool</returns>
89-
public static bool HasValue(this string input)
90-
{
91-
return !string.IsNullOrEmpty(input);
92-
}
74+
public static bool HasValue(this string input) => !string.IsNullOrEmpty(input);
9375

9476
/// <summary>
9577
/// Remove underscores from a string
9678
/// </summary>
9779
/// <param name="input">String to process</param>
9880
/// <returns>string</returns>
99-
public static string RemoveUnderscoresAndDashes(this string input)
100-
{
101-
return input.Replace("_", "")
102-
.Replace("-", ""); // avoiding regex
103-
}
81+
public static string RemoveUnderscoresAndDashes(this string input) =>
82+
input.Replace("_", "").Replace("-", "");
10483

10584
/// <summary>
10685
/// Parses most common JSON date formats
@@ -116,15 +95,11 @@ public static DateTime ParseJsonDate(this string input, CultureInfo culture)
11695
input = input.Replace("\r", "");
11796
input = input.RemoveSurroundingQuotes();
11897

119-
long unix;
120-
121-
if (long.TryParse(input, out unix))
98+
if (long.TryParse(input, out var unix))
12299
{
123100
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
124101

125-
if (unix > maxAllowedTimestamp)
126-
return epoch.AddMilliseconds(unix);
127-
return epoch.AddSeconds(unix);
102+
return unix > maxAllowedTimestamp ? epoch.AddMilliseconds(unix) : epoch.AddSeconds(unix);
128103
}
129104

130105
if (input.Contains("/Date("))
@@ -180,24 +155,23 @@ private static DateTime ExtractDate(string input, string pattern, CultureInfo cu
180155
var dt = DateTime.MinValue;
181156
var regex = new Regex(pattern);
182157

183-
if (regex.IsMatch(input))
184-
{
185-
var matches = regex.Matches(input);
186-
var match = matches[0];
187-
var ms = Convert.ToInt64(match.Groups[1].Value);
188-
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
158+
if (!regex.IsMatch(input)) return dt;
189159

190-
dt = epoch.AddMilliseconds(ms);
160+
var matches = regex.Matches(input);
161+
var match = matches[0];
162+
var ms = Convert.ToInt64(match.Groups[1].Value);
163+
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
191164

192-
// adjust if time zone modifier present
193-
if (match.Groups.Count <= 2 || string.IsNullOrEmpty(match.Groups[3].Value)) return dt;
165+
dt = epoch.AddMilliseconds(ms);
194166

195-
var mod = DateTime.ParseExact(match.Groups[3].Value, "HHmm", culture);
167+
// adjust if time zone modifier present
168+
if (match.Groups.Count <= 2 || string.IsNullOrEmpty(match.Groups[3].Value)) return dt;
196169

197-
dt = match.Groups[2].Value == "+"
198-
? dt.Add(mod.TimeOfDay)
199-
: dt.Subtract(mod.TimeOfDay);
200-
}
170+
var mod = DateTime.ParseExact(match.Groups[3].Value, "HHmm", culture);
171+
172+
dt = match.Groups[2].Value == "+"
173+
? dt.Add(mod.TimeOfDay)
174+
: dt.Subtract(mod.TimeOfDay);
201175

202176
return dt;
203177
}
@@ -208,21 +182,16 @@ private static DateTime ExtractDate(string input, string pattern, CultureInfo cu
208182
/// <param name="input">String to check</param>
209183
/// <param name="pattern">Pattern to match</param>
210184
/// <returns>bool</returns>
211-
public static bool Matches(this string input, string pattern)
212-
{
213-
return Regex.IsMatch(input, pattern);
214-
}
185+
public static bool Matches(this string input, string pattern) => Regex.IsMatch(input, pattern);
215186

216187
/// <summary>
217188
/// Converts a string to pascal case
218189
/// </summary>
219190
/// <param name="lowercaseAndUnderscoredWord">String to convert</param>
220191
/// <param name="culture"></param>
221192
/// <returns>string</returns>
222-
public static string ToPascalCase(this string lowercaseAndUnderscoredWord, CultureInfo culture)
223-
{
224-
return ToPascalCase(lowercaseAndUnderscoredWord, true, culture);
225-
}
193+
public static string ToPascalCase(this string lowercaseAndUnderscoredWord, CultureInfo culture) =>
194+
ToPascalCase(lowercaseAndUnderscoredWord, true, culture);
226195

227196
/// <summary>
228197
/// Converts a string to pascal case with the option to remove underscores
@@ -270,88 +239,72 @@ public static string ToPascalCase(this string text, bool removeUnderscores, Cult
270239
/// <param name="lowercaseAndUnderscoredWord">String to convert</param>
271240
/// <param name="culture"></param>
272241
/// <returns>String</returns>
273-
public static string ToCamelCase(this string lowercaseAndUnderscoredWord, CultureInfo culture)
274-
{
275-
return MakeInitialLowerCase(ToPascalCase(lowercaseAndUnderscoredWord, culture));
276-
}
242+
public static string ToCamelCase(this string lowercaseAndUnderscoredWord, CultureInfo culture) =>
243+
MakeInitialLowerCase(ToPascalCase(lowercaseAndUnderscoredWord, culture));
277244

278245
/// <summary>
279246
/// Convert the first letter of a string to lower case
280247
/// </summary>
281248
/// <param name="word">String to convert</param>
282249
/// <returns>string</returns>
283-
public static string MakeInitialLowerCase(this string word)
284-
{
285-
return string.Concat(word.Substring(0, 1).ToLower(), word.Substring(1));
286-
}
250+
public static string MakeInitialLowerCase(this string word) =>
251+
string.Concat(word.Substring(0, 1).ToLower(), word.Substring(1));
287252

288253
/// <summary>
289254
/// Checks to see if a string is all uppper case
290255
/// </summary>
291256
/// <param name="inputString">String to check</param>
292257
/// <returns>bool</returns>
293-
public static bool IsUpperCase(this string inputString)
294-
{
295-
return Regex.IsMatch(inputString, @"^[A-Z]+$");
296-
}
258+
public static bool IsUpperCase(this string inputString) => Regex.IsMatch(inputString, @"^[A-Z]+$");
297259

298260
/// <summary>
299261
/// Add underscores to a pascal-cased string
300262
/// </summary>
301263
/// <param name="pascalCasedWord">String to convert</param>
302264
/// <returns>string</returns>
303-
public static string AddUnderscores(this string pascalCasedWord)
304-
{
305-
return Regex.Replace(
265+
public static string AddUnderscores(this string pascalCasedWord) =>
266+
Regex.Replace(
306267
Regex.Replace(
307268
Regex.Replace(pascalCasedWord, @"([A-Z]+)([A-Z][a-z])", "$1_$2"),
308269
@"([a-z\d])([A-Z])",
309270
"$1_$2"),
310271
@"[-\s]",
311272
"_");
312-
}
313273

314274
/// <summary>
315275
/// Add dashes to a pascal-cased string
316276
/// </summary>
317277
/// <param name="pascalCasedWord">String to convert</param>
318278
/// <returns>string</returns>
319-
public static string AddDashes(this string pascalCasedWord)
320-
{
321-
return Regex.Replace(
279+
public static string AddDashes(this string pascalCasedWord) =>
280+
Regex.Replace(
322281
Regex.Replace(
323282
Regex.Replace(pascalCasedWord, @"([A-Z]+)([A-Z][a-z])", "$1-$2"),
324283
@"([a-z\d])([A-Z])",
325284
"$1-$2"),
326285
@"[\s]",
327286
"-");
328-
}
329287

330288
/// <summary>
331289
/// Add an undescore prefix to a pascasl-cased string
332290
/// </summary>
333291
/// <param name="pascalCasedWord"></param>
334292
/// <returns></returns>
335-
public static string AddUnderscorePrefix(this string pascalCasedWord)
336-
{
337-
return string.Format("_{0}", pascalCasedWord);
338-
}
293+
public static string AddUnderscorePrefix(this string pascalCasedWord) => string.Format("_{0}", pascalCasedWord);
339294

340295
/// <summary>
341296
/// Add spaces to a pascal-cased string
342297
/// </summary>
343298
/// <param name="pascalCasedWord">String to convert</param>
344299
/// <returns>string</returns>
345-
public static string AddSpaces(this string pascalCasedWord)
346-
{
347-
return Regex.Replace(
300+
public static string AddSpaces(this string pascalCasedWord) =>
301+
Regex.Replace(
348302
Regex.Replace(
349303
Regex.Replace(pascalCasedWord, @"([A-Z]+)([A-Z][a-z])", "$1 $2"),
350304
@"([a-z\d])([A-Z])",
351305
"$1 $2"),
352306
@"[-\s]",
353307
" ");
354-
}
355308

356309
/// <summary>
357310
/// Return possible variants of a name for name matching.

0 commit comments

Comments
 (0)