Skip to content

Commit 54920ff

Browse files
authored
[csharp][generichost] Better handling of duplicate operation ids (#19913)
* fixed missing output * bug fix * add new sample * build samples again * delete sample * move the sample and add to github workflow * remove model not needed for this test * handle specs with no models * better handling of duplicate operation ids * fixed mistake in resolving conflicts * build samples again * revert a change sent in another pr
1 parent f98073d commit 54920ff

File tree

11 files changed

+110
-220
lines changed

11 files changed

+110
-220
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5701,8 +5701,6 @@ protected void addHeaders(ApiResponse response, List<CodegenProperty> properties
57015701
}
57025702
}
57035703

5704-
private final Map<String, Integer> seenOperationIds = new HashMap<String, Integer>();
5705-
57065704
/**
57075705
* Add operation to group
57085706
*
@@ -5723,18 +5721,13 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
57235721
}
57245722
// check for operationId uniqueness
57255723
String uniqueName = co.operationId;
5726-
int counter = seenOperationIds.getOrDefault(uniqueName, 0);
5727-
while (seenOperationIds.containsKey(uniqueName)) {
5728-
uniqueName = co.operationId + "_" + counter;
5729-
counter++;
5730-
}
5724+
int counter = 0;
57315725
for (CodegenOperation op : opList) {
57325726
if (uniqueName.equals(op.operationId)) {
57335727
uniqueName = co.operationId + "_" + counter;
57345728
counter++;
57355729
}
57365730
}
5737-
seenOperationIds.put(co.operationId, counter);
57385731
if (!co.operationId.equals(uniqueName)) {
57395732
LOGGER.warn("generated unique operationId `{}`", uniqueName);
57405733
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ private void postProcessResponseCode(CodegenResponse response, String status, Se
823823
httpStatusesWithReturn.add(status);
824824
}
825825
}
826+
private HashMap<String, String> duplicateOf = new HashMap<String, String>();
826827

827828
@Override
828829
@SuppressWarnings("unchecked")
@@ -842,6 +843,12 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
842843
if (operations != null) {
843844
List<CodegenOperation> ops = operations.getOperation();
844845
for (CodegenOperation operation : ops) {
846+
String duplicates = duplicateOf.get(operation.operationId);
847+
if (duplicates != null) {
848+
operation.vendorExtensions.put("x-duplicates", duplicates);
849+
} else {
850+
duplicateOf.put(operation.operationId, operations.getClassname());
851+
}
845852
if (operation.responses != null) {
846853
for (CodegenResponse response : operation.responses) {
847854

modules/openapi-generator/src/main/resources/csharp/libraries/generichost/api.mustache

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ namespace {{packageName}}.{{apiPackage}}
8585
{{/operation}}
8686
}
8787
{{#operation}}
88+
{{^vendorExtensions.x-duplicates}}
8889
{{#responses}}
8990
{{#-first}}
9091

@@ -115,6 +116,7 @@ namespace {{packageName}}.{{apiPackage}}
115116
}
116117
{{/-first}}
117118
{{/responses}}
119+
{{/vendorExtensions.x-duplicates}}
118120
{{/operation}}
119121

120122
/// <summary>
@@ -134,7 +136,7 @@ namespace {{packageName}}.{{apiPackage}}
134136
/// </summary>
135137
public event EventHandler<ExceptionEventArgs>{{nrt?}} OnError{{operationId}};
136138

137-
internal void ExecuteOn{{operationId}}({{classname}}.{{operationId}}ApiResponse apiResponse)
139+
internal void ExecuteOn{{operationId}}({{#vendorExtensions.x-duplicates}}{{.}}{{/vendorExtensions.x-duplicates}}{{^vendorExtensions.x-duplicates}}{{classname}}{{/vendorExtensions.x-duplicates}}.{{operationId}}ApiResponse apiResponse)
138140
{
139141
On{{operationId}}?.Invoke(this, new ApiResponseEventArgs(apiResponse));
140142
}
@@ -619,9 +621,9 @@ namespace {{packageName}}.{{apiPackage}}
619621
{
620622
string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync({{#net60OrLater}}cancellationToken{{/net60OrLater}}).ConfigureAwait(false);
621623

622-
ILogger<{{operationId}}ApiResponse> apiResponseLoggerLocalVar = LoggerFactory.CreateLogger<{{operationId}}ApiResponse>();
624+
ILogger<{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse> apiResponseLoggerLocalVar = LoggerFactory.CreateLogger<{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse>();
623625

624-
{{operationId}}ApiResponse apiResponseLocalVar = new{{^net60OrLater}} {{operationId}}ApiResponse{{/net60OrLater}}(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "{{{path}}}", requestedAtLocalVar, _jsonSerializerOptions);
626+
{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse apiResponseLocalVar = new{{^net60OrLater}} {{operationId}}ApiResponse{{/net60OrLater}}(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "{{{path}}}", requestedAtLocalVar, _jsonSerializerOptions);
625627

626628
After{{operationId}}DefaultImplementation({{#lambda.joinWithComma}}apiResponseLocalVar {{#allParams}}{{paramName}} {{/allParams}}{{/lambda.joinWithComma}});
627629

@@ -680,6 +682,7 @@ namespace {{packageName}}.{{apiPackage}}
680682
}
681683
{{/lambda.trimLineBreaks}}
682684
}
685+
{{^vendorExtensions.x-duplicates}}
683686
{{#responses}}
684687
{{#-first}}
685688

@@ -792,6 +795,7 @@ namespace {{packageName}}.{{apiPackage}}
792795
}
793796
{{/-first}}
794797
{{/responses}}
798+
{{/vendorExtensions.x-duplicates}}
795799
{{/operation}}
796800
}
797801
{{/operations}}

samples/client/petstore/csharp/generichost/latest/Tags/docs/apis/APIKEYSApi.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ All URIs are relative to *http://app.files.com/api/rest/v1*
44

55
| Method | HTTP request | Description |
66
|--------|--------------|-------------|
7-
| [**GetApiKeysId_1**](APIKEYSApi.md#getapikeysid_1) | **GET** /api_keys/{id} | Show API Key |
7+
| [**GetApiKeysId**](APIKEYSApi.md#getapikeysid) | **GET** /api_keys/{id} | Show API Key |
88

9-
<a id="getapikeysid_1"></a>
10-
# **GetApiKeysId_1**
11-
> void GetApiKeysId_1 (int id)
9+
<a id="getapikeysid"></a>
10+
# **GetApiKeysId**
11+
> void GetApiKeysId (int id)
1212
1313
Show API Key
1414

@@ -24,7 +24,7 @@ using Org.OpenAPITools.Model;
2424

2525
namespace Example
2626
{
27-
public class GetApiKeysId_1Example
27+
public class GetApiKeysIdExample
2828
{
2929
public static void Main()
3030
{
@@ -36,11 +36,11 @@ namespace Example
3636
try
3737
{
3838
// Show API Key
39-
apiInstance.GetApiKeysId_1(id);
39+
apiInstance.GetApiKeysId(id);
4040
}
4141
catch (ApiException e)
4242
{
43-
Debug.Print("Exception when calling APIKEYSApi.GetApiKeysId_1: " + e.Message);
43+
Debug.Print("Exception when calling APIKEYSApi.GetApiKeysId: " + e.Message);
4444
Debug.Print("Status Code: " + e.ErrorCode);
4545
Debug.Print(e.StackTrace);
4646
}
@@ -49,18 +49,18 @@ namespace Example
4949
}
5050
```
5151

52-
#### Using the GetApiKeysId_1WithHttpInfo variant
52+
#### Using the GetApiKeysIdWithHttpInfo variant
5353
This returns an ApiResponse object which contains the response data, status code and headers.
5454

5555
```csharp
5656
try
5757
{
5858
// Show API Key
59-
apiInstance.GetApiKeysId_1WithHttpInfo(id);
59+
apiInstance.GetApiKeysIdWithHttpInfo(id);
6060
}
6161
catch (ApiException e)
6262
{
63-
Debug.Print("Exception when calling APIKEYSApi.GetApiKeysId_1WithHttpInfo: " + e.Message);
63+
Debug.Print("Exception when calling APIKEYSApi.GetApiKeysIdWithHttpInfo: " + e.Message);
6464
Debug.Print("Status Code: " + e.ErrorCode);
6565
Debug.Print(e.StackTrace);
6666
}

samples/client/petstore/csharp/generichost/latest/Tags/docs/apis/APIKeys0Api.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ All URIs are relative to *http://app.files.com/api/rest/v1*
44

55
| Method | HTTP request | Description |
66
|--------|--------------|-------------|
7-
| [**GetApiKeysId_0**](APIKeysApi.md#getapikeysid_0) | **GET** /api_keys/{id} | Show API Key |
7+
| [**GetApiKeysId**](APIKeysApi.md#getapikeysid) | **GET** /api_keys/{id} | Show API Key |
88

9-
<a id="getapikeysid_0"></a>
10-
# **GetApiKeysId_0**
11-
> void GetApiKeysId_0 (int id)
9+
<a id="getapikeysid"></a>
10+
# **GetApiKeysId**
11+
> void GetApiKeysId (int id)
1212
1313
Show API Key
1414

@@ -24,7 +24,7 @@ using Org.OpenAPITools.Model;
2424

2525
namespace Example
2626
{
27-
public class GetApiKeysId_0Example
27+
public class GetApiKeysIdExample
2828
{
2929
public static void Main()
3030
{
@@ -36,11 +36,11 @@ namespace Example
3636
try
3737
{
3838
// Show API Key
39-
apiInstance.GetApiKeysId_0(id);
39+
apiInstance.GetApiKeysId(id);
4040
}
4141
catch (ApiException e)
4242
{
43-
Debug.Print("Exception when calling APIKeysApi.GetApiKeysId_0: " + e.Message);
43+
Debug.Print("Exception when calling APIKeysApi.GetApiKeysId: " + e.Message);
4444
Debug.Print("Status Code: " + e.ErrorCode);
4545
Debug.Print(e.StackTrace);
4646
}
@@ -49,18 +49,18 @@ namespace Example
4949
}
5050
```
5151

52-
#### Using the GetApiKeysId_0WithHttpInfo variant
52+
#### Using the GetApiKeysIdWithHttpInfo variant
5353
This returns an ApiResponse object which contains the response data, status code and headers.
5454

5555
```csharp
5656
try
5757
{
5858
// Show API Key
59-
apiInstance.GetApiKeysId_0WithHttpInfo(id);
59+
apiInstance.GetApiKeysIdWithHttpInfo(id);
6060
}
6161
catch (ApiException e)
6262
{
63-
Debug.Print("Exception when calling APIKeysApi.GetApiKeysId_0WithHttpInfo: " + e.Message);
63+
Debug.Print("Exception when calling APIKeysApi.GetApiKeysIdWithHttpInfo: " + e.Message);
6464
Debug.Print("Status Code: " + e.ErrorCode);
6565
Debug.Print(e.StackTrace);
6666
}

samples/client/petstore/csharp/generichost/latest/Tags/src/Org.OpenAPITools.Test/Api/APIKEYSApiTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public APIKEYSApiTests(): base(Array.Empty<string>())
5151
}
5252

5353
/// <summary>
54-
/// Test GetApiKeysId_1
54+
/// Test GetApiKeysId
5555
/// </summary>
5656
[Fact (Skip = "not implemented")]
57-
public async Task GetApiKeysId_1AsyncTest()
57+
public async Task GetApiKeysIdAsyncTest()
5858
{
5959
int id = default!;
60-
await _instance.GetApiKeysId_1Async(id);
60+
await _instance.GetApiKeysIdAsync(id);
6161
}
6262
}
6363
}

samples/client/petstore/csharp/generichost/latest/Tags/src/Org.OpenAPITools.Test/Api/APIKeys0ApiTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public APIKeysApiTests(): base(Array.Empty<string>())
5151
}
5252

5353
/// <summary>
54-
/// Test GetApiKeysId_0
54+
/// Test GetApiKeysId
5555
/// </summary>
5656
[Fact (Skip = "not implemented")]
57-
public async Task GetApiKeysId_0AsyncTest()
57+
public async Task GetApiKeysIdAsyncTest()
5858
{
5959
int id = default!;
60-
await _instance.GetApiKeysId_0Async(id);
60+
await _instance.GetApiKeysIdAsync(id);
6161
}
6262
}
6363
}

0 commit comments

Comments
 (0)