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.

Default Operation ID clashes

See original GitHub issue

I’m submitting a…


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Two controllers with the same operation will produce the same operation ID if no ID is provided when using tsc to compile.

@Controller('a')
class A {
    @ApiOperation()
    getOne(){...}
}

@Controller('b')
class B {
    @ApiOperation()
    getOne(){...}
}
// Output truncated 
{
    "paths" : {
        "a" : {"get" : {"operationId": "getOne"}},
        "b" : {"get" : {"operationId": "getOne"}},
    }
}

Expected behavior

Take the name of the controller also, or append a hash

// Output truncated 
{
    "paths" : {
        "a" : {"get" : {"operationId": "a__getOne"}},
        "b" : {"get" : {"operationId": "b__getOne"}},
        // or         
        "a" : {"get" : {"operationId": "getOne_r@nd0mH4$h"}},
        "b" : {"get" : {"operationId": "getOne_abcd12345"}},

    }
}

Minimal reproduction of the problem with instructions

  1. Create two controllers with the same name for a method
  2. Expose the same named method with @ApiOperation() decorator, do not provide an operationId
  3. Use TSC to compile (untested if nest build command works)
  4. View the JSON output

What is the motivation / use case for changing the behavior?

This is causing issues where operationId fields are expected to be globally unique but the value of them isn’t important. I am using the JSON output in an OpenAPI UI generator called redoc. Without the unique operationIds, the links it produces are broken because all links with the same name point to the same endpoint, and this is frequent for my project as I’m using generic controllers.

Environment

Nest version: 6.10.14

For Tooling issues:

  • Node version: 11.15
  • Platform: Mac

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
wafscommented, Feb 20, 2020

Looking at the patch, I feel like this could be customisable behaviour if it were exposed in the settings.

https://github.com/nestjs/swagger/pull/487/files#diff-bb845d0d85e92f24987732c111594fb0R208 is how it decides what the default operation id is if there wasn’t one.

I envision something like


type SwaggerOperationIdStrategy = 
    | 'FUNCTION_NAME' // old behaviour
    | 'CONTROLLER_FUNCTION_NAME'     // default, current behaviour
    | (instance: object, method: Function) => string  // custom behaviour, make your own Id

 new DocumentBuilder()
    .setTitle('Cats example')
    .setDefaultOperationIdStrategy('FUNCTION_NAME')

6reactions
maxfriedmanncommented, Jan 23, 2020

Just upgraded to the latest version and now my method names are unnecessary long when I use e.g. the open api generator. Before, it generated a nice designService.updateImages method based on the operationId, now it is called designService.frontendDesignsControllerUpdateImages ?

Is it possible to turn that off again and use the not-so-unique version? I understand that problem with doubled operationIds (been there, done that) but since you can easily work around that issue by scoping your swagger spec via nestjs modules, I’d really like to have the possibility to get back to the shorter method names…

So for me, https://github.com/nestjs/swagger/pull/487 is clearly a breaking change, have to update all projects that are using our generated api clients!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to customize the value of operationId generated in api ...
This operationId is used while generating clients using swagger-codegen. Does anyone know how to customize it? I know this can be done per...
Read more >
Handling Concurrency Conflicts - EF Core - Microsoft Learn
Managing conflicts when the same data is updated concurrently with Entity Framework Core.
Read more >
OpenAPI Specification - Version 3.0.3 - Swagger
Unique string used to identify the operation. The id MUST be unique among all operations described in the API. The operationId value is...
Read more >
Conflict Detection and Sync - AWS AppSync
Conflict Detection, Conflict Resolution, and Sync operations require a Versioned data ... and an ID of 1a and version of 2 , this...
Read more >
Resolving HA cluster chassis ID conflicts
Once both FortiGates are operating in HA mode with different chassis IDs, they will negotiate to form a cluster, and if their chassis...
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