diff --git a/Refit-Tests/RequestBuilder.cs b/Refit-Tests/RequestBuilder.cs index af4d06730..6511e52df 100644 --- a/Refit-Tests/RequestBuilder.cs +++ b/Refit-Tests/RequestBuilder.cs @@ -30,6 +30,9 @@ public interface IRestMethodInfoTests [Get("/foo/bar/{id}")] Task FetchSomeStuffWithAlias([AliasAs("id")] int anId); + [Get("/foo/bar/{width}x{height}")] + Task FetchAnImage(int width, int height); + [Get("/foo/bar/{id}")] IObservable FetchSomeStuffWithBody([AliasAs("id")] int anId, [Body] Dictionary theData); @@ -120,6 +123,17 @@ public void AliasMappingShouldWork() Assert.IsNull(fixture.BodyParameterInfo); } + [Test] + public void MultipleParametersPerSegmentShouldWork() + { + var input = typeof(IRestMethodInfoTests); + var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "FetchAnImage")); + Assert.AreEqual("width", fixture.ParameterMap[0]); + Assert.AreEqual("height", fixture.ParameterMap[1]); + Assert.AreEqual(0, fixture.QueryParameterMap.Count); + Assert.IsNull(fixture.BodyParameterInfo); + } + [Test] public void FindTheBodyParameter() { @@ -202,6 +216,9 @@ public interface IDummyHttpApi [Get("/foo/bar/{id}?baz=bamf")] Task FetchSomeStuffWithHardcodedAndOtherQueryParameters(int id, [AliasAs("search_for")] string searchQuery); + [Get("/{id}/{width}x{height}/foo")] + Task FetchSomethingWithMultipleParametersPerSegment(int id, int width, int height); + [Get("/foo/bar/{id}")] [Headers("Api-Version: 2")] Task FetchSomeStuffWithHardcodedHeader(int id); @@ -305,6 +322,17 @@ public void ParameterizedQueryParamsShouldBeInUrl() Assert.AreEqual("/foo/bar/6?baz=bamf&search_for=foo", uri.PathAndQuery); } + [Test] + public void MultipleParametersInTheSameSegmentAreGeneratedProperly() + { + var fixture = new RequestBuilderImplementation(typeof(IDummyHttpApi)); + var factory = fixture.BuildRequestFactoryForMethod("FetchSomethingWithMultipleParametersPerSegment"); + var output = factory(new object[] { 6, 1024, 768 }); + + var uri = new Uri(new Uri("http://api"), output.RequestUri); + Assert.AreEqual("/6/1024x768/foo", uri.PathAndQuery); + } + [Test] public void HardcodedHeadersShouldBeInHeaders() { diff --git a/Refit/RequestBuilderImplementation.cs b/Refit/RequestBuilderImplementation.cs index ebc294473..e62badbf2 100644 --- a/Refit/RequestBuilderImplementation.cs +++ b/Refit/RequestBuilderImplementation.cs @@ -331,7 +331,7 @@ public class RestMethodInfo public Type ReturnType { get; set; } public Type SerializedReturnType { get; set; } - static readonly Regex parameterRegex = new Regex(@"^{(.*)}$"); + static readonly Regex parameterRegex = new Regex(@"{(.*?)}"); public RestMethodInfo(Type targetInterface, MethodInfo methodInfo) { @@ -388,10 +388,9 @@ Dictionary buildParameterMap(string relativePath, List(); - var parameterizedParts = relativePath.Split('/', '?').SelectMany(x => { - var m = parameterRegex.Match(x); - return (m.Success ? EnumerableEx.Return(m) : Enumerable.Empty()); - }).ToList(); + var parameterizedParts = relativePath.Split('/', '?') + .SelectMany(x => parameterRegex.Matches(x).Cast()) + .ToList(); if (parameterizedParts.Count == 0) { return ret;