forked from loafle/openapi-generator-original
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) {
|
protected String getOrGenerateOperationId(Operation operation, String path, String httpMethod) {
|
||||||
String operationId = operation.getOperationId();
|
String operationId = operation.getOperationId();
|
||||||
|
|
||||||
if (StringUtils.isBlank(operationId)) {
|
if (StringUtils.isBlank(operationId)) {
|
||||||
String tmpPath = path;
|
String tmpPath = path;
|
||||||
tmpPath = tmpPath.replaceAll("\\{", "");
|
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);
|
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
|
// remove prefix in operationId
|
||||||
if (removeOperationIdPrefix) {
|
if (removeOperationIdPrefix) {
|
||||||
// The prefix is everything before the removeOperationIdPrefixCount occurrence of removeOperationIdPrefixDelimiter
|
// 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 = String.join(removeOperationIdPrefixDelimiter, Arrays.copyOfRange(components, component_number, components.length));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
operationId = removeNonNameElementToCamelCase(operationId);
|
|
||||||
|
|
||||||
if (operationIdNameMapping.containsKey(operationId)) {
|
return toOperationId(removeNonNameElementToCamelCase(operationId));
|
||||||
return operationIdNameMapping.get(operationId);
|
|
||||||
} else {
|
|
||||||
return toOperationId(operationId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2326,6 +2326,20 @@ public class DefaultCodegenTest {
|
|||||||
Assertions.assertEquals(codegenModel.vars.get(0).getBaseType(), "TypeAlias");
|
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
|
@Test
|
||||||
public void modelWithPrefixDoNotContainInheritedVars() {
|
public void modelWithPrefixDoNotContainInheritedVars() {
|
||||||
DefaultCodegen codegen = new DefaultCodegen();
|
DefaultCodegen codegen = new DefaultCodegen();
|
||||||
|
@ -6,6 +6,7 @@ info:
|
|||||||
paths:
|
paths:
|
||||||
/type-alias:
|
/type-alias:
|
||||||
get:
|
get:
|
||||||
|
operationId: edge case !@# 123
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
description: OK
|
description: OK
|
||||||
|
Loading…
x
Reference in New Issue
Block a user