Fix operationId mapping (#20846)

* fix operationId mapping

* add tests

* minor change
This commit is contained in:
William Cheng 2025-03-12 20:28:18 +08:00 committed by GitHub
parent e40f9e3801
commit 3ab495a0aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 6 deletions

View File

@ -5657,6 +5657,7 @@ public class DefaultCodegen implements CodegenConfig {
*/
protected String getOrGenerateOperationId(Operation operation, String path, String httpMethod) {
String operationId = operation.getOperationId();
if (StringUtils.isBlank(operationId)) {
String tmpPath = path;
tmpPath = tmpPath.replaceAll("\\{", "");
@ -5681,6 +5682,10 @@ public class DefaultCodegen implements CodegenConfig {
LOGGER.warn("Empty operationId found for path: {} {}. Renamed to auto-generated operationId: {}", httpMethod, path, operationId);
}
if (operationIdNameMapping.containsKey(operationId)) {
return operationIdNameMapping.get(operationId);
}
// remove prefix in operationId
if (removeOperationIdPrefix) {
// The prefix is everything before the removeOperationIdPrefixCount occurrence of removeOperationIdPrefixDelimiter
@ -5693,13 +5698,8 @@ public class DefaultCodegen implements CodegenConfig {
operationId = String.join(removeOperationIdPrefixDelimiter, Arrays.copyOfRange(components, component_number, components.length));
}
}
operationId = removeNonNameElementToCamelCase(operationId);
if (operationIdNameMapping.containsKey(operationId)) {
return operationIdNameMapping.get(operationId);
} else {
return toOperationId(operationId);
}
return toOperationId(removeNonNameElementToCamelCase(operationId));
}
/**

View File

@ -2326,6 +2326,20 @@ public class DefaultCodegenTest {
Assertions.assertEquals(codegenModel.vars.get(0).getBaseType(), "TypeAlias");
}
@Test
public void operationIdNameMapping() {
DefaultCodegen codegen = new DefaultCodegen();
codegen.operationIdNameMapping.put("edge case !@# 123", "fix_edge_case");
OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/type-alias.yaml", null, new ParseOptions()).getOpenAPI();
codegen.setOpenAPI(openAPI);
CodegenOperation codegenOperation = codegen.fromOperation("/type-alias", "get", openAPI.getPaths().get("/type-alias").getGet(), null);
Assertions.assertEquals(codegenOperation.operationId, "fix_edge_case");
Assertions.assertEquals(codegen.getOrGenerateOperationId(openAPI.getPaths().get("/type-alias").getGet(), "/type-alias", "get"), "fix_edge_case");
}
@Test
public void modelWithPrefixDoNotContainInheritedVars() {
DefaultCodegen codegen = new DefaultCodegen();

View File

@ -6,6 +6,7 @@ info:
paths:
/type-alias:
get:
operationId: edge case !@# 123
responses:
200:
description: OK