typescript - Swagger/OpenAPI 客户端代码生成器更改属性名称

标签 typescript interface swagger openapi

我正在开发一个带有 .NET Core 后端和 React(TS 支持)前端的小型 Web 应用程序。

对于 API 定义/文档,我使用的是 OpenAPI,但在为客户端生成代码时遇到了问题。 Bellow 是我用来生成带有 Swagger Editor 的客户端接口(interface)的 yaml 定义片段(来自 swagger.json)。

crmObiskiPartnerjevResponse:
      type: object
      properties:
        id:
          type: integer
          format: int32
        PoslovniPartner:
          type: integer
          format: int32
          nullable: true
        DatumObiska:
          type: string
          format: date-time
          nullable: true
        NamenObiskaId:
          type: integer
          format: int32
          nullable: true
        Uporabnik:
          type: integer
          format: int32
          nullable: true
        Opomba:
          type: string
          nullable: true
        NamenObiskaNaziv:
          type: string
          nullable: true
        UporabnikNaziv:
          type: string
          nullable: true
        PoslovniPartnerNaziv:
          type: string
          nullable: true
      nullable: true

问题是在为 typescript 生成客户端代码时,Swagger 编辑器弄乱了属性的命名。在此示例中,它将第一个字母转换为小型大写字母 (PoslovniPartner -> poslovniPartner)。任何想法将如何解决这个问题将不胜感激。

/**
 * ECE_crmWebAPI
 * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
 *
 * OpenAPI spec version: v1
 * 
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */

export interface CrmObiskiPartnerjevResponse { 
    id?: number;
    poslovniPartner?: number;
    datumObiska?: Date;
    namenObiskaId?: number;
    uporabnik?: number;
    opomba?: string;
    namenObiskaNaziv?: string;
    uporabnikNaziv?: string;
    poslovniPartnerNaziv?: string;
}

最佳答案

请查看 Codegen 常见问题解答: Codegen - FAQ

The JSON response failed to deserialize properly into the object due to change in variable naming (snake_case to camelCase). Is there any way to keep the original naming?

Yes, please use the following option when generating TypeScript clients:

modelPropertyNaming
    Naming convention for the property: 'camelCase', 'PascalCase', 'snake_case' and 'original', which keeps the original name (Default: camelCase)

所以,请将modelPropertyNaming设置为original

关于typescript - Swagger/OpenAPI 客户端代码生成器更改属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59541546/

相关文章:

typescript - Next.js typescript自定义文件报错: TypeError: Class constructor Document cannot be invoked without 'new'

PHP ↔ Perl 接口(interface)或绑定(bind)

c# - 为什么返回集合接口(interface)而不是具体类型?

go - 冲突匹配的嵌入式接口(interface)方法是功能、错误还是其他?

java - 如何将 joda dateTime 转换为 swagger 中的字符串

angular - Typescript 装饰器如何让 Angular 2 发现类型元数据

reactjs - 在 Typescript 中 react 子组件

java - 如何创建分组 Swagger 注释 - 此位置不允许使用注释

yaml - swagger 编辑器中的多行字符串

angularjs - 如何在 ng2-signalr 中设置授权 header ?