Conversation
…iple underscores Use the first underscore as the separator in MultipleClientsFromOperationIdOperationNameGenerator: - GetClientName: everything before the first '_' = client name - GetOperationName: everything after the first '_' = operation name This guarantees uniqueness since OpenAPI requires globally unique operation IDs. Previously, 'Orders_items_get' and 'Products_items_get' both produced client='items', operation='get', causing duplicate generated code. Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a long-standing bug in MultipleClientsFromOperationIdOperationNameGenerator where operation IDs with multiple underscores (e.g., Orders_items_get and Products_items_get) would map to duplicate (client, operation) pairs, causing duplicate generated classes and methods. The fix simplifies the splitting strategy to always use the first underscore as the delimiter — everything before it becomes the client name, everything after becomes the operation name. Since OpenAPI mandates globally unique operation IDs, this guarantees uniqueness of (client, operation) pairs.
Changes:
GetClientName: Returns everything before the first underscore (instead of the second-to-last segment)GetOperationName: Returns everything after the first underscore (instead of the last segment)- Test updates and additions to cover the new behavior and regression-test uniqueness guarantees
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/NSwag.CodeGeneration/OperationNameGenerators/MultipleClientsFromOperationIdOperationNameGenerator.cs |
Simplified GetClientName and GetOperationName to split on the first underscore instead of the last |
src/NSwag.CodeGeneration.Tests/CodeGenerationTests.cs |
Updated existing test expectations to match new behavior, added operation-name tests for MultipleClientsFromOperationId, and added regression tests asserting uniqueness of (client, operation) pairs |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
MultipleClientsFromOperationIdOperationNameGeneratorused the second-to-last underscore segment as client name and last segment as operation name. Any two operation IDs sharing the same suffix (e.g.Orders_items_getandProducts_items_get) mapped to the same(client="items", operation="get")pair, producing duplicate generated classes and methods.Changes
GetClientName: switched from second-to-last segment to everything before the first_GetOperationName: switched fromLastIndexOf('_')toIndexOf('_')— returns everything after the first_OperationId_TestOperationId→TestOperationId→TestOrders_items_getitems→getOrders→items_getProducts_items_getitems→getProducts→items_get✅Since OpenAPI mandates globally unique operation IDs, splitting at the first
_guarantees that no two operation IDs can ever produce the same(client, operation)pair. The fix also propagates toMultipleClientsFromFirstTagAndOperationNameGenerator, which inheritsGetOperationNamefrom the base class.Tests
MultipleClientsFromOperationId(client, operation)pairsOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.