lb4 openapi generator produces invalid TypeScript code when OpenAPI parameters contain hyphens

未关闭 适合新手
#11,887 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@aaqilniz 已经在做这个了。

开始于 2024年3月21日。

  • #10427 来自 @aaqilniz —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
node.js, openapi, typescript

调研方向

先阅读 lb4 OpenAPI 生成器中构建控制器方法参数的代码,并追踪它如何将 OpenAPI 规范中的参数名称映射到生成的 TypeScript。使用提供的规范重现问题,并检查生成的控制器。当带连字符的名称在服务调用参数中得到一致的规范化处理,且生成的 TypeScript 可以编译时,即为完成。

由索引模型根据 Issue 内容生成。

描述

bug CLI
Bug Description

When generating server controllers or services using the lb4 openapi generator from an OpenAPI specification containing query or path parameters with hyphens (kebab-case, like limit-number or status-code), the code generator incorrectly emits literal code containing hyphens (e.g., limit-number, status-code) directly inside the TypeScript method body call arguments instead of sanitizing/camelCasing them into valid TypeScript identifiers (limitNumber, statusCode).

This causes immediate compilation/syntax errors.

Steps to Reproduce

Create an OpenAPI 3.0 specification JSON file (openapi.json) with an endpoint having hyphenated query parameters:

{
  "openapi": "3.0.3",
  "info": {
    "title": "Hyphenated Bug API",
    "version": "1.0.0"
  },
  "paths": {
    "/customers": {
      "get": {
        "operationId": "getCustomers",
        "parameters": [
          {
            "name": "limit-number",
            "in": "query",
            "schema": { "type": "integer" }
          },
          {
            "name": "status-code",
            "in": "query",
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": { "description": "OK" }
        }
      }
    }
  }
}
  1. Run the LoopBack OpenAPI generator command pointing to this spec: lb4 openapi openapi.json
  2. Inspect the generated Controller file.
Current Output (Buggy Code)

The controller method tries to pass raw hyphenated strings as variable names into the service layer call:

 async getCustomers(
    @param({
      name: 'limit-number',
      in: 'query',
      // ...
    }) limitNumber: number | undefined, 
    @param({
      name: 'status-code',
      in: 'query',
      // ...
    }) statusCode: string | undefined
  ): Promise<any[]> {
       // BUG: TypeScript treats 'limit-number' and 'status-code' as invalid mathematical subtraction expressions
       return this.openApiService.getCustomers(limit-number, status-code);  
  }
Expected Output

The generated controller should correctly use the sanitized camelCase parameter identifiers (limitNumber, statusCode) inside the implementation body:

async getCustomers(...): Promise<any[]> {
       return this.openApiService.getCustomers(limitNumber, statusCode);  
  }
主要语言
TypeScript
星标
5.1k
派生
1.1k
平均合并
1 天 23 小时
30 天内合并 PR
44

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

loopbackio/loopback-next 的其他 Issue

查看 loopbackio/loopback-next 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。