[jOOQ/jOOQ#17099] [jOOQ/jOOQ#17117] Code generation escaping issues

This includes:

- [jOOQ/jOOQ#17099] KotlinGenerator contains incomplete list of Kotlin forbidden identifier characters
- [jOOQ/jOOQ#17117] Bad code generated for enum values containing backslash character
This commit is contained in:
Lukas Eder 2024-08-21 14:18:18 +02:00
parent 7ce935bbb5
commit d57dcdbb85
3 changed files with 27 additions and 15 deletions

View File

@ -358,19 +358,31 @@ class GenerationUtil {
* Check if a character can be used in a kotlin identifier.
* <p>
* See <a href=
* "https://kotlinlang.org/spec/syntax-and-grammar.html#grammar-rule-QuotedSymbol">https://kotlinlang.org/spec/syntax-and-grammar.html#grammar-rule-QuotedSymbol</a>
* "https://kotlinlang.org/docs/reference/grammar.html#Identifier">https://kotlinlang.org/docs/reference/grammar.html#Identifier</a>
*/
private static boolean isKotlinIdentifierPart(char c) {
return c != '\r'
&& c != '\n'
&& c != '`'
&& c != '('
&& c != ')'
&& c != '{'
&& c != '}'
&& c != '['
&& c != ']'
&& c != '.';
switch (c) {
case '\r':
case '\n':
case '`':
case '.':
case ';':
case ':':
case '\\':
case '/':
case '[':
case ']':
// [#17099] These used to be listed, but aren't actually forbidden:
// case '(':
// case ')':
// case '{':
// case '}':
case '<':
case '>':
return false;
default:
return true;
}
}
/**

View File

@ -4158,7 +4158,7 @@ public class JavaGenerator extends AbstractGenerator {
out.println("def valueOf(s: %s): %s = s match {", String.class, className);
for (int i = 0; i < identifiers.size(); i++) {
out.println("case \"%s\" => %s", literals.get(i), identifiers.get(i));
out.println("case \"%s\" => %s", escapeString(literals.get(i)), identifiers.get(i));
}
out.println("case _ => throw new %s()", IllegalArgumentException.class);
out.println("}");
@ -4199,7 +4199,7 @@ public class JavaGenerator extends AbstractGenerator {
out.println("%senum class %s(@get:JvmName(\"literal\") public val literal: String)[[before= : ][%s]] {", visibility(), className, interfaces);
for (int i = 0; i < literals.size(); i++)
out.println("%s(\"%s\")%s", identifiers.get(i), literals.get(i), (i == literals.size() - 1) ? ";" : ",");
out.println("%s(\"%s\")%s", identifiers.get(i), escapeString(literals.get(i)), (i == literals.size() - 1) ? ";" : ",");
out.println("%soverride fun getCatalog(): %s? = %s",
visibilityPublic(), Catalog.class, noCatalog ? "null" : "schema.catalog");
@ -4222,7 +4222,7 @@ public class JavaGenerator extends AbstractGenerator {
for (int i = 0; i < literals.size(); i++) {
out.println();
out.println("%s(\"%s\")%s", identifiers.get(i), literals.get(i), (i == literals.size() - 1) ? ";" : ",");
out.println("%s(\"%s\")%s", identifiers.get(i), escapeString(literals.get(i)), (i == literals.size() - 1) ? ";" : ",");
}
out.println();

View File

@ -1176,7 +1176,7 @@ implements
Condition and = null;
for (Field<?> field : fields) {
Field<Object> f = (Field<Object>) field;
Field<Object> f = (Field<Object>) orElse(table().field(field), () -> field);
Condition other = matchByConflictingKey(ctx, f, s.field(f));
and = (and == null) ? other : and.and(other);
}