-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathTweakOpenApi.ps1
76 lines (68 loc) · 2.86 KB
/
TweakOpenApi.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string] $OpenAPIFilesPath,
[Parameter(Mandatory = $false)]
[Switch] $SetNavigationPropertiesAsReadOnly
)
$prepositionReplacements = @{
By = "GraphBPre"
With = "GraphWPre"
At = "GraphAPre"
For = "GraphFPre"
Of = "GraphOPre"
#Or = "GraphRPre" Handling of this preposition needs to be reviewed
}
$wordReplacements = @{
Deltum = "delta"
Quotum = "quota"
Statistic = "statistics"
}
$targetOperationIdRegex = [Regex]::new("([a-z*])($($prepositionReplacements.Keys -join "|"))([A-Z*]|$)", "Compiled")
$stopwatch = [system.diagnostics.stopwatch]::StartNew()
# Tweak prepositions in operationIds to byPass https://github.com/Azure/autorest.powershell/issues/795.
Get-ChildItem -Path $OpenAPIFilesPath | ForEach-Object {
$filePath = $_.FullName
$modified = $false
$updatedContent = Get-Content $filePath | ForEach-Object {
if ($_.contains("operationId:")) {
$operationId = $_
$wordReplacements.Keys | ForEach-Object {
if ($operationId.EndsWith($_, "CurrentCultureIgnoreCase")) {
$operationId = ($operationId -replace $_, $wordReplacements[$_])
$modified = $true
Write-Debug "$_ -> $operationId".Trim()
}
}
if (($targetOperationIdRegex.Match($_)).Success) {
$prepositionReplacements.Keys | ForEach-Object {
# Replace prepositions with replacement word.
#e.g., 'applications_GetCreatedOnBehalfOfByRef' will be renamed to 'applications_GetCreatedOnBehalfGraphOPreGraphBPreRef'.
$operationId = ($operationId -creplace $_, $prepositionReplacements[$_])
$modified = $true
Write-Debug "$_ -> $operationId".Trim()
}
}
return $operationId
}
if ($SetNavigationPropertiesAsReadOnly.IsPresent -and $_.contains("x-ms-navigationProperty: true")) {
# Mark navigation properties as readOnly.
$navigationPropertyExtension = ($_ -replace "x-ms-navigationProperty", "readOnly")
$modified = $true
return $navigationPropertyExtension
}
if ($_ -match "'2\d\d':") {
# Replace '2\d\d' with '2xx' to avoid status code mismatch errors.
$newStatusCode = ($_ -replace $Matches[0], "2XX:")
$modified = $true
return $newStatusCode
}
return $_
}
if ($modified) { $updatedContent | Out-File $filePath -Force }
}
$stopwatch.Stop()
Write-Debug "Tweaked '$OpenAPIFilesPath' OpenAPI files in '$($stopwatch.Elapsed.TotalMinutes)' minutes."