2

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",

2 Answers2

2

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

mwilson
  • 12,295
  • 7
  • 55
  • 95
  • Yea, I could see that being a problem. You might also see if the swagger hub editor UI has any options to do some sort of mass update to the endpoints based on method name or something. I know the cli has some options around prefix/suffix but probably won't get you to where you are wanting me without some level of manual effort. – mwilson Jul 29 '20 at 00:19
  • hi, I have another issue, I we have a bunch of different controllers with methods called, "GetAll", now in swagger io, its giving error, Semantic error at paths./api/Product.get.operationId Operations must have unique operationIds. , so does that mean, different controllers, Product, Customer, Account, cannot have a method name called Get All? it generates the swagger codegen proxies at least, but shows errors in swagger io , we are naming proxies off the method name –  Jul 30 '20 at 05:20
  • I have another question here, https://stackoverflow.com/questions/63166905/swashbuckle-swagger-codegen-io-change-service-naming-convention-without-operati –  Jul 30 '20 at 05:47
0

You can also change the generated client SDK service naming conventions using tags (if you're like me and want to prevent conflict with client services).

Instead of tagging as user, and having the client SDK generate the service name as UserService, you can use the tag of user-api and the generated library service will be named UserApiService.

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

Andrew
  • 1,406
  • 1
  • 15
  • 23