Skip to content

Commit 603cb85

Browse files
committed
Fixes #2288
1 parent 488091c commit 603cb85

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

src/RestSharp/Parameters/UrlSegmentParameter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public partial record UrlSegmentParameter : NamedParameter {
2828
/// <param name="value">Parameter value</param>
2929
/// <param name="encode">Optional: encode the value, default is true</param>
3030
/// <param name="replaceEncodedSlash">Optional: whether to replace all %2f and %2F in the parameter value with '/', default is true</param>
31-
public UrlSegmentParameter(string name, string value, bool encode = true, bool replaceEncodedSlash = true)
31+
public UrlSegmentParameter(string name, string? value, bool encode = true, bool replaceEncodedSlash = true)
3232
: base(
3333
name,
34-
value.IsEmpty() ? value : replaceEncodedSlash ? RegexPattern.Replace(value, "/") : value,
34+
value.IsEmpty() ? string.Empty : replaceEncodedSlash ? RegexPattern.Replace(value, "/") : value,
3535
ParameterType.UrlSegment,
3636
encode
3737
) { }

src/RestSharp/Request/RestRequestExtensions.Url.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,27 @@
1515
namespace RestSharp;
1616

1717
public static partial class RestRequestExtensions {
18-
1918
/// <summary>
2019
/// Adds a URL segment parameter to the request. The resource URL must have a placeholder for the parameter for it to work.
2120
/// For example, if you add a URL segment parameter with the name "id", the resource URL should contain {id} in its path.
2221
/// </summary>
2322
/// <param name="request">Request instance</param>
24-
/// <param name="name">Name of the parameter, must be matching a placeholder in the resource URL as {name}</param>
23+
/// <param name="name">Name of the parameter; must be matching a placeholder in the resource URL as {name}</param>
2524
/// <param name="value">Value of the parameter</param>
2625
/// <param name="encode">Encode the value or not, default true</param>
2726
/// <returns></returns>
28-
public static RestRequest AddUrlSegment(this RestRequest request, string name, string value, bool encode = true)
27+
public static RestRequest AddUrlSegment(this RestRequest request, string name, string? value, bool encode = true)
2928
=> request.AddParameter(new UrlSegmentParameter(name, value, encode));
3029

3130
/// <summary>
3231
/// Adds a URL segment parameter to the request. The resource URL must have a placeholder for the parameter for it to work.
3332
/// For example, if you add a URL segment parameter with the name "id", the resource URL should contain {id} in its path.
3433
/// </summary>
3534
/// <param name="request">Request instance</param>
36-
/// <param name="name">Name of the parameter, must be matching a placeholder in the resource URL as {name}</param>
35+
/// <param name="name">Name of the parameter; must be matching a placeholder in the resource URL as {name}</param>
3736
/// <param name="value">Value of the parameter</param>
3837
/// <param name="encode">Encode the value or not, default true</param>
3938
/// <returns></returns>
4039
public static RestRequest AddUrlSegment<T>(this RestRequest request, string name, T value, bool encode = true) where T : struct
41-
=> request.AddUrlSegment(name, Ensure.NotNull(value.ToString(), nameof(value)), encode);
40+
=> request.AddUrlSegment(name, value.ToString(), encode);
4241
}

src/RestSharp/Request/UriExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ params ParametersCollection[] parametersCollections
6868
builder.Path = builder.Path.UrlDecode().Replace(paramPlaceHolder, paramValue);
6969
}
7070

71-
return new UrlSegmentParamsValues(builder.Uri, assembled);
71+
return new(builder.Uri, assembled);
7272
}
7373
}
7474

test/RestSharp.Tests/UrlBuilderTests.Get.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public void GET_with_empty_request_and_query_parameters_without_encoding() {
6262

6363
[Fact]
6464
public void GET_with_Invalid_Url_string_throws_exception()
65-
=> Assert.Throws<UriFormatException>(
66-
() => { _ = new RestClient("invalid url"); }
65+
=> Assert.Throws<UriFormatException>(() => { _ = new RestClient("invalid url"); }
6766
);
6867

6968
[Fact]
@@ -107,7 +106,12 @@ public void GET_with_multiple_instances_of_same_key() {
107106
[Fact]
108107
public void GET_with_resource_containing_null_token() {
109108
var request = new RestRequest($"/{Resource}/{{foo}}");
110-
Assert.Throws<ArgumentNullException>(() => request.AddUrlSegment("foo", null!));
109+
request.AddUrlSegment("foo", null);
110+
111+
using var client = new RestClient(new Uri(Base));
112+
113+
var output = client.BuildUri(request);
114+
Assert.Equal(new($"{Base}/{Resource}/"), output);
111115
}
112116

113117
[Fact]

0 commit comments

Comments
 (0)