mcl*_*129 5 powershell json swagger
我正在尝试在 Powershell 中更新 Swagger JSON 文档。如果对象上不存在一些属性和值,我需要添加它们。
执行此操作的代码非常简单:
$swaggerDoc = (Get-Content $filePath -raw | ConvertFrom-Json)
$swaggerDoc | Add-Member -Name host -MemberType NoteProperty -Value "swagger.io" -Force
$swaggerDoc | Add-Member -Name schemes -MemberType NoteProperty -Value @("https") -Force
$swaggerDoc | ConvertTo-Json | Set-Content $filePath
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我将它保存回文件时,JSON 会被完全破坏:例如
"get": {
"tags": [
"Links"
],
"operationId": "Links_GetAll",
"parameters": [],
"responses": {
"200": {
"description": "Returns all the available links in the system",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/AdministrativeLink"
}
},
"x-nullable": true
}
}
}
Run Code Online (Sandbox Code Playgroud)
变成
"get": "@{tags=System.Object[]; operationId=Links_GetAll; parameters=System.Object[]; responses=}",
Run Code Online (Sandbox Code Playgroud)
我还没有看到任何其他关于如何在 Powershell 中执行此操作的示例,是否缺少某些语法或参数以保留原始格式?
使用时ConvertTo-Json,请-Depth保留正确的格式JSON。
例如:
$swaggerDoc | ConvertTo-Json -Depth 10 | Set-Content $filePath
Run Code Online (Sandbox Code Playgroud)
我还必须在 JSON 周围添加括号,使其成为 powershell 可读的有效 JSON。