Skip to content

Commit

Permalink
[Core] Consolidate Operation ID generation (OpenAPITools#19339)
Browse files Browse the repository at this point in the history
* [Core] Consolidate Operation ID generation

* Update samples
  • Loading branch information
richardwhiuk authored Aug 14, 2024
1 parent 58dd030 commit b800132
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4445,21 +4445,7 @@ public CodegenOperation fromOperation(String path,

// store the original operationId for plug-in
op.operationIdOriginal = operation.getOperationId();

String operationId = getOrGenerateOperationId(operation, path, httpMethod);
// remove prefix in operationId
if (removeOperationIdPrefix) {
// The prefix is everything before the removeOperationIdPrefixCount occurrence of removeOperationIdPrefixDelimiter
String[] components = operationId.split("[" + removeOperationIdPrefixDelimiter + "]");
if (components.length > 1) {
// If removeOperationIdPrefixCount is -1 or bigger that the number of occurrences, uses the last one
int component_number = removeOperationIdPrefixCount == -1 ? components.length - 1 : removeOperationIdPrefixCount;
component_number = Math.min(component_number, components.length - 1);
// Reconstruct the operationId from its split elements and the delimiter
operationId = String.join(removeOperationIdPrefixDelimiter, Arrays.copyOfRange(components, component_number, components.length));
}
}
operationId = removeNonNameElementToCamelCase(operationId);
op.operationId = getOrGenerateOperationId(operation, path, httpMethod);

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

if (operationIdNameMapping.containsKey(operationId)) {
op.operationId = operationIdNameMapping.get(operationId);
} else {
op.operationId = toOperationId(operationId);
}
op.summary = escapeText(operation.getSummary());
op.unescapedNotes = operation.getDescription();
op.notes = escapeText(operation.getDescription());
Expand Down Expand Up @@ -5599,7 +5580,26 @@ protected String getOrGenerateOperationId(Operation operation, String path, Stri
operationId = sanitizeName(builder.toString());
LOGGER.warn("Empty operationId found for path: {} {}. Renamed to auto-generated operationId: {}", httpMethod, path, operationId);
}
return operationId;

// remove prefix in operationId
if (removeOperationIdPrefix) {
// The prefix is everything before the removeOperationIdPrefixCount occurrence of removeOperationIdPrefixDelimiter
String[] components = operationId.split("[" + removeOperationIdPrefixDelimiter + "]");
if (components.length > 1) {
// If removeOperationIdPrefixCount is -1 or bigger that the number of occurrences, uses the last one
int component_number = removeOperationIdPrefixCount == -1 ? components.length - 1 : removeOperationIdPrefixCount;
component_number = Math.min(component_number, components.length - 1);
// Reconstruct the operationId from its split elements and the delimiter
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);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ paths:
callback:
'{$request.query.url}/callback':
post:
operationId: callback_CallbackPost
operationId: CallbackCallbackPost
responses:
"204":
description: OK
Expand All @@ -281,7 +281,7 @@ paths:
callback:
'{$request.query.url}/callback-with-header':
post:
operationId: callback_CallbackWithHeaderPost
operationId: CallbackCallbackWithHeaderPost
parameters:
- explode: false
in: header
Expand Down

0 comments on commit b800132

Please sign in to comment.