Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for stripping prefix on single enums #896

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -396,21 +396,25 @@ private String findEnumName(int truncateIdx, Object value) {
}

/**
* Returns the common prefix of variables for enum naming
* Returns the common prefix of variables for enum naming if
* two or more variables are present.
*
* @param vars List of variable names
* @return the common prefix for naming
*/
public String findCommonPrefixOfVars(List<Object> vars) {
try {
String[] listStr = vars.toArray(new String[vars.size()]);
String prefix = StringUtils.getCommonPrefix(listStr);
// exclude trailing characters that should be part of a valid variable
// e.g. ["status-on", "status-off"] => "status-" (not "status-o")
return prefix.replaceAll("[a-zA-Z0-9]+\\z", "");
} catch (ArrayStoreException e) {
return "";
if (vars.size() > 1) {
try {
String[] listStr = vars.toArray(new String[vars.size()]);
String prefix = StringUtils.getCommonPrefix(listStr);
// exclude trailing characters that should be part of a valid variable
// e.g. ["status-on", "status-off"] => "status-" (not "status-o")
return prefix.replaceAll("[a-zA-Z0-9]+\\z", "");
} catch (ArrayStoreException e) {
// do nothing, just return default value
}
}
return "";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1233,15 +1233,6 @@ protected void replaceDuplicatedInModelProperties(Map<String, Schema> definition
schema.set$ref(schema.get$ref().replace(modelName, newModelName));
});
}
/*
@Override
public String findCommonPrefixOfVars(List<String> vars) {
String prefix = StringUtils.getCommonPrefix(vars.toArray(new String[vars.size()]));
// exclude trailing characters that should be part of a valid variable
// e.g. ["status-on", "status-off"] => "status-" (not "status-o")
return prefix.replaceAll("[a-zA-Z0-9]+\\z", "");
}
*/

@Override
public String toEnumName(CodegenProperty property) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.testng.annotations.Test;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -310,6 +312,22 @@ public void testFromResponse_referenceHeaders() {
Assert.assertEquals(headerProperty.example, referencedHeader.getSchema().getExample());
}

@Test(dataProvider = "testCommonPrefixProvider")
public void testCommonPrefix(List<Object> vars, String expectedPrefix) {
DefaultCodegenConfig codegen = new P_DefaultCodegenConfig();
Assert.assertEquals(codegen.findCommonPrefixOfVars(vars), expectedPrefix);
}

@DataProvider(name = "testCommonPrefixProvider")
public Object[][] provideData_testCommonPrefix() {
return new Object[][]{
{Collections.singletonList("FOO_BAR"), ""},
{Arrays.asList("FOO_BAR", "FOO_BAZ"), "FOO_"},
{Arrays.asList("FOO_BAR", "FOO_BAZ", "TEST"), ""},
{Arrays.asList("STATUS-ON", "STATUS-OFF", "STATUS"), ""}
};
}

private static class P_DefaultCodegenConfig extends DefaultCodegenConfig{
@Override
public String getArgumentsLocation() {
Expand Down