Azure API管理:验证具有可为null属性的内容



我想使用验证内容策略验证API管理中的JSON请求正文。

某些JSON属性应支持null值。因此,我在OpenApi中指定了这一点:
nullable:true

但API管理层忽略了这一点。如何使用null值验证正文?

政策:

<inbound>
<validate-content unspecified-content-type-action="prevent" max-size="102400" size-exceeded-action="prevent" errors-variable-name="requestBodyValidation">
<content type="application/json" validate-as="json" action="prevent" />
</validate-content>
<base />
</inbound>

OpenApi:

paths:
/sample:
post:
summary: Sample
description: Sample
operationId: sample
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Customer'
responses:
'200':
description: OK
components:
schemas:
Customer:
type: object
properties:
id:
type: integer
format: int64
example: '100000.0'
username:
type: string
example: fehguy
country:
type: string
nullable: true
example: Lorem Ipsum
example:
id: '123.0'
username: fehguy
country: Lorem Ipsum

请求主体:

{
"id":123,
"username":"fehguy",
"country": null
}

响应主体:

{
"statusCode": 400,
"message": "Body of the request does not conform to the definition Customer, which is associated with the content type application/json. Invalid type. Expected String but got Null. Line: 4, Position: 19" 
}

微软正在进行修复:

工程团队正在进行修复,但还没有明确的日期

解决方法:

country:
anyOf:
- type: "string"
- type: "null"
example: Lorem Ipsum

最新更新