Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate operationId's for routes supporting multiple versions #2537

Closed
2 of 4 tasks
deongroenewald opened this issue Jul 26, 2023 · 1 comment · Fixed by #2549
Closed
2 of 4 tasks

Duplicate operationId's for routes supporting multiple versions #2537

deongroenewald opened this issue Jul 26, 2023 · 1 comment · Fixed by #2549

Comments

@deongroenewald
Copy link
Contributor

deongroenewald commented Jul 26, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Routes that support multiple versions, either by specifying the versions on the controller or the routes themselves, result in duplicate operationId's in the same specification.

Breaking the requirement for operationId's to be unique amongst all operations causes some tools, like code generation, to fail.

The SwaggerModule.createDocument() function does support passing in an operationIdFactory function to customise the generated operationId's and I was hoping it would offer a simple option to workaround the problem.

However, the function is called multiple times with the same controllerKey and methodKey and isn't really provided with enough information about the route/version so would have to do some additional book-keeping of its own to try an generate a unique ID each time which isn't ideal.

Minimum reproduction code

https://github.com/deongroenewald/nestjs-multiversion-duplicate-operation-id

Steps to reproduce

  1. npm start
  2. Navigate to the specification: http://localhost:3000/api-json
  3. Note that there are two routes with the same operationId: AppController_getHello
{
  "openapi": "3.0.0",
  "paths": {
    "/v1": {
      "get": {
        "operationId": "AppController_getHello",
        "parameters": [],
        "responses": {
          "200": {
            "description": ""
          }
        }
      }
    },
    "/v2": {
      "get": {
        "operationId": "AppController_getHello",
        "parameters": [],
        "responses": {
          "200": {
            "description": ""
          }
        }
      }
    }
  },
  "info": {
    "title": "Cats example",
    "description": "The cats API description",
    "version": "1.0",
    "contact": {}
  },
  "tags": [{
    "name": "cats",
    "description": ""
  }],
  "servers": [],
  "components": {
    "schemas": {}
  }
}

Expected behavior

The OpenAPI specification states that the operationId MUST be unique among all operations:

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.

  • Automatically generated operationIds should be guaranteed to be unique
  • operationIdFactory methods should be provided with enough information to distinguish between what version of the route the operationId is being generated for.

Package version

7.1.2

NestJS version

10.0.0

Node.js version

v18.14.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@kamilmysliwiec
Copy link
Member

Ref #1949

deongroenewald added a commit to deongroenewald/nestjs-swagger that referenced this issue Aug 2, 2023
Adds a new optional parameter to operationIdFactory to allow providing the API version prefix added to the operation's path.

This allows the operationIdFactory to generate unique IDs in the case of the same controller method being used for multiple API versions.

Default behavior is unchanged - the operation IDs will be of the form controllerKey_methodKey, but where necessary this can be customised using the URI version prefix.

This is based on the work completed in nestjs#1949. Those changes were reverted due to test failures.

This change adapts those changes to also support neutral versions which resolves the test failures.

Closes nestjs#2537
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants