Skip to content

Commit

Permalink
Merge pull request #789 from reactiveui/index-fixes
Browse files Browse the repository at this point in the history
Get rid of query shift parameter and rely on value from dictionary key
  • Loading branch information
Oren Novotny authored Nov 18, 2019
2 parents 96b11d4 + 2fed231 commit 87afba2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Refit.Tests/Refit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="StrongNamer" Version="0.2.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="System.Reactive" Version="4.2.0" />
Expand All @@ -24,6 +23,7 @@

<ItemGroup Condition="'$(TargetFramework)' == 'net461' ">
<Reference Include="System.Net.Http" />
<PackageReference Include="StrongNamer" Version="0.2.5" />
</ItemGroup>
<ItemGroup>
<None Include="RefitStubs.*.cs" />
Expand Down
29 changes: 28 additions & 1 deletion Refit.Tests/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,10 @@ Task<ApiResponse<object>> UploadFile(int companyId,

[Get("/api/{obj.someProperty}")]
Task QueryWithOptionalParametersPathBoundObject(PathBoundObject obj, [Query]string text = null, [Query]int? optionalId = null, [Query(CollectionFormat = CollectionFormat.Multi)]string[] filters = null);

[Headers("Accept:application/json", "X-API-V: 125")]
[Get("/api/someModule/deviceList?controlId={control_id}")]
Task QueryWithHeadersBeforeData([Header("Authorization")] string authorization, [Header("X-Lng")] string twoLetterLang, string search, [AliasAs("control_id")] string controlId, string secret);
}

interface ICancellableMethods
Expand Down Expand Up @@ -1594,7 +1598,7 @@ public void MultipartPostWithAliasAndHeader()
public void PostBlobByteWithAlias()
{
var fixture = new RequestBuilderImplementation<IDummyHttpApi>();
var factory = fixture.BuildRequestFactoryForMethod("Blob_Post_Byte");
var factory = fixture.BuildRequestFactoryForMethod(nameof(IDummyHttpApi.Blob_Post_Byte));

var bytes = new byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Expand All @@ -1607,6 +1611,29 @@ public void PostBlobByteWithAlias()
Assert.Equal("/blobstorage/the/path", uri.PathAndQuery);
}

[Fact]
public void QueryWithAliasAndHeadersWorks()
{
var fixture = new RequestBuilderImplementation<IDummyHttpApi>();
var factory = fixture.BuildRequestFactoryForMethod(nameof(IDummyHttpApi.QueryWithHeadersBeforeData));

var authHeader = "theAuth";
var langHeader = "LnG";
var searchParam = "theSearchParam";
var controlIdParam = "theControlId";
var secretValue = "theSecret";



var output = factory(new object[] { authHeader, langHeader, searchParam, controlIdParam, secretValue });

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

Assert.Equal($"/api/someModule/deviceList?controlId={controlIdParam}&search={searchParam}&secret={secretValue}", uri.PathAndQuery);
Assert.Equal(langHeader, output.Headers.GetValues("X-LnG").FirstOrDefault());
Assert.Equal(authHeader, output.Headers.Authorization?.Scheme);
}

class RequestBuilderMock : IRequestBuilder
{
public int CallCount { get; private set; }
Expand Down
11 changes: 4 additions & 7 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,7 @@ Func<object[], Task<HttpRequestMessage>> BuildRequestFactoryForMethod(RestMethod
var urlTarget = (basePath == "/" ? string.Empty : basePath) + restMethod.RelativePath;
var queryParamsToAdd = new List<KeyValuePair<string, string>>();
var headersToAdd = new Dictionary<string, string>(restMethod.Headers);
RestMethodParameterInfo parameterInfo = null;
var queryParamShift = 0;
RestMethodParameterInfo parameterInfo = null;

for (var i = 0; i < paramList.Length; i++)
{
Expand All @@ -477,7 +476,6 @@ Func<object[], Task<HttpRequestMessage>> BuildRequestFactoryForMethod(RestMethod
propertyInfo.PropertyInfo.PropertyType) ?? string.Empty),
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}
queryParamShift++;
//don't continue here as we want it to fall through so any parameters on this object not bound here get passed as query parameters
}
else
Expand Down Expand Up @@ -511,7 +509,6 @@ Func<object[], Task<HttpRequestMessage>> BuildRequestFactoryForMethod(RestMethod
replacement,
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

queryParamShift++;
continue;

}
Expand Down Expand Up @@ -603,7 +600,7 @@ Func<object[], Task<HttpRequestMessage>> BuildRequestFactoryForMethod(RestMethod
foreach (var paramValue in paramValues)
{
queryParamsToAdd.Add(new KeyValuePair<string, string>(
restMethod.QueryParameterMap.ElementAt(i - queryParamShift).Value,
restMethod.QueryParameterMap[i],
settings.UrlParameterFormatter.Format(paramValue, restMethod.ParameterInfoMap[i], restMethod.ParameterInfoMap[i].ParameterType)));
}
continue;
Expand All @@ -620,13 +617,13 @@ Func<object[], Task<HttpRequestMessage>> BuildRequestFactoryForMethod(RestMethod
.Select(v => settings.UrlParameterFormatter.Format(v, restMethod.ParameterInfoMap[i], restMethod.ParameterInfoMap[i].ParameterType));

queryParamsToAdd.Add(new KeyValuePair<string, string>(
restMethod.QueryParameterMap.ElementAt(i - queryParamShift).Value,
restMethod.QueryParameterMap[i],
string.Join(delimiter, formattedValues)));
continue;
}
}

queryParamsToAdd.Add(new KeyValuePair<string, string>(restMethod.QueryParameterMap.ElementAt(i - queryParamShift).Value, settings.UrlParameterFormatter.Format(param, restMethod.ParameterInfoMap[i], restMethod.ParameterInfoMap[i].ParameterType)));
queryParamsToAdd.Add(new KeyValuePair<string, string>(restMethod.QueryParameterMap[i], settings.UrlParameterFormatter.Format(param, restMethod.ParameterInfoMap[i], restMethod.ParameterInfoMap[i].ParameterType)));
}
else
{
Expand Down

0 comments on commit 87afba2

Please sign in to comment.