当内容为字符串而不是JSON时,Api网关模型验证错误地验证了请求

问题描述

我向我的API网关添加了一个模型。当我发送带有包含字符串(而不是有效JSON)的正文的POST请求时,模型将验证该请求。我期望它会被拒绝。

我已经尝试了这两种显示问题的情况:

  1. 模型将验证“ A_STRING”(行为不正确)
  2. 模型将验证“ A_STRING” {}(行为不正确)
  3. 模型将失败{“ A_STRING”}(正确的行为)

我正在直接从AWS控制台测试API网关(请参阅随附的屏幕截图),以下是一个示例的日志:

Execution log for request b2b825b4-4fb2-43e6-935b-0781264eb5df
Mon Aug 17 21:45:42 UTC 2020 : Starting execution for request: b2b825b4-4fb2-43e6-935b-0781264eb5df
Mon Aug 17 21:45:42 UTC 2020 : HTTP Method: POST,Resource Path: /TrainingNotif
Mon Aug 17 21:45:42 UTC 2020 : Method request path: {}
Mon Aug 17 21:45:42 UTC 2020 : Method request query string: {}
Mon Aug 17 21:45:42 UTC 2020 : Method request headers: {}
Mon Aug 17 21:45:42 UTC 2020 : Method request body before transformations: "A_STRING"
Mon Aug 17 21:45:42 UTC 2020 : Request validation succeeded for content type application/json

如果有帮助,这是我的模特:

{
  "additionalProperties": false,"$schema": "http://json-schema.org/draft-04/schema#","required": 
  [
    "trainingSiteRequester","employeeTrainingList"
  ],"properties": 
  {
    "trainingSiteRequester": 
    {
      "type": "string"
    },"employeeTrainingList": 
    {
      "type": "array","items": 
      {
        "additionalProperties": false,"type": "object","required": 
        [
          "id","trainingURL","dueDate"
        ],"properties": 
        {
          "trainingURL": 
          {
            "type": "string"
          },"dueDate": 
          {
            "type": "string"
          },"id": 
          {
            "type": "integer"
          }
        }
      }
    }
  }
}

为了完整起见,这是我的CloudFormation代码,用于将模型附加到API Gateay:

PostMethod:
  Type: AWS::ApiGateway::Method
  Properties:
    AuthorizationType: AWS_IAM
    HttpMethod: POST
    RequestValidatorId: !Ref TrainingNotificationRequestValidator
    RequestModels:
      application/json: !Ref TrainingNotificationRequestModel
    Integration:
      Credentials: !GetAtt TrainingNotificationsAPIGatewayRole.Arn
      IntegrationHttpMethod: POST
      IntegrationResponses:
        - SelectionPattern: 200
          StatusCode: 200
        - SelectionPattern: 429
          StatusCode: 429
      PassthroughBehavior: NEVER
      RequestParameters:
        integration.request.querystring.Action: "'SendMessage'"
        integration.request.querystring.TopicArn: !Sub "'${ReceivedRequestsSNS}'"
        integration.request.querystring.Message: "method.request.body"
      RequestTemplates:
        application/json: "{\"statusCode\": 200}"
      Type: AWS
      Uri:
        Fn::Join:
          - ""
          - - "arn:aws:apigateway:"
            - Ref: AWS::Region
            - ":sns:action/Publish"
    MethodResponses:
      - StatusCode: 200
      - StatusCode: 429
      - StatusCode: 500
    ResourceId: !Ref TrainingNotificationsAPIGatewayResources
    RestApiId: !Ref TrainingNotificationsAPIGateway

# Request Model
TrainingNotificationRequestModel:
  Type: AWS::ApiGateway::Model
  Properties:
    RestApiId: !Ref TrainingNotificationsAPIGateway
    ContentType: application/json
    Name: TrainingNotificationRequestModel
    Schema:
      $schema: 'http://json-schema.org/draft-04/schema#'
      additionalProperties: false
      required:
        - trainingSiteRequester
        - employeeTrainingList
      properties:
        trainingSiteRequester:
          type: string
        employeeTrainingList:
          type: array
          items:
            type: object
            additionalProperties: false
            properties:
              id:
                type: integer
              trainingURL:
                type: string
              dueDate:
                type: string
            required:
              - id
              - trainingURL
              - dueDate

TrainingNotificationRequestValidator:
  Type: AWS::ApiGateway::RequestValidator
  Properties:
    RestApiId: !Ref TrainingNotificationsAPIGateway
    Name: TrainingNotificationRequestValidator
    ValidateRequestBody: true

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)