openapi-generator codegen:Ajv 无法解析对来自 openapi 3.0.0 的 requestBodies 的引用

openapi-generator codegen: Ajv cannot resolve reference to requestBodies from openapi 3.0.0

我使用 openapi-generator 中我自己的 openapi-3.0.0 模板创建了一个服务器存根。 在实现用于创建资源的 API 逻辑时,我引用了在 requestBodies 下声明的组件,如下所示

components:
  requestBodies:
    SamyojyaUser:
      content:
        application/json:
          schema:
           $ref: '#/components/schemas/SamyojyaUser'
        application/xml:
          schema:
           $ref: '#/components/schemas/SamyojyaUser'
    description: Samyojya User object that needs to be added to the system
    required: true

API声明如下

paths:
  /samyojya-user:
    post:
      operationId: addSamyojyaUser
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/requestBodies/SamyojyaUser'
              x-content-type: application/json

但是,在处理请求时 Ajv 抱怨说

can't resolve reference #/components/requestBodies/SamyojyaUser from id #

注册 requestBodies 组件似乎存在一些问题。我在调试时看到其他组件出现在 Ajv 中。我很想直接在路径对象上使用用户组件,但我想进一步自定义请求正文。关于进一步调整它的任何想法?

#/components/requestBodies/...的引用只能在requestBody下直接使用,不能在schema下使用:

paths:
  /samyojya-user:
    post:
      operationId: addSamyojyaUser
      requestBody:
        $ref: '#/components/requestBodies/SamyojyaUser'

此外,请求正文组件中的缩进错误 - descriptionrequired: true 必须与 content 处于同一级别。

components:
  requestBodies:
    SamyojyaUser:
      content:
        application/json:
          schema:
           $ref: '#/components/schemas/SamyojyaUser'
        application/xml:
          schema:
           $ref: '#/components/schemas/SamyojyaUser'

      # vvvv  Note the indentation level
      description: Samyojya User object that needs to be added to the system
      required: true

在生成代码之前使用 https://editor.swagger.io(或其他验证器)验证您的 OpenAPI 定义。