启用基本请求验证的示例 API 的 OpenAPI 定义 - Amazon API Gateway
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

启用基本请求验证的示例 API 的 OpenAPI 定义

以下 OpenAPI 定义定义了一个启用了请求验证的示例 API。PetStore API 是 API 的一部分。其使用 POST 方法将宠物添加到 pets 集合,并使用 GET 方法按指定类型查询宠物。

在 API 级别的 x-amazon-apigateway-request-validators 映射中声明了两种请求验证程序。params-only 验证程序在 API 上启用,并由 GET 方法继承。该验证程序让 API Gateway 可以验证所需的查询参数 (q1) 包含在传入请求内且不为空。all 验证程序在 POST 方法上启用。该验证程序验证所需的标头参数 (h1) 是否已设置且不为空。它还会验证有效负载格式是否符合指定的 RequestBodyModel。如果未找到匹配的内容类型,则不执行请求验证。使用模型验证正文时,如果找不到匹配的内容类型,则不执行请求验证。要使用同一模型而不考虑内容类型,请指定 $default 作为键。

这种模型要求输入 JSON 对象包含 nametypeprice 属性。name 属性可以是任何字符串,type 必须是一种指定枚举字段 (["dog", "cat", "fish"]),而 price 必须介于 25 和 500 之间。id 参数不是必需参数。

OpenAPI 2.0
{ "swagger": "2.0", "info": { "title": "ReqValidators Sample", "version": "1.0.0" }, "schemes": [ "https" ], "basePath": "/v1", "produces": [ "application/json" ], "x-amazon-apigateway-request-validators" : { "all" : { "validateRequestBody" : true, "validateRequestParameters" : true }, "params-only" : { "validateRequestBody" : false, "validateRequestParameters" : true } }, "x-amazon-apigateway-request-validator" : "params-only", "paths": { "/validation": { "post": { "x-amazon-apigateway-request-validator" : "all", "parameters": [ { "in": "header", "name": "h1", "required": true }, { "in": "body", "name": "RequestBodyModel", "required": true, "schema": { "$ref": "#/definitions/RequestBodyModel" } } ], "responses": { "200": { "schema": { "type": "array", "items": { "$ref": "#/definitions/Error" } }, "headers" : { "test-method-response-header" : { "type" : "string" } } } }, "security" : [{ "api_key" : [] }], "x-amazon-apigateway-auth" : { "type" : "none" }, "x-amazon-apigateway-integration" : { "type" : "http", "uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "httpMethod" : "POST", "requestParameters": { "integration.request.header.custom_h1": "method.request.header.h1" }, "responses" : { "2\\d{2}" : { "statusCode" : "200" }, "default" : { "statusCode" : "400", "responseParameters" : { "method.response.header.test-method-response-header" : "'static value'" }, "responseTemplates" : { "application/json" : "json 400 response template", "application/xml" : "xml 400 response template" } } } } }, "get": { "parameters": [ { "name": "q1", "in": "query", "required": true } ], "responses": { "200": { "schema": { "type": "array", "items": { "$ref": "#/definitions/Error" } }, "headers" : { "test-method-response-header" : { "type" : "string" } } } }, "security" : [{ "api_key" : [] }], "x-amazon-apigateway-auth" : { "type" : "none" }, "x-amazon-apigateway-integration" : { "type" : "http", "uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "httpMethod" : "GET", "requestParameters": { "integration.request.querystring.type": "method.request.querystring.q1" }, "responses" : { "2\\d{2}" : { "statusCode" : "200" }, "default" : { "statusCode" : "400", "responseParameters" : { "method.response.header.test-method-response-header" : "'static value'" }, "responseTemplates" : { "application/json" : "json 400 response template", "application/xml" : "xml 400 response template" } } } } } } }, "definitions": { "RequestBodyModel": { "type": "object", "properties": { "id": { "type": "integer" }, "type": { "type": "string", "enum": ["dog", "cat", "fish"] }, "name": { "type": "string" }, "price": { "type": "number", "minimum": 25, "maximum": 500 } }, "required": ["type", "name", "price"] }, "Error": { "type": "object", "properties": { } } } }