首页 > 解决方案 > Swagger Codegen IO:更改服务命名约定和昵称

问题描述

在创建 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#.netangulartypescriptswagger-codegen

解决方案


您正在寻找的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"'

参阅


推荐阅读