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

feat(swagger): Provide URI version to operationIdFactory #1949

Merged

Conversation

MichaelMarner
Copy link
Contributor

@MichaelMarner MichaelMarner commented Jun 21, 2022

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.

The implementation here gets the versions that the method is for, and generates the URI prefixes. It then looks for the version prefixes in the paths returned from RoutePathFactory, and provides that information to operationIdFactory, so that unique IDs can be generated.

I'm not super familiar with Nest internals, but this looks like the only real way to implement this without making more extensive changes to nest-router, which would have potentially wider side effects.

Closes #1711

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

When using Nest's API versioning feature, and having a controller method cover multiple versions, nestjs-swagger generates invalid OpenAPI spec, due to duplicate operation IDs.

It generates duplicate operation IDs because only the controller and method names are available to operationIdFactory - but in these cases the same controller and method is used multiple times.

Issue Number: #1711

What is the new behavior?

When URI versioning is used, an additional parameter containing the version is provided to operationIdFactory. This allows developers to provide unique operation IDs in the case of a method being used in multiple versions.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

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.

Closes nestjs#1711
@MichaelMarner MichaelMarner force-pushed the 1711-operation-ids-with-version branch from bf46f02 to fd11b46 Compare June 21, 2022 03:46
Copy link

@hebertcisco hebertcisco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good so far. What does it take to be merged?

@altais
Copy link

altais commented Jan 17, 2023

Would love to have this feature too, what does it need to be merged?

@kamilmysliwiec
Copy link
Member

lgtm

@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Feb 6, 2023

I had to revert this PR due to failing tests

image

@viktorhajer
Copy link

Hi everyone!

Any update on this issue? It looks like it was merged, but accoring to the last comment the change was reverted because of the tests. Do you have any information about the delivery? Thanks in advance.

deongroenewald added a commit to deongroenewald/nestjs-swagger that referenced this pull request 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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Duplicate operationIds when using nestjs Version feature
5 participants