Skip to content

Commit 854120b

Browse files
authored
Merge pull request #410 from microsoftgraph/po/queryOptionFix
Remove leading ? from query option on PS 5.1.
2 parents 310a3c6 + 8076cb9 commit 854120b

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

.azure-pipelines/generate-auth-module-template.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ jobs:
6161
- task: CodeMetrics@1
6262
displayName: 'Run CodeMetrics'
6363
inputs:
64-
Files: '$(System.DefaultWorkingDirectory)//**//*Microsoft.Graph*.dll;$(System.DefaultWorkingDirectory)//**//*Microsoft.Graph*.exe'
65-
file: '$(System.DefaultWorkingDirectory)//**//*Microsoft.Graph*.dll;$(System.DefaultWorkingDirectory)//**//*Microsoft.Graph*.exe'
64+
Files: '$(System.DefaultWorkingDirectory)//**//Microsoft.Graph.Authentication.dll;$(System.DefaultWorkingDirectory)//**//Microsoft.Graph.Authentication.exe'
6665
continueOnError: true
6766

6867
- task: PowerShell@2

src/Authentication/Authentication.Test/Helpers/AuthenticationHelpersTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public void ShouldUseDeviceCodeProviderWhenDelegatedContextIsProvided()
6666
GraphSession.Reset();
6767
}
6868

69+
#if NETCORE
6970
[Fact]
7071
public void ShouldUseClientCredentialProviderWhenAppOnlyContextIsProvided()
7172
{
@@ -133,6 +134,7 @@ private void DeleteSelfSignedCert(string certificateName)
133134
xStore.Remove(xCertificate);
134135
}
135136
}
137+
#endif
136138

137139
}
138140
}

src/Authentication/Authentication.Test/Helpers/ODataQueryOptionsHandlerTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Microsoft.Graph.Authentication.Test
22
{
33
using Microsoft.Graph.PowerShell.Authentication.Helpers;
44
using System;
5+
using System.Linq;
56
using System.Net.Http;
67
using System.Threading;
78
using System.Threading.Tasks;
@@ -51,6 +52,7 @@ public async Task ShouldAddDollarSignToStandardODataQueryOptionsToV1Endpoint()
5152
Assert.Contains($"${filterParam}", sentRequestQuery);
5253
Assert.Contains($"${expandParam}", sentRequestQuery);
5354
Assert.Equal(5, sentRequestQuery.Split('&').Length);
55+
Assert.Equal(1, response.RequestMessage.RequestUri.AbsoluteUri.Count(u => (u == '?')));
5456
}
5557

5658
[Fact]

src/Authentication/Authentication.Test/Microsoft.Graph.Authentication.Test.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
5-
4+
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>
65
<IsPackable>false</IsPackable>
76
</PropertyGroup>
87

8+
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
9+
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
10+
</PropertyGroup>
11+
912
<ItemGroup>
1013
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
1114
<PackageReference Include="xunit" Version="2.4.0" />

src/Authentication/Authentication/Helpers/ODataQueryOptionsHandler.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ public ODataQueryOptionsHandler(HttpMessageHandler innerHandler)
5353
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the request.</param>
5454
/// <returns></returns>
5555
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
56+
{
57+
request = AddDollarSignToQueryParameters(request);
58+
return base.SendAsync(request, cancellationToken);
59+
}
60+
61+
/// <summary>
62+
/// Add `$` to all standard OData query options. v1.0 endpoint requires $ to be prefixed to all OData query options.
63+
/// </summary>
64+
/// <param name="request"></param>
65+
/// <returns></returns>
66+
private HttpRequestMessage AddDollarSignToQueryParameters(HttpRequestMessage request)
5667
{
5768
if (request.RequestUri.Segments[1].ToLower().Contains("v1.0") && request.RequestUri.Query != null)
5869
{
@@ -65,12 +76,19 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
6576
newRequestQuery = newRequestQuery.Replace(targetQueryParam, $"${targetQueryParam}");
6677
}
6778

68-
UriBuilder uriBuilder = new UriBuilder(request.RequestUri);
69-
uriBuilder.Query = newRequestQuery;
79+
// Remove leading question mark. NET 4.x doesn't drop the question mark when appending it to Query property of UriBuilder.
80+
if (newRequestQuery.Contains("?"))
81+
{
82+
newRequestQuery = newRequestQuery.Split('?')[1];
83+
}
84+
UriBuilder uriBuilder = new UriBuilder(request.RequestUri)
85+
{
86+
Query = newRequestQuery
87+
};
7088
request.RequestUri = uriBuilder.Uri;
7189
}
7290

73-
return base.SendAsync(request, cancellationToken);
91+
return request;
7492
}
7593
}
7694
}

0 commit comments

Comments
 (0)