Skip to content

Commit b800132

Browse files
authored
[Core] Consolidate Operation ID generation (#19339)
* [Core] Consolidate Operation ID generation * Update samples
1 parent 58dd030 commit b800132

2 files changed

Lines changed: 23 additions & 23 deletions

File tree

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4445,21 +4445,7 @@ public CodegenOperation fromOperation(String path,
44454445

44464446
// store the original operationId for plug-in
44474447
op.operationIdOriginal = operation.getOperationId();
4448-
4449-
String operationId = getOrGenerateOperationId(operation, path, httpMethod);
4450-
// remove prefix in operationId
4451-
if (removeOperationIdPrefix) {
4452-
// The prefix is everything before the removeOperationIdPrefixCount occurrence of removeOperationIdPrefixDelimiter
4453-
String[] components = operationId.split("[" + removeOperationIdPrefixDelimiter + "]");
4454-
if (components.length > 1) {
4455-
// If removeOperationIdPrefixCount is -1 or bigger that the number of occurrences, uses the last one
4456-
int component_number = removeOperationIdPrefixCount == -1 ? components.length - 1 : removeOperationIdPrefixCount;
4457-
component_number = Math.min(component_number, components.length - 1);
4458-
// Reconstruct the operationId from its split elements and the delimiter
4459-
operationId = String.join(removeOperationIdPrefixDelimiter, Arrays.copyOfRange(components, component_number, components.length));
4460-
}
4461-
}
4462-
operationId = removeNonNameElementToCamelCase(operationId);
4448+
op.operationId = getOrGenerateOperationId(operation, path, httpMethod);
44634449

44644450
if (isStrictSpecBehavior() && !path.startsWith("/")) {
44654451
// modifies an operation.path to strictly conform to OpenAPI Spec
@@ -4468,11 +4454,6 @@ public CodegenOperation fromOperation(String path,
44684454
op.path = path;
44694455
}
44704456

4471-
if (operationIdNameMapping.containsKey(operationId)) {
4472-
op.operationId = operationIdNameMapping.get(operationId);
4473-
} else {
4474-
op.operationId = toOperationId(operationId);
4475-
}
44764457
op.summary = escapeText(operation.getSummary());
44774458
op.unescapedNotes = operation.getDescription();
44784459
op.notes = escapeText(operation.getDescription());
@@ -5599,7 +5580,26 @@ protected String getOrGenerateOperationId(Operation operation, String path, Stri
55995580
operationId = sanitizeName(builder.toString());
56005581
LOGGER.warn("Empty operationId found for path: {} {}. Renamed to auto-generated operationId: {}", httpMethod, path, operationId);
56015582
}
5602-
return operationId;
5583+
5584+
// remove prefix in operationId
5585+
if (removeOperationIdPrefix) {
5586+
// The prefix is everything before the removeOperationIdPrefixCount occurrence of removeOperationIdPrefixDelimiter
5587+
String[] components = operationId.split("[" + removeOperationIdPrefixDelimiter + "]");
5588+
if (components.length > 1) {
5589+
// If removeOperationIdPrefixCount is -1 or bigger that the number of occurrences, uses the last one
5590+
int component_number = removeOperationIdPrefixCount == -1 ? components.length - 1 : removeOperationIdPrefixCount;
5591+
component_number = Math.min(component_number, components.length - 1);
5592+
// Reconstruct the operationId from its split elements and the delimiter
5593+
operationId = String.join(removeOperationIdPrefixDelimiter, Arrays.copyOfRange(components, component_number, components.length));
5594+
}
5595+
}
5596+
operationId = removeNonNameElementToCamelCase(operationId);
5597+
5598+
if (operationIdNameMapping.containsKey(operationId)) {
5599+
return operationIdNameMapping.get(operationId);
5600+
} else {
5601+
return toOperationId(operationId);
5602+
}
56035603
}
56045604

56055605
/**

samples/server/petstore/rust-server/output/openapi-v3/api/openapi.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ paths:
258258
callback:
259259
'{$request.query.url}/callback':
260260
post:
261-
operationId: callback_CallbackPost
261+
operationId: CallbackCallbackPost
262262
responses:
263263
"204":
264264
description: OK
@@ -281,7 +281,7 @@ paths:
281281
callback:
282282
'{$request.query.url}/callback-with-header':
283283
post:
284-
operationId: callback_CallbackWithHeaderPost
284+
operationId: CallbackCallbackWithHeaderPost
285285
parameters:
286286
- explode: false
287287
in: header

0 commit comments

Comments
 (0)