You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在serverless.yml中为POST请求配置bodyType(serverless-auto-swagger)

解决serverless-auto-swagger在serverless.yml中配置请求体类型的问题

在serverless.yml(或拆分的functions.yml)的HTTP事件配置中,需要使用**x-swagger-body-type**字段来指定请求体对应的TypeScript接口类型,而不是直接用schema字段——这是serverless-auto-swagger针对YAML配置的专属扩展字段。

修改后的functions.yml配置

createSurvey:
  handler: src/functions/survey/createSurveyFunction.handler
  events:
    - http:
        path: /survey
        method: post
        cors: true
        authorizer:
          name: authorizer
          arn: ${self:custom.userPoolArn}
        x-swagger-body-type: PostISurvey # 指定对应的接口类型

关键注意事项

  1. 修正类型文件路径拼写:你的serverless.yml中typefiles配置的路径是./src/types/surevy.d.ts,存在拼写错误(应为survey.d.ts),这会导致库无法读取到PostISurvey接口定义,务必修正:
autoswagger:
    title: "Survey API Documentation"
    apiType: "http"
    generateSwaggerOnDeploy: true
    typefiles: ["./src/types/survey.d.ts"] # 修正拼写错误
    schemes: ["http"]
    basePath: "/${opt:stage}"
    apiKeyHeaders: ["authorizer"]
  1. 确认接口导出有效性:你的PostISurvey接口已经正确导出,只需保证类型文件路径正确,serverless-auto-swagger就能自动识别并关联到请求体。

完成以上配置后,重新部署或生成Swagger文档时,createSurvey接口的请求体就会自动匹配PostISurvey的类型定义了。

内容的提问来源于stack exchange,提问作者Ramachandran Krishnamohan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 11:42:15