Skip to content

Commit

Permalink
#3444 - DBName with hyphens creates invalid code (#3447)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave authored Jul 16, 2024
1 parent 9f0eaef commit bbd5636
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void writeMethodOtherClasses() {
private void writeMethodEntityClasses(Set<String> dbEntities, String dbName) {
String method = "defaultEntityClasses";
if (dbName != null) {
method = "entitiesFor_" + dbName;
method = "entitiesFor_" + Util.stripForMethod(dbName);
writeMethodComment("Entities for @DbName(name=\"%s\"))", dbName);
} else {
writeMethodComment("Entities with no @DbName", dbName);
Expand Down Expand Up @@ -187,7 +187,7 @@ private void writeMethodComment(String msg, String arg) {
private void writeMethodEntityClassesFor(Set<String> otherDbNames) {
writer.append(" private List<Class<?>> classesFor(String dbName) {").eol();
for (String dbName : otherDbNames) {
writer.append(" if (\"%s\".equals(dbName)) return entitiesFor_%s();", dbName, dbName).eol();
writer.append(" if (\"%s\".equals(dbName)) return entitiesFor_%s();", dbName, Util.stripForMethod(dbName)).eol();
}
writer.append(" return new ArrayList<>();").eol();
writer.append(" }").eol().eol();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

class Util {

static String stripForMethod(String dbName) {
return dbName.replace('-', '_');
}

static String packageOf(boolean nested, String originName) {
return nested ? nestedPackageOf(originName) : packageOf(originName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import org.junit.jupiter.api.Test

internal class UtilTest {

@Test
fun stripForMethod() {
Assertions.assertEquals(io.ebean.querybean.generator.Util.stripForMethod("-a"), "_a")
Assertions.assertEquals(io.ebean.querybean.generator.Util.stripForMethod("-a-b-"), "_a_b_")
Assertions.assertEquals(io.ebean.querybean.generator.Util.stripForMethod("c-"), "c_")
Assertions.assertEquals(io.ebean.querybean.generator.Util.stripForMethod("my-foo"), "my_foo")
}

@Test
fun packageOf() {
Assertions.assertEquals(Util.packageOf(true, "com.example.Foo.Bar"), "com.example")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private void writeMethodOtherClasses() {
private void writeMethodEntityClasses(Set<String> dbEntities, String dbName) {
String method = "defaultEntityClasses";
if (dbName != null) {
method = "entitiesFor_" + dbName;
method = "entitiesFor_" + Util.stripForMethod(dbName);
writeMethodComment("Entities for @DbName(name=\"%s\"))", dbName);
} else {
writeMethodComment("Entities with no @DbName", dbName);
Expand Down Expand Up @@ -216,7 +216,7 @@ private void writeMethodComment(String msg, String arg) {
private void writeMethodEntityClassesFor(Set<String> otherDbNames) {
writer.append(" private List<Class<?>> classesFor(String dbName) {").eol();
for (String dbName : otherDbNames) {
writer.append(" if (\"%s\".equals(dbName)) return entitiesFor_%s();", dbName, dbName).eol();
writer.append(" if (\"%s\".equals(dbName)) return entitiesFor_%s();", dbName, Util.stripForMethod(dbName)).eol();
}
writer.append(" return new ArrayList<>();").eol();
writer.append(" }").eol().eol();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

class Util {

static String stripForMethod(String dbName) {
return dbName.replace('-', '_');
}

static String packageOf(boolean nested, String originName) {
return nested ? nestedPackageOf(originName) : packageOf(originName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

class UtilTest {

@Test
void stripForMethod() {
assertEquals(Util.stripForMethod("-a"), "_a");
assertEquals(Util.stripForMethod("-a-b-"), "_a_b_");
assertEquals(Util.stripForMethod("c-"), "c_");
assertEquals(Util.stripForMethod("my-foo"), "my_foo");
}

@Test
void packageOf() {
assertEquals(Util.packageOf(true, "com.example.Foo.Bar"), "com.example");
Expand Down

0 comments on commit bbd5636

Please sign in to comment.