【问题标题】:Swagger Codegen IO: Change Service Naming Convention and NicknameSwagger Codegen IO:更改服务命名约定和昵称
【发布时间】:2020-11-18 11:27:49
【问题描述】:

在创建 Angular API 服务代理时,是否有覆盖 Swagger IO CodeGen 命名约定的方法?

https://editor.swagger.io/

它的命名公式为:API + 控制器名称 + 控制器方法 + HTTP 操作。

public apiProductGetProductByProductIdGet(productNumber?: string, observe?: 'body', reportProgress?: boolean): Observable<ProductResponse>;

我们想为我们公司重组/重新排序命名约定。

目前正在将 Net Core 3 API 与 Angular Typescript 相关联。

将接受 CodeGen 的 javascript 答案。

更新可能的解决方案:

如何在 C# 中更改昵称属性?

https://docs.swagger.io/spec.html

"昵称。操作的唯一 id,可供读取输出的工具使用,以便进一步和更轻松地操作。例如,Swagger-Codegen 将使用昵称作为它生成的客户端中操作的方法名称。该值必须是字母数字,并且可以包含下划线。不允许使用空白字符。

"nickname": "addPet",

【问题讨论】:

    标签: c# .net angular typescript swagger-codegen


    【解决方案1】:

    您正在寻找operationId 属性:

    operationId 是一个可选的唯一字符串,用于标识一个 手术。如果提供,这些 ID 在所有操作中必须是唯一的 在您的 API 中描述。

    https://swagger.io/docs/specification/paths-and-operations/

    例子:

    /users:
      get:
        operationId: getUsers
        summary: Gets all users
        ...
      post:
        operationId: addUser
        summary: Adds a new user
        ...
    /user/{id}:
      get:
        operationId: getUserById
        summary: Gets a user by user ID
        ...
    

    如果您使用Swashbuckle,您可以通过以下几种不同的方式指定 operationId:

    [HttpGet("{id:int}", Name = nameof(GetProductById))]
    public IActionResult GetProductById(int id) // operationId = "GetProductById"'
    

    [HttpGet("{id:int}", Name = "GetProductById")]
    public IActionResult GetProductById(int id) // operationId = "GetProductById"'
    

    另见this

    【讨论】:

    • 是的,我可以看出这是一个问题。您可能还会查看 swagger hub 编辑器 UI 是否有任何选项可以根据方法名称或其他内容对端点进行某种大规模更新。我知道 cli 有一些关于前缀/后缀的选项,但如果没有一定程度的手动操作,可能无法让您到达您想要的位置。
    • 嗨,我有另一个问题,我有一堆不同的控制器,它们的方法称为“GetAll”,现在在 swagger io 中,它给出错误,paths./api/Product 处的语义错误。 get.operationId 操作必须具有唯一的 operationId。 ,那么这是否意味着,不同的控制器,Product、Customer、Account,不能有一个名为 Get All 的方法名?它至少生成 swagger codegen 代理,但在 swagger io 中显示错误,我们将代理命名为方法名称
    【解决方案2】:

    您还可以使用标签更改生成的客户端 SDK 服务命名约定(如果您像我一样想防止与客户端服务发生冲突)。

    不用标记为user,让客户端SDK生成服务名称为UserService,您可以使用user-api的标记,生成的库服务将命名为UserApiService

    https://swagger.io/docs/specification/2-0/grouping-operations-with-tags/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      • 2017-08-04
      • 2021-03-13
      • 2016-09-21
      相关资源
      最近更新 更多