Skip to content

Commit f1af4a5

Browse files
baynezyChrisPulman
andauthored
fix: stop removing trailing single chars from query string (#2045)
* bug: stop removing trailing single chars from query string Closes #2044 * Add more code coverage requested in review #2044 (comment) --------- Co-authored-by: Chris Pulman <chris.pulman@yahoo.com>
1 parent 1b67a8c commit f1af4a5

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

Refit.Tests/RequestBuilder.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,15 @@ Task<string> FetchSomeStuffWithHardcodedAndOtherQueryParameters(
17701770
[Get("/foo/bar?param=first {id} and second {id}")]
17711771
Task<string> FetchSomeStuffWithTheIdInAParameterMultipleTimes(int id);
17721772

1773+
[Get("/foo?q=app_metadata.id:\"{id}\"")]
1774+
Task<string> FetchSomeStuffWithDoubleQuotesInUrl(int id);
1775+
1776+
[Get("/foo/bar/({id})")]
1777+
Task<string> GetWithTrainingParenthesis(int id);
1778+
1779+
[Get("/foo/bar/{id}/")]
1780+
Task<string> GetWithTrailingSlash(int id);
1781+
17731782
[Post("/foo/bar/{id}")]
17741783
[Headers("Content-Type: literally/anything")]
17751784
Task<string> PostSomeStuffWithHardCodedContentTypeHeader(int id, [Body] string content);
@@ -2594,6 +2603,37 @@ public void QueryParamWithPathDelimiterShouldBeEncoded()
25942603
);
25952604
}
25962605

2606+
[Fact]
2607+
public void QueryParamWhichEndsInDoubleQuotesShouldNotBeTruncated()
2608+
{
2609+
var fixture = new RequestBuilderImplementation<IDummyHttpApi>();
2610+
var factory = fixture.BuildRequestFactoryForMethod(
2611+
"FetchSomeStuffWithDoubleQuotesInUrl"
2612+
);
2613+
var output = factory([42]);
2614+
2615+
var uri = new Uri(new Uri("http://api"), output.RequestUri!);
2616+
2617+
Assert.Equal("/foo?q=app_metadata.id%3A%2242%22", uri.PathAndQuery);
2618+
}
2619+
2620+
[Theory]
2621+
[InlineData("GetWithTrainingParenthesis", ")", "/foo/bar/(1)")]
2622+
[InlineData("GetWithTrailingSlash", "/", "/foo/bar/1/")]
2623+
public void ShouldCaptureLastCharacterWhenRouteEndsWithConstant(string methodToTest, string constantChar, string contains)
2624+
{
2625+
var fixture = new RequestBuilderImplementation<IDummyHttpApi>();
2626+
var factory = fixture.BuildRequestFactoryForMethod(
2627+
methodToTest
2628+
);
2629+
var output = factory(["1"]);
2630+
2631+
var uri = new Uri(new Uri("http://api/"), output.RequestUri!);
2632+
2633+
Assert.EndsWith(constantChar, uri.PathAndQuery, StringComparison.Ordinal);
2634+
Assert.Contains(contains, uri.PathAndQuery, StringComparison.Ordinal);
2635+
}
2636+
25972637
[Fact]
25982638
public void ParameterizedQueryParamsShouldBeInUrlAndValuesEncodedWhenMixedReplacementAndQueryBadId()
25992639
{

Refit/RestMethodInfo.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,12 @@ ParameterInfo[] parameterInfo
401401
}
402402
}
403403

404+
if (index >= relativePath.Length) return (ret, fragmentList);
405+
404406
// add trailing string
405-
if (index < relativePath.Length - 1)
406-
{
407-
var trailingConstant = relativePath.Substring(index, relativePath.Length - index);
408-
fragmentList.Add(ParameterFragment.Constant(trailingConstant));
409-
}
407+
var trailingConstant = relativePath.Substring(index);
408+
fragmentList.Add(ParameterFragment.Constant(trailingConstant));
409+
410410
return (ret, fragmentList);
411411
}
412412

0 commit comments

Comments
 (0)