question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generated OpenAPI definitions include duplicate operationIds, which violates the OpenAPI spec

See original GitHub issue

Sorting

  • I’m submitting a …

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn’t already been submit

NOTE: This is almost certainly a dup of the closed issue #183, however since there have been several unanswered requests to re-open that issue, I’m going ahead and filing a new one (since it seems those requests were missed).

Expected Behavior

As required by OpenAPI, I expect tsoa spec to generate paths with operationIds that are unique across the entire API definition. However, if two different controllers have the same method name, tsoa spec will generate the same operationId for both, even though they are two distinct paths. Here’s a trivial example which breaks today:

@Route('one')
class FirstController extends Controller {
    @Get()
    async get(): Promise<void> {}
}

@Route('two')
class SecondController extends Controller {
    @Get()
    async get(): Promise<void> {}
}

Current Behavior

Both of the aforementioned methods are given the same operationId of Get in the generated swagger.json:

...
	"paths": {
		"/one": {
			"get": {
				"operationId": "Get",
...
			}
		},
		"/two": {
			"get": {
				"operationId": "Get",
...
			}
		},
	},
...

This violates the OpenAPI spec, which says the following about operationIds (emphasis added):

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.

Possible Solution

I expect each get() method in the TypeScript source to be given a unique operationId which includes the route or controller name, for example “get_one” or “one_get” or something similar (preferably something that is moderately human-readable).

Steps to Reproduce

As above

Context (Environment)

Version of the library: 3.8.0 Version of NodeJS: 15.14.0

  • Confirm you were using yarn not npm: [ ] <-- we use npm exclusively; that’s irrelevant in this case though

Detailed Description

Change the default method of generating operationIds in OpenAPI definitions so that they are unique (and still human-understandable) across the entire generated API definition. This could be accomplished by adding the class name or (preferably) a sanitized form of the route path as a prefix. The operationId should probably conform to standard variable-naming syntax (i.e. avoid special characters like . and -) so it’s easily consumable by code generators for clients.

Another option might be to simply not emit operationIds at all by default, and only emit operationIds for methods with an explicit @OperationId decorator.

Breaking change?

Yes, the proposed change may potentially cause breakage for clients which are doing codegen from OpenAPI definitions generated by Tsoa, since some will rely on the operationId to choose function/method names, and those names would therefore change.

However, this could be avoided by including a backward-compatibility flag in tsoa.json or as an option to tsoa spec. It could also be mitigated manually by adding @OperationIds to all affected methods in application server code.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
joshua-berry-ntnxcommented, Mar 2, 2022

That seems like a good approach to me!

1reaction
joshua-berry-ntnxcommented, Jul 3, 2021

Still relevant; not stale

Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicate operationId values · Issue #361 - GitHub
Currently the operationId for the OpenAPI spec is generated only from the simple method name (if not explicitly set).
Read more >
Checking uniqueness of OperationId when using c ...
OperationId)) throw new NotSupportedException($"There are 2 operations ... an OpenApi spec that violates this unique OperationId constraint.
Read more >
How to deal with Duplicate Operation - MSDN - Microsoft
We have these two operations in the controller and when we import the API ... Swagger definition when you import one, and per...
Read more >
F.A.Q - Springdoc-openapi
How can I define multiple OpenAPI definitions in one Spring Boot project? You can define your own groups of API based on the...
Read more >
Using OpenApiReference To Generate Open API Client Code
I this post I show how you can customise the code generated by the ... your OpenApi definitions in the source API include...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found