我使用swagger-codegen在javascript中构建服务api。当调用工作时,它返回我自定义的类型和状态200,但是当有验证错误时,它会返回错误,但也会返回状态200.
例如:
{
"message": "Request validation failed: Parameter (model) is required",
"code": "REQUIRED",
"failedValidation": true,
"path": [
"paths",
"/make/ford",
"get",
"parameters",
"0"
],
"paramName": "model"
}
我希望它返回状态代码400,因为我的swagger.yml仅指定我的用户定义类型作为响应。如何使生成的代码返回状态400以获得验证错误?
端点描述如下(部分文件):
/make/ford:
x-swagger-router-controller: controller
get:
description: Description
operationId: get_car
parameters:
- name: model
in: query
description: Model
required: true
type: array
items:
type: string
responses:
"200":
description: Success
schema:
$ref: "#/definitions/ModelResponse"
# responses may fall through to errors
default:
description: Error
schema:
$ref: "#/definitions/ErrorResponse"
definitions:
ModelResponse:
properties:
options:
type: array
items:
type: string
ErrorResponse:
required:
- message
properties:
message:
type: string
注意:AWS API Gateway不支持默认响应,因此我可以将其解除。