rest-guide icon indicating copy to clipboard operation
rest-guide copied to clipboard

Make operationId mandatory

Open pvdbosch opened this issue 1 year ago • 3 comments

Add a guideline that operationId MUST be present for each operation:

  • This operationId is used by code generation tools; using it will avoid warnings and bad method names: Empty operationId found for path: get /someResource. Renamed to auto-generated operationId: someResourceGet
  • other tooling may use it as well (e.g. the api gateway we're using)

Further guidelines in the OpenAPI spec about the operationId seem sufficiently clear (we refer or copy them):

Unique string used to identify the operation. The id MUST be unique among all operations described in the API. The operationId value is case-sensitive. Tools and libraries MAY use the operationId to uniquely identify an operation, therefore, it is RECOMMENDED to follow common programming naming conventions.

pvdbosch avatar Apr 17 '24 15:04 pvdbosch

I'm wondering about the "RECOMMENDED to follow common programming naming conventions".

Doesn't that mean it would depend on the targeted programming language:

  • java: lowerCamelCase, e.g. getEnterprise
  • C#: UpperCamelCase, e.g. GetEnterprise
  • python: snake_case, e.g. get_enterprise

Or do code generators generally take care of converting operationId getEnterprise to GetEnterprise for C#, and get_enterprise for python?

jpraet avatar Apr 18 '24 14:04 jpraet

openapi-generator seems to convert to language-specific conventions, tested with python-flask,

e.g. operationId: getOneOfTwoStrings => def get_one_of_two_strings() it also generates an openapi.yaml file with the operationId replaced this converted method name

Works for other elements as well (without modification in the openapi), e.g.:

Parameter name 'paramObject' is not consistent with Python variable names. It will be replaced by 'param_object'

pvdbosch avatar Apr 19 '24 12:04 pvdbosch

WG: OK to mandate operationId. Prefer to have lowerCamelCase as naming convention, consistent with property naming convention.

pvdbosch avatar May 29 '24 12:05 pvdbosch