【问题标题】:Azure API Management: Validate-Content with nullable propertiesAzure API 管理:使用可为空的属性验证内容
【发布时间】:2021-08-09 18:46:07
【问题描述】:

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

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

但 API 管理忽略它。 如何使用空值验证正文?

政策:

<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" 
}

【问题讨论】:

    标签: openapi azure-api-management


    【解决方案1】:

    微软正在努力解决问题:

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

    解决方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多