Skip to content

Commit 6836d78

Browse files
authored
Merge pull request #2317 from microsoftgraph/2306-find-mggraphcommand-uri-not-working-for-me-endpoints
Translates /me to /users/{user-id}
2 parents 31d1c27 + 06637b8 commit 6836d78

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/Authentication/Authentication/custom/Find-MgGraphCommand.ps1

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,33 @@ Function Find-MgGraphCommand {
166166

167167
$Result = @()
168168
Write-Debug "Received URI: $Uri."
169+
169170
$Uri = GraphUri_RemoveNamespaceFromActionFunction $Uri
170171
$GraphUri = GraphUri_ConvertStringToUri $Uri
171172

172173
# Use API version in URI if -ApiVersion is not provided.
173174
if ([System.String]::IsNullOrWhiteSpace($ApiVersion) -and ($GraphUri.OriginalString -match "(v1.0|beta)\/")) {
174175
$ApiVersion = $Matches[1]
175176
}
177+
178+
176179

177180
if (!$GraphUri.IsAbsoluteUri) {
178181
$GraphUri = GraphUri_ConvertRelativeUriToAbsoluteUri -Uri $GraphUri -ApiVersion $ApiVersion
179182
}
183+
184+
$ContainsMeSegment = $False
185+
$Segment = $GraphUri.Segments
186+
foreach ($s in $Segment) {
187+
if ($s.StartsWith("me")) {
188+
$ContainsMeSegment = $True
189+
break
190+
}
191+
}
192+
if ($ContainsMeSegment) {
193+
$GraphUri = $GraphUri.AbsoluteUri.Replace("/me/", "/users/{id}/")
194+
}
180195
Write-Debug "Resolved URI: $GraphUri."
181-
182196
return $GraphUri
183197
}
184198

@@ -187,7 +201,6 @@ Function Find-MgGraphCommand {
187201
[Parameter(Mandatory = $true, Position = 0)]
188202
[System.Uri]$Uri
189203
)
190-
191204
$Result = @()
192205
$TokenizedUri = GraphUri_TokenizeIds $Uri
193206
Write-Debug "Tokenized URI: $TokenizedUri."

src/Authentication/Authentication/test/Find-MgGraphCommand.Tests.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,30 @@ Describe "Find-MgGraphCommand Command" {
138138
$MgCommand.Command | Should -Be @("Get-MgReportSharePointActivityUserCount", "Get-MgBetaReportSharePointActivityUserCount")
139139
} | Should -Not -Throw
140140
}
141+
It 'Should find commands for uri woth /me segments' {
142+
{
143+
$MgCommand = Find-MgGraphCommand -Uri "/me/events/"
144+
$MgCommand | Should -HaveCount 4
145+
$MgCommand.Command | Select-Object -Unique | should -HaveCount 4
146+
$MgCommand.Method | Select-Object -Unique | should -HaveCount 2
147+
$MgCommand.APIVersion | Select-Object -Unique | should -HaveCount 2
148+
$MgCommand.Variants | Select-Object -Unique | should -HaveCount 5
149+
$MgCommand.URI | Select-Object -Unique | Should -Be "/users/{user-id}/events"
150+
$MgCommand.Command | Select-Object -Unique | Should -BeIn @("New-MgUserEvent", "Get-MgUserEvent", "New-MgBetaUserEvent", "Get-MgBetaUserEvent")
151+
} | Should -Not -Throw
152+
}
153+
It 'Should find commands for uri woth /me segments' {
154+
{
155+
$MgCommand = Find-MgGraphCommand -Uri "https://graph.microsoft.com/v1.0/me/events/"
156+
$MgCommand | Should -HaveCount 4
157+
$MgCommand.Command | Select-Object -Unique | should -HaveCount 4
158+
$MgCommand.Method | Select-Object -Unique | should -HaveCount 2
159+
$MgCommand.APIVersion | Select-Object -Unique | should -HaveCount 2
160+
$MgCommand.Variants | Select-Object -Unique | should -HaveCount 5
161+
$MgCommand.URI | Select-Object -Unique | Should -Be "/users/{user-id}/events"
162+
$MgCommand.Command | Select-Object -Unique | Should -BeIn @("New-MgUserEvent", "Get-MgUserEvent", "New-MgBetaUserEvent", "Get-MgBetaUserEvent")
163+
} | Should -Not -Throw
164+
}
141165
}
142166

143167
Context "FindByCommand" {

0 commit comments

Comments
 (0)