Search code examples
c#.netangulartypescriptswagger-codegen

Swagger Codegen IO: Change Service Naming Convention and Nickname


Is there anyway to override Swagger IO CodeGen naming conventions, when its creating Angular API Service Proxies?

https://editor.swagger.io/

Its naming by this equation: API + Controller Name + Controller Method + HTTP Action.

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

We want to restructure/reorder the naming convention for our company.

Currently linking Net Core 3 APIs with Angular Typescript.

Will accept javascript answer for CodeGen.

Update Possible Solution:

How do I change the nickname property in C#?

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

"Nickname. A unique id for the operation that can be used by tools reading the output for further and easier manipulation. For example, Swagger-Codegen will use the nickname as the method name of the operation in the client it generates. The value MUST be alphanumeric and may include underscores. Whitespace characters are not allowed.

"nickname": "addPet",

Solution

  • You are looking for the operationId property:

    operationId is an optional unique string used to identify an operation. If provided, these IDs must be unique among all operations described in your API.

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

    Example:

    /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
        ...
    

    If you're using Swashbuckle, you can specify the operationId a couple of different ways below:

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

    or

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

    See this also