Skip to content

Commit

Permalink
[java-generator] handle more special characters in field names (fabri…
Browse files Browse the repository at this point in the history
  • Loading branch information
miriSch authored May 24, 2023
1 parent 39b526c commit b5091a8
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#### Bugs
* Fix #5125: TLS 1.3 only should be supported
* Fix #5145: [java-generator] handle `additionalProperties: true` emitting a field of type `AnyType`
* Fix #5164: [java-generator] handle more special characters in field names

#### Improvements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ public static String sanitizeString(String str) {
index = sanitized.indexOf('-');
}

sanitized = sanitized.replace('.', '_');
sanitized = sanitized.replace(' ', '_');
sanitized = sanitized.replace('\'', '_');
sanitized = sanitized.replace('\"', '_');
StringBuilder sanitizedSb = new StringBuilder(sanitized.length());
for (char originalChar : sanitized.toCharArray()) {
char sanitizedChar = Character.isJavaIdentifierPart(originalChar) ? originalChar : '_';
sanitizedSb.append(sanitizedChar);
}
sanitized = sanitizedSb.toString();

return sanitized;
}
Expand Down
2 changes: 2 additions & 0 deletions java-generator/core/src/test/resources/dummy-crd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ spec:
type: string
four"doublequote:
type: string
five/slash:
type: string
scope: Namespaced
names:
plural: dummies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void testDeserialization() {
assertEquals("2", spec.getTwo_space());
assertEquals("3", spec.getThree_quote());
assertEquals("4", spec.getFour_doublequote());
assertEquals("5", spec.getFive_slash());
}

@Test
Expand All @@ -61,6 +62,7 @@ void testAgainstSample() throws Exception {
spec.setTwo_space("2");
spec.setThree_quote("3");
spec.setFour_doublequote("4");
spec.setFive_slash("5");
sample.setSpec(spec);
ObjectMeta om = new ObjectMeta();
om.setName("sample");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ spec:
type: string
four"doublequote:
type: string
five/slash:
type: string
scope: Namespaced
names:
plural: dummies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ spec:
two space: "2"
three'quote: "3"
four"doublequote: "4"
five/slash: "5"

0 comments on commit b5091a8

Please sign in to comment.