mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-12 12:40:53 +00:00
Fix operationId mapping (#20846)
* fix operationId mapping * add tests * minor change
This commit is contained in:
parent
e40f9e3801
commit
3ab495a0aa
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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();
|
||||
|
@ -6,6 +6,7 @@ info:
|
||||
paths:
|
||||
/type-alias:
|
||||
get:
|
||||
operationId: edge case !@# 123
|
||||
responses:
|
||||
200:
|
||||
description: OK
|
||||
|
Loading…
x
Reference in New Issue
Block a user