-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwaggerJsonExtractor.ps1
130 lines (117 loc) · 4.31 KB
/
SwaggerJsonExtractor.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
param (
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[string]
$SwaggerJsonString = $null,
[Parameter(Mandatory = $false)]
[string]
$InputPath = $null,
[Parameter(Mandatory = $false)]
[string]
$OutputPath = $null,
[Parameter(Mandatory = $true)]
[array]
$PathsToExtract,
[Parameter(Mandatory = $false)]
[switch]
$CompressOutputJson = $false,
[Parameter(Mandatory = $false)]
[switch]
$SuppressClipboardOutput = $false,
[Parameter(Mandatory = $false)]
[switch]
$SuppressConsoleOutput = $false,
[switch]
$ForceFileOutput = $false
)
function Main {
$swaggerJsonRaw = ""
if (![string]::IsNullOrWhiteSpace($SwaggerJsonString)) {
$swaggerJsonRaw = $SwaggerJsonString
}
elseif ([string]::IsNullOrWhiteSpace($InputPath)) {
Write-Output "No InputPath provided - reading from clipboard"
$swaggerJsonRaw = Get-Clipboard
}
else {
$swaggerJsonRaw = Get-Content -Path $InputPath
}
try {
$swaggerJson = $swaggerJsonRaw | ConvertFrom-Json
}
catch {
Write-Error "Invalid JSON provided"
exit "Invalid JSON provided"
}
$resultJsonObject = [PSCustomObject]@{
openapi = $swaggerJson.openapi
info = $swaggerJson.info
paths = [PSCustomObject]@{}
components = [PSCustomObject]@{schemas = [PSCustomObject]@{} }
}
$schemasToBeTransfered = @{}
$schemasMentionedInPaths = @{}
$PathsToExtract = $PathsToExtract | Get-Unique
$schemaRegEx = ([regex]'"\$ref":[ ]*"#\/components\/schemas\/[^"]*"')
foreach ($path in $PathsToExtract) {
$pathInfo = $null
$pathInfo = $swaggerJson.paths.$path
if ($null -eq $pathInfo) {
Write-Warning "$path not found - SKIPPING"
continue
}
$resultJsonObject.paths | Add-Member -MemberType NoteProperty -Name $path -Value $pathInfo
$schemasFromPathInfosRaw = [array] $schemaRegEx.Matches(($pathInfo | ConvertTo-Json -Depth 99 -Compress)).value
if ($null -ne $schemasFromPathInfosRaw) {
$schemasFromPathInfos = [array] $schemasFromPathInfosRaw | % { $_.Replace('"$ref":"#/components/schemas/', "").Replace('"', "") }
}
foreach ($schemasFromPathInfo in $schemasFromPathInfos) {
if (!$schemasMentionedInPaths.ContainsKey($schemasFromPathInfo)) {
$schemasMentionedInPaths.Add($schemasFromPathInfo, $schemasFromPathInfo)
}
}
}
foreach ($schema in $schemasMentionedInPaths.Keys) {
if (!$schemasToBeTransfered.ContainsKey($schema)) {
$schemasToBeTransfered.Add($schema, $schema)
}
$schemaBody = $null
$schemaBody = $swaggerJson.components.schemas.$schema
if ($null -ne $schemaBody) {
$schemasFromSchemaBodyRaw = [array] $schemaRegEx.Matches(($schemaBody | ConvertTo-Json -Depth 99 -Compress)).value
if ($null -ne $schemasFromSchemaBodyRaw) {
$schemasFromSchemaBody = [array] $schemasFromSchemaBodyRaw | % { $_.Replace('"$ref":"#/components/schemas/', "").Replace('"', "") }
}
}
foreach ($schemaFromSchemaBody in $schemasFromSchemaBody) {
if (!$schemasToBeTransfered.ContainsKey($schemaFromSchemaBody)) {
$schemasToBeTransfered.Add($schemaFromSchemaBody, $schemaFromSchemaBody)
}
}
}
foreach ($schema in $schemasToBeTransfered.Keys) {
$schemaBody = $null
$schemaBody = $swaggerJson.components.schemas.$schema
if ($null -ne $schemaBody) {
$resultJsonObject.components.schemas | Add-Member -MemberType NoteProperty -Name $schema -Value $schemaBody
}
else {
Write-Warning "$schema was not found - SKIPPING"
}
}
$resultJson = $resultJsonObject | ConvertTo-Json -Depth 99 -Compress:$CompressOutputJson
if (!$SuppressClipboardOutput) {
$resultJson | Set-Clipboard
}
if (!$SuppressConsoleOutput) {
Write-Output $resultJson
}
if (![string]::IsNullOrWhiteSpace($OutputPath)) {
if ($ForceFileOutput) {
$resultJson | Out-File -FilePath $OutputPath
}
else {
$resultJson | Out-File -FilePath $OutputPath -NoClobber
}
}
}
Main