Skip to content

Commit 3ab495a

Browse files
authored
Fix operationId mapping (#20846)
* fix operationId mapping * add tests * minor change
1 parent e40f9e3 commit 3ab495a

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5657,6 +5657,7 @@ protected boolean isReservedWord(String word) {
56575657
*/
56585658
protected String getOrGenerateOperationId(Operation operation, String path, String httpMethod) {
56595659
String operationId = operation.getOperationId();
5660+
56605661
if (StringUtils.isBlank(operationId)) {
56615662
String tmpPath = path;
56625663
tmpPath = tmpPath.replaceAll("\\{", "");
@@ -5681,6 +5682,10 @@ protected String getOrGenerateOperationId(Operation operation, String path, Stri
56815682
LOGGER.warn("Empty operationId found for path: {} {}. Renamed to auto-generated operationId: {}", httpMethod, path, operationId);
56825683
}
56835684

5685+
if (operationIdNameMapping.containsKey(operationId)) {
5686+
return operationIdNameMapping.get(operationId);
5687+
}
5688+
56845689
// remove prefix in operationId
56855690
if (removeOperationIdPrefix) {
56865691
// The prefix is everything before the removeOperationIdPrefixCount occurrence of removeOperationIdPrefixDelimiter
@@ -5693,13 +5698,8 @@ protected String getOrGenerateOperationId(Operation operation, String path, Stri
56935698
operationId = String.join(removeOperationIdPrefixDelimiter, Arrays.copyOfRange(components, component_number, components.length));
56945699
}
56955700
}
5696-
operationId = removeNonNameElementToCamelCase(operationId);
56975701

5698-
if (operationIdNameMapping.containsKey(operationId)) {
5699-
return operationIdNameMapping.get(operationId);
5700-
} else {
5701-
return toOperationId(operationId);
5702-
}
5702+
return toOperationId(removeNonNameElementToCamelCase(operationId));
57035703
}
57045704

57055705
/**

modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,6 +2326,20 @@ public void schemaMapping() {
23262326
Assertions.assertEquals(codegenModel.vars.get(0).getBaseType(), "TypeAlias");
23272327
}
23282328

2329+
@Test
2330+
public void operationIdNameMapping() {
2331+
DefaultCodegen codegen = new DefaultCodegen();
2332+
codegen.operationIdNameMapping.put("edge case !@# 123", "fix_edge_case");
2333+
2334+
OpenAPI openAPI = new OpenAPIParser()
2335+
.readLocation("src/test/resources/3_0/type-alias.yaml", null, new ParseOptions()).getOpenAPI();
2336+
codegen.setOpenAPI(openAPI);
2337+
2338+
CodegenOperation codegenOperation = codegen.fromOperation("/type-alias", "get", openAPI.getPaths().get("/type-alias").getGet(), null);
2339+
Assertions.assertEquals(codegenOperation.operationId, "fix_edge_case");
2340+
Assertions.assertEquals(codegen.getOrGenerateOperationId(openAPI.getPaths().get("/type-alias").getGet(), "/type-alias", "get"), "fix_edge_case");
2341+
}
2342+
23292343
@Test
23302344
public void modelWithPrefixDoNotContainInheritedVars() {
23312345
DefaultCodegen codegen = new DefaultCodegen();

modules/openapi-generator/src/test/resources/3_0/type-alias.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ info:
66
paths:
77
/type-alias:
88
get:
9+
operationId: edge case !@# 123
910
responses:
1011
200:
1112
description: OK

0 commit comments

Comments
 (0)