Allow $ in java var name (#200)

This commit is contained in:
Jérémie Bresson 2018-04-23 15:48:10 +02:00 committed by GitHub
parent 450cbb8250
commit e7410d4c8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 5 deletions

View File

@ -3320,7 +3320,14 @@ public class DefaultCodegen implements CodegenConfig {
}
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
@ -3514,6 +3521,17 @@ public class DefaultCodegen implements CodegenConfig {
*/
@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
@ -3553,9 +3571,9 @@ public class DefaultCodegen implements CodegenConfig {
// 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;

View File

@ -516,7 +516,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
@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";

View File

@ -18,6 +18,23 @@ import java.util.Set;
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");

View File

@ -49,6 +49,23 @@ public class AbstractJavaCodegenTest {
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();