无法向PropertiesCollection添加System.Int32类型的问题排查
问题分析与解决方案
问题1:布尔属性被自动转为字符串
你手动拼接的JSON中,true是布尔值,但PowerShell的Here-String处理或HTTP请求头配置问题,导致后端将其默认解析为字符串;同时Azure DevOps API在接收未指定类型的属性时,会自动按字符串类型存储。
问题2:手动指定类型后返回properties: null
你的第二个请求体存在JSON语法错误:'$type'使用了单引号,而JSON规范要求所有键必须用双引号包裹。这个错误导致API无法正确解析properties字段,最终返回null。
正确的实现方式
方式1:修正语法,正确指定布尔类型属性
如果需要布尔类型的ComposerDenyStatus,请求体需修正引号格式,并使用正确的类型标识System.Boolean:
$body = @" { "comments": [ { "parentCommentId": 0, "content": "$content", "commentType": 1 } ], "properties": { "ComposerDenyStatus": { "$type": "System.Boolean", "$value": true } }, "status": $status } "@
方式2:用PowerShell对象转JSON避免手动拼接错误
手动拼接JSON极易出现格式问题,推荐通过PowerShell对象自动生成正确的JSON结构:
$bodyObject = [PSCustomObject]@{ comments = @( [PSCustomObject]@{ parentCommentId = 0 content = $content commentType = 1 } ) properties = [PSCustomObject]@{ ComposerDenyStatus = [PSCustomObject]@{ '$type' = 'System.Boolean' '$value' = $true } } status = $status } $body = $bodyObject | ConvertTo-Json -Depth 3
额外注意事项
- 确保请求的
Content-Type头设置为application/json,否则API可能无法正确解析请求体。 - 若需整数类型,将
$type改为System.Int32,$value设为无引号的数字(如"$value": 1)。
内容的提问来源于stack exchange,提问作者Graham Reynolds
相关产品推荐
相关产品推荐

