部署端点时遇YAMLException:无法转储Undefined类型对象
问题背景
部署POST /favorites/add端点时触发YAML解析错误,错误信息:
YAMLException: unacceptable kind of an object to dump [object Undefined]
日志提示错误由名为NAME的对象引发,但在提供的YAML配置、控制器及JSON Schema文件中均未找到该对象。userId存储在请求的JWT Token中,对应的YAML配置如下:
Method: POST Path: /favorites/add Description: | Allows users to add favorite pages to their profile. This inserts a new favorite into the database, which can be queried later through the getFavorites endpoint. Enhances user experience by allowing quick access to favorite pages. Integration: Uri: https://${opt:authority}/{Version}/favorites/add RequestParameters: integration.request.path.Version: stageVariables.Version integration.request.header.X-API-Path: "'/favorites/add'" # 修正HTML实体转义问题 Parameters: Authorization: | Requires a valid, unexpired JSON web token for authentication. Content-Type: | Request body must be `application/json`. RequestModel: $schema: "http://json-schema.org/draft-07/schema#" type: object title: AddFavoriteRequest required: - favorite properties: favorite: type: string description: Identifier of the favorite item to be added. RequestSamples: - favorite: "Allowances" # 改用YAML块级格式,避免流式解析问题 Responses: "200": Description: Returned on success. Model: $schema: "http://json-schema.org/draft-07/schema#" type: object properties: message: type: string description: A message indicating the successful addition of a favorite. userid: type: string description: Echoes back the userid for confirmation. favorite: type: string description: Echoes back the favorite added. Samples: - message: "Favorite added successfully" userid: "user123" favorite: "Allowances" # 改用块级格式 "400": Model: StandardErrorModel Description: Returned on a bad request, such as missing required fields. Samples: - Message: "Bad Request - Missing required fields" "500": Model: StandardErrorModel Description: General server error, typically due to database issues or processing errors. Samples: - Message: "Error processing request"
排查与修复步骤
1. 修正YAML中的HTML实体转义问题
原配置中integration.request.header.X-API-Path: "'/favorites/add'"使用了HTML实体",这会导致YAML解析器无法正确识别引号,进而产生未定义对象。直接替换为YAML原生引号:
integration.request.header.X-API-Path: "'/favorites/add'"
2. 统一RequestSamples/Samples的格式
原配置中流式写法(- { "favorite": "Allowances" })在部分YAML解析器中可能因换行或缩进问题触发解析错误,改用更稳定的块级格式:
RequestSamples: - favorite: "Allowances"
3. 验证StandardErrorModel的定义
400/500响应中引用了StandardErrorModel,但未提供该模型的具体配置。如果这个模型未定义或存在缺失字段(比如隐含依赖NAME字段),会导致解析时出现[object Undefined]错误。需要确认:
StandardErrorModel是否在其他配置文件中正确定义- 模型的字段是否完整,不存在未定义的引用
4. 检查变量替换的有效性
Uri: https://${opt:authority}/{Version}/favorites/add中的${opt:authority}是Serverless Framework的变量占位符,如果部署时未传入该参数,或参数值未定义,会导致解析器生成未定义对象,部分工具会用NAME作为未定义变量的占位符。解决方式:
- 部署时通过
--authority参数传入有效值,比如:serverless deploy --authority your-api-domain.com - 在serverless.yml中配置默认值:
opt:authority, 'default-domain.com'
5. 排查后端控制器的userId处理逻辑
虽然YAML中未直接涉及NAME,但日志提示的NAME可能来自后端从Token提取userId的逻辑。检查控制器代码:
- 是否存在拼写错误(比如将
userId误写为NAME) - Token解析后是否正确赋值给变量,未出现未定义的
NAME变量引用
内容的提问来源于stack exchange,提问作者Jakew9697

