forked from loafle/openapi-generator-original
Add new configuration flags: REMOVE_OPERATION_ID_PREFIX_DELIMITER and REMOVE_OPERATION_ID_PREFIX_COUNT Add the corresponding fields in the DefaultCodegen Sanitize the operationId according to the new fields Add delimiter to list of nonNameElementPattern Add unit test
This commit is contained in:
@@ -317,6 +317,12 @@ public class CodegenConstants {
|
||||
public static final String REMOVE_OPERATION_ID_PREFIX = "removeOperationIdPrefix";
|
||||
public static final String REMOVE_OPERATION_ID_PREFIX_DESC = "Remove prefix of operationId, e.g. config_getId => getId";
|
||||
|
||||
public static final String REMOVE_OPERATION_ID_PREFIX_DELIMITER = "removeOperationIdPrefixDelimiter";
|
||||
public static final String REMOVE_OPERATION_ID_PREFIX_DELIMITER_DESC = "Character to use as a delimiter for the prefix. Default: '_'";
|
||||
|
||||
public static final String REMOVE_OPERATION_ID_PREFIX_COUNT = "removeOperationIdPrefixCount";
|
||||
public static final String REMOVE_OPERATION_ID_PREFIX_COUNT_DESC = "Count of delimiter for the prefix. Use -1 for last Default: 1";
|
||||
|
||||
public static final String SKIP_OPERATION_EXAMPLE = "skipOperationExample";
|
||||
public static final String SKIP_OPERATION_EXAMPLE_DESC = "Skip examples defined in operations to avoid out of memory errors.";
|
||||
|
||||
|
||||
@@ -177,6 +177,8 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
protected List<CliOption> cliOptions = new ArrayList<CliOption>();
|
||||
protected boolean skipOverwrite;
|
||||
protected boolean removeOperationIdPrefix;
|
||||
protected String removeOperationIdPrefixDelimiter = "_";
|
||||
protected int removeOperationIdPrefixCount = 1;
|
||||
protected boolean skipOperationExample;
|
||||
|
||||
protected final static Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application\\/json(;.*)?");
|
||||
@@ -323,6 +325,16 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
.get(CodegenConstants.REMOVE_OPERATION_ID_PREFIX).toString()));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_DELIMITER)) {
|
||||
this.setRemoveOperationIdPrefixDelimiter(additionalProperties
|
||||
.get(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_DELIMITER).toString());
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_COUNT)) {
|
||||
this.setRemoveOperationIdPrefixCount(Integer.parseInt(additionalProperties
|
||||
.get(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_COUNT).toString()));
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.SKIP_OPERATION_EXAMPLE)) {
|
||||
this.setSkipOperationExample(Boolean.parseBoolean(additionalProperties
|
||||
.get(CodegenConstants.SKIP_OPERATION_EXAMPLE).toString()));
|
||||
@@ -3725,9 +3737,14 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
String operationId = getOrGenerateOperationId(operation, path, httpMethod);
|
||||
// remove prefix in operationId
|
||||
if (removeOperationIdPrefix) {
|
||||
int offset = operationId.indexOf('_');
|
||||
if (offset > -1) {
|
||||
operationId = operationId.substring(offset + 1);
|
||||
// The prefix is everything before the removeOperationIdPrefixCount occurrence of removeOperationIdPrefixDelimiter
|
||||
String[] componenets = operationId.split("[" + removeOperationIdPrefixDelimiter + "]");
|
||||
if (componenets.length > 1) {
|
||||
// If removeOperationIdPrefixCount is -1 or bigger that the number of occurrences, uses the last one
|
||||
int componenet_number = removeOperationIdPrefixCount == -1 ? componenets.length - 1 : removeOperationIdPrefixCount;
|
||||
componenet_number = Math.min(componenet_number, componenets.length - 1);
|
||||
// Reconstruct the operationId from its split elements and the delimiter
|
||||
operationId = String.join(removeOperationIdPrefixDelimiter, Arrays.copyOfRange(componenets, componenet_number, componenets.length));
|
||||
}
|
||||
}
|
||||
operationId = removeNonNameElementToCamelCase(operationId);
|
||||
@@ -4990,7 +5007,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
*/
|
||||
@SuppressWarnings("static-method")
|
||||
public String removeNonNameElementToCamelCase(String name) {
|
||||
return removeNonNameElementToCamelCase(name, "[-_:;#]");
|
||||
return removeNonNameElementToCamelCase(name, "[-_:;#" + removeOperationIdPrefixDelimiter + "]");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5069,6 +5086,22 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
this.removeOperationIdPrefix = removeOperationIdPrefix;
|
||||
}
|
||||
|
||||
public String getRemoveOperationIdPrefixDelimiter() {
|
||||
return removeOperationIdPrefixDelimiter;
|
||||
}
|
||||
|
||||
public void setRemoveOperationIdPrefixDelimiter(String removeOperationIdPrefixDelimiter) {
|
||||
this.removeOperationIdPrefixDelimiter = removeOperationIdPrefixDelimiter;
|
||||
}
|
||||
|
||||
public int getRemoveOperationIdPrefixCount() {
|
||||
return removeOperationIdPrefixCount;
|
||||
}
|
||||
|
||||
public void setRemoveOperationIdPrefixCount(int removeOperationIdPrefixCount) {
|
||||
this.removeOperationIdPrefixCount = removeOperationIdPrefixCount;
|
||||
}
|
||||
|
||||
public void setSkipOperationExample(boolean skipOperationExample) {
|
||||
this.skipOperationExample = skipOperationExample;
|
||||
}
|
||||
|
||||
@@ -3560,4 +3560,70 @@ public class DefaultCodegenTest {
|
||||
assertEquals(cr.isShort, false);
|
||||
assertEquals(cr.isLong, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveOperationIdPrefix() {
|
||||
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/bugs/issue_9719.yaml");
|
||||
final DefaultCodegen codegen = new DefaultCodegen();
|
||||
codegen.setOpenAPI(openAPI);
|
||||
codegen.setDisallowAdditionalPropertiesIfNotPresent(false);
|
||||
|
||||
String path;
|
||||
Operation operation;
|
||||
CodegenOperation co;
|
||||
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX, "True");
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_DELIMITER, ".");
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_COUNT, 2);
|
||||
codegen.processOpts();
|
||||
path = "/dotDelimiter";
|
||||
operation = openAPI.getPaths().get(path).getGet();
|
||||
co = codegen.fromOperation(path, "GET", operation, null);
|
||||
assertEquals(co.operationId, "usersGetAll");
|
||||
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX, "True");
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_DELIMITER, ".");
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_COUNT, -1);
|
||||
codegen.processOpts();
|
||||
path = "/dotDelimiter";
|
||||
operation = openAPI.getPaths().get(path).getGet();
|
||||
co = codegen.fromOperation(path, "GET", operation, null);
|
||||
assertEquals(co.operationId, "getAll");
|
||||
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX, "True");
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_DELIMITER, ".");
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_COUNT, 10);
|
||||
codegen.processOpts();
|
||||
path = "/dotDelimiter";
|
||||
operation = openAPI.getPaths().get(path).getGet();
|
||||
co = codegen.fromOperation(path, "GET", operation, null);
|
||||
assertEquals(co.operationId, "getAll");
|
||||
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX, "True");
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_DELIMITER, "_");
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_COUNT, 2);
|
||||
codegen.processOpts();
|
||||
path = "/underscoreDelimiter";
|
||||
operation = openAPI.getPaths().get(path).getGet();
|
||||
co = codegen.fromOperation(path, "GET", operation, null);
|
||||
assertEquals(co.operationId, "usersGetAll");
|
||||
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX, "True");
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_DELIMITER, "_");
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_COUNT, -1);
|
||||
codegen.processOpts();
|
||||
path = "/underscoreDelimiter";
|
||||
operation = openAPI.getPaths().get(path).getGet();
|
||||
co = codegen.fromOperation(path, "GET", operation, null);
|
||||
assertEquals(co.operationId, "getAll");
|
||||
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX, "True");
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_DELIMITER, "_");
|
||||
codegen.additionalProperties().put(CodegenConstants.REMOVE_OPERATION_ID_PREFIX_COUNT, 10);
|
||||
codegen.processOpts();
|
||||
path = "/underscoreDelimiter";
|
||||
operation = openAPI.getPaths().get(path).getGet();
|
||||
co = codegen.fromOperation(path, "GET", operation, null);
|
||||
assertEquals(co.operationId, "getAll");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
openapi: 3.0.1
|
||||
info:
|
||||
title: OpenAPI Petstore
|
||||
description: "sample to vet integer handling"
|
||||
license:
|
||||
name: Apache-2.0
|
||||
url: https://www.apache.org/licenses/LICENSE-2.0.html
|
||||
version: 1.0.0
|
||||
servers:
|
||||
- url: http://petstore.swagger.io:80/v2
|
||||
tags: []
|
||||
paths:
|
||||
/dotDelimiter:
|
||||
get:
|
||||
summary: List all users
|
||||
operationId: petstore.api.users.get_all
|
||||
tags:
|
||||
- users
|
||||
responses:
|
||||
'200':
|
||||
description: A paged array of users
|
||||
headers:
|
||||
x-next:
|
||||
description: A link to the next page of responses
|
||||
schema:
|
||||
type: string
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: string
|
||||
/underscoreDelimiter:
|
||||
get:
|
||||
summary: List all users
|
||||
operationId: petstore_api_users_getAll
|
||||
tags:
|
||||
- users
|
||||
responses:
|
||||
'200':
|
||||
description: A paged array of users
|
||||
headers:
|
||||
x-next:
|
||||
description: A link to the next page of responses
|
||||
schema:
|
||||
type: string
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: string
|
||||
Reference in New Issue
Block a user