Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Refit.Tests/RequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
Expand All @@ -9,7 +9,9 @@
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.AspNetCore.WebUtilities;

using Xunit;

namespace Refit.Tests
Expand Down Expand Up @@ -1726,6 +1728,21 @@ public void GenericReturnTypeIsNotTaskOrObservableShouldThrow()
)
);
}

[Fact]
public void ShouldCaptureLastCharacterWhenRouteEndsWithConstant()
{
var fixture = new RequestBuilderImplementation<IDummyHttpApi>();
var factory = fixture.BuildRequestFactoryForMethod(
"GetWithParenthesis"
);
var output = factory(["1"]);

var uri = new Uri(new Uri("http://api/foo/bar/"), output.RequestUri);

Assert.EndsWith(")", uri.PathAndQuery, StringComparison.Ordinal);
Assert.Contains("/foo/bar/(1)", uri.PathAndQuery, StringComparison.Ordinal);
}
}

[Headers("User-Agent: RefitTestClient", "Api-Version: 1")]
Expand Down Expand Up @@ -1767,6 +1784,9 @@ Task<string> FetchSomeStuffWithHardcodedAndOtherQueryParameters(
[Get("/foo/bar/{id}?param1={id}&param2={id}")]
Task<string> FetchSomeStuffWithTheSameId(int id);

[Get("/foo/bar/({id})")]
Task<string> GetWithParenthesis(int id);

[Get("/foo/bar?param=first {id} and second {id}")]
Task<string> FetchSomeStuffWithTheIdInAParameterMultipleTimes(int id);

Expand Down
20 changes: 10 additions & 10 deletions Refit/RestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Type ReturnType
[DebuggerDisplay("{MethodInfo}")]
internal class RestMethodInfoInternal
{
private int HeaderCollectionParameterIndex { get; }
private int HeaderCollectionParameterIndex { get; }
private string Name => MethodInfo.Name;
public Type Type { get; }
public MethodInfo MethodInfo { get; }
Expand All @@ -47,7 +47,7 @@ internal class RestMethodInfoInternal
public Dictionary<int, Tuple<string, string>> AttachmentNameMap { get; }
public ParameterInfo[] ParameterInfoArray { get; }
public Dictionary<int, RestMethodParameterInfo> ParameterMap { get; }
public List<ParameterFragment> FragmentPath { get ; set ; }
public List<ParameterFragment> FragmentPath { get; set; }
public Type ReturnType { get; set; }
public Type ReturnResultType { get; set; }
public Type DeserializedResultType { get; set; }
Expand Down Expand Up @@ -167,7 +167,7 @@ public RestMethodInfoInternal(
RestMethodInfo = new RestMethodInfo(Name, Type, MethodInfo, RelativePath, ReturnType!);
CancellationToken = ctParam;

QueryUriFormat = methodInfo.GetCustomAttribute<QueryUriFormatAttribute>()?.UriFormat
QueryUriFormat = methodInfo.GetCustomAttribute<QueryUriFormatAttribute>()?.UriFormat
?? UriFormat.UriEscaped;

IsApiResponse =
Expand Down Expand Up @@ -201,7 +201,7 @@ static int GetHeaderCollectionParameterIndex(ParameterInfo[] parameterArray)
if (param.ParameterType.IsAssignableFrom(typeof(IDictionary<string, string>)))
{
// throw if there is already a HeaderCollection parameter
if(headerIndex >= 0)
if (headerIndex >= 0)
throw new ArgumentException("Only one parameter can be a HeaderCollection parameter");

headerIndex = i;
Expand Down Expand Up @@ -278,7 +278,7 @@ ParameterInfo[] parameterInfo

if (parameterizedParts.Length == 0)
{
if(string.IsNullOrEmpty(relativePath))
if (string.IsNullOrEmpty(relativePath))
return (ret, []);

return (ret, [ParameterFragment.Constant(relativePath)]);
Expand All @@ -300,7 +300,7 @@ ParameterInfo[] parameterInfo
var fragmentList = new List<ParameterFragment>();
var index = 0;

foreach (var match in parameterizedParts)
foreach (var match in parameterizedParts)
{
// Add constant value from given http path
if (match.Index != index)
Expand Down Expand Up @@ -402,7 +402,7 @@ ParameterInfo[] parameterInfo
}

// add trailing string
if (index < relativePath.Length - 1)
if (index < relativePath.Length)
{
var trailingConstant = relativePath.Substring(index, relativePath.Length - index);
fragmentList.Add(ParameterFragment.Constant(trailingConstant));
Expand Down Expand Up @@ -708,8 +708,8 @@ internal record struct ParameterFragment(string? Value, int ArgumentIndex, int P
public bool IsDynamicRoute => ArgumentIndex >= 0 && PropertyIndex < 0;
public bool IsObjectProperty => ArgumentIndex >= 0 && PropertyIndex >= 0;

public static ParameterFragment Constant(string value) => new (value, -1, -1);
public static ParameterFragment Dynamic(int index) => new (null, index, -1);
public static ParameterFragment DynamicObject(int index, int propertyIndex) => new (null, index, propertyIndex);
public static ParameterFragment Constant(string value) => new(value, -1, -1);
public static ParameterFragment Dynamic(int index) => new(null, index, -1);
public static ParameterFragment DynamicObject(int index, int propertyIndex) => new(null, index, propertyIndex);
}
}