Skip to content

Commit

Permalink
Allow $ in java var name (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmini committed Apr 23, 2018
1 parent 450cbb8 commit e7410d4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3320,7 +3320,14 @@ public static String camelize(String word, boolean lowercaseFirstLetter) {
}

if (lowercaseFirstLetter && word.length() > 0) {
word = word.substring(0, 1).toLowerCase() + word.substring(1);
int i = 0;
char charAt = word.charAt(i);
while(i + 1 < word.length() && !((charAt >= 'a' && charAt <= 'z') || (charAt >= 'A' && charAt <= 'Z'))) {
i = i + 1;
charAt = word.charAt(i);
}
i = i + 1;
word = word.substring(0, i).toLowerCase() + word.substring(i);
}

// remove all underscore
Expand Down Expand Up @@ -3514,6 +3521,17 @@ protected CliOption buildLibraryCliOption(Map<String, String> supportedLibraries
*/
@SuppressWarnings("static-method")
public String sanitizeName(String name) {
return sanitizeName(name, "\\W");
}

/**
* Sanitize name (parameter, property, method, etc)
*
* @param name string to be sanitize
* @param removeCharRegEx a regex containing all char that will be removed
* @return sanitized string
*/
public String sanitizeName(String name, String removeCharRegEx) {
// NOTE: performance wise, we should have written with 2 replaceAll to replace desired
// character with _ or empty character. Below aims to spell out different cases we've
// encountered so far and hopefully make it easier for others to add more special
Expand Down Expand Up @@ -3553,9 +3571,9 @@ public String sanitizeName(String name) {
// remove everything else other than word, number and _
// $php_variable => php_variable
if (allowUnicodeIdentifiers) { //could be converted to a single line with ?: operator
name = Pattern.compile("\\W", Pattern.UNICODE_CHARACTER_CLASS).matcher(name).replaceAll("");
name = Pattern.compile(removeCharRegEx, Pattern.UNICODE_CHARACTER_CLASS).matcher(name).replaceAll("");
} else {
name = name.replaceAll("\\W", "");
name = name.replaceAll(removeCharRegEx, "");
}

return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public String toApiFilename(String name) {
@Override
public String toVarName(String name) {
// sanitize name
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
name = sanitizeName(name, "\\W-[\\$]"); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.

if (name.toLowerCase().matches("^_*class$")) {
return "propertyClass";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@

public class DefaultCodegenTest {

@Test
public void testCamelize() throws Exception {
Assert.assertEquals(DefaultCodegen.camelize("abcd"), "Abcd");
Assert.assertEquals(DefaultCodegen.camelize("some-value"), "SomeValue");
Assert.assertEquals(DefaultCodegen.camelize("some_value"), "SomeValue");
Assert.assertEquals(DefaultCodegen.camelize("$type"), "$Type");

Assert.assertEquals(DefaultCodegen.camelize("abcd", true), "abcd");
Assert.assertEquals(DefaultCodegen.camelize("some-value", true), "someValue");
Assert.assertEquals(DefaultCodegen.camelize("some_value", true), "someValue");
Assert.assertEquals(DefaultCodegen.camelize("Abcd", true), "abcd");
Assert.assertEquals(DefaultCodegen.camelize("$type", true), "$type");

Assert.assertEquals(DefaultCodegen.camelize("123", true), "123");
Assert.assertEquals(DefaultCodegen.camelize("$123", true), "$123");
}

@Test
public void testHasBodyParameter() throws Exception {
final Schema refSchema = new Schema<>().$ref("#/components/schemas/Pet");
Expand All @@ -41,7 +58,7 @@ public void testHasBodyParameter() throws Exception {
Assert.assertEquals(codegen.hasBodyParameter(openAPI, pingOperation), false);
Assert.assertEquals(codegen.hasBodyParameter(openAPI, createOperation), true);
}

@Test
public void testGetConsumesInfoAndGetProducesInfo() throws Exception {
final DefaultCodegen codegen = new DefaultCodegen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ public void testPreprocessOpenAPI() throws Exception {
Assert.assertEquals(openAPI.getPaths().get("/pet").getPost().getExtensions().get("x-accepts"), "application/json");
}

@Test
public void convertVarName() throws Exception {
Assert.assertEquals(fakeJavaCodegen.toVarName("name"), "name");
Assert.assertEquals(fakeJavaCodegen.toVarName("$name"), "$name");
Assert.assertEquals(fakeJavaCodegen.toVarName("nam$$e"), "nam$$e");
Assert.assertEquals(fakeJavaCodegen.toVarName("user-name"), "userName");
Assert.assertEquals(fakeJavaCodegen.toVarName("user_name"), "userName");
}

@Test
public void convertModelName() throws Exception {
Assert.assertEquals(fakeJavaCodegen.toModelName("name"), "Name");
Assert.assertEquals(fakeJavaCodegen.toModelName("$name"), "Name");
Assert.assertEquals(fakeJavaCodegen.toModelName("nam#e"), "Name");
Assert.assertEquals(fakeJavaCodegen.toModelName("$another-fake?"), "AnotherFake");
}

@Test
public void testInitialConfigValues() throws Exception {
final AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
Expand Down

0 comments on commit e7410d4

Please sign in to comment.