From 5ebcd695e9cbb00930df7d62333e34326f981a24 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Thu, 10 Aug 2023 15:09:03 +0100 Subject: [PATCH] [java-generator] Avoid to emit Java Keywords as package names (cherry picked from commit d969f01562b44c8f39e22ff5bb79e006828fac36) --- CHANGELOG.md | 1 + .../java/generator/nodes/AbstractJSONSchema2Pojo.java | 5 +++++ .../java/io/fabric8/it/dummy/TestEscapeCharacters.java | 8 ++++++++ .../it/escape-characters/src/test/resources/dummy-crd.yml | 8 ++++++++ .../it/escape-characters/src/test/resources/sample.yaml | 3 +++ 5 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19619630bc1..e2c3e608ff8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ #### Bugs * Fix #5382: [java-generator] Allow to deserialize more valid RFC3339 date-time and make the format customizable +* Fix #5380: [java-generator] Avoid to emit Java Keywords as package names ### 6.8.0 (2023-07-24) diff --git a/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/AbstractJSONSchema2Pojo.java b/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/AbstractJSONSchema2Pojo.java index cf7f537b8d3..ff2c7b630bb 100644 --- a/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/AbstractJSONSchema2Pojo.java +++ b/java-generator/core/src/main/java/io/fabric8/java/generator/nodes/AbstractJSONSchema2Pojo.java @@ -80,6 +80,11 @@ public static String packageName(String str) { if (pkg.equals(str)) { // avoid package/class name clash pkg = "_" + pkg; } + // https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html + // if the package name contains a reserved Java keyword ... the suggested convention is to add an underscore + if (JAVA_KEYWORDS.contains(pkg)) { + pkg = pkg + "_"; + } return pkg; } diff --git a/java-generator/it/src/it/escape-characters/src/test/java/io/fabric8/it/dummy/TestEscapeCharacters.java b/java-generator/it/src/it/escape-characters/src/test/java/io/fabric8/it/dummy/TestEscapeCharacters.java index 639cca14511..fde2608bb0f 100644 --- a/java-generator/it/src/it/escape-characters/src/test/java/io/fabric8/it/dummy/TestEscapeCharacters.java +++ b/java-generator/it/src/it/escape-characters/src/test/java/io/fabric8/it/dummy/TestEscapeCharacters.java @@ -18,6 +18,8 @@ import com.fasterxml.jackson.databind.JsonNode; import com.example.v1.Dummy; import com.example.v1.DummySpec; +import com.example.v1.dummyspec.Package; +import com.example.v1.dummyspec.package_.Foo; import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.client.utils.Serialization; import org.junit.jupiter.api.Test; @@ -48,6 +50,7 @@ void testDeserialization() { assertEquals("3", spec.getThree_quote()); assertEquals("4", spec.getFour_doublequote()); assertEquals("5", spec.getFive_slash()); + assertEquals("6", spec.get_package().getFoo().getBar()); } @Test @@ -63,6 +66,11 @@ void testAgainstSample() throws Exception { spec.setThree_quote("3"); spec.setFour_doublequote("4"); spec.setFive_slash("5"); + Foo foo = new Foo(); + foo.setBar("6"); + Package pack = new Package(); + pack.setFoo(foo); + spec.set_package(pack); sample.setSpec(spec); ObjectMeta om = new ObjectMeta(); om.setName("sample"); diff --git a/java-generator/it/src/it/escape-characters/src/test/resources/dummy-crd.yml b/java-generator/it/src/it/escape-characters/src/test/resources/dummy-crd.yml index a15c273118d..555c1590a84 100644 --- a/java-generator/it/src/it/escape-characters/src/test/resources/dummy-crd.yml +++ b/java-generator/it/src/it/escape-characters/src/test/resources/dummy-crd.yml @@ -43,6 +43,14 @@ spec: type: string five/slash: type: string + package: + type: object + properties: + foo: + type: object + properties: + bar: + type: string scope: Namespaced names: plural: dummies diff --git a/java-generator/it/src/it/escape-characters/src/test/resources/sample.yaml b/java-generator/it/src/it/escape-characters/src/test/resources/sample.yaml index 545e700f2ac..75e1ecec3e4 100644 --- a/java-generator/it/src/it/escape-characters/src/test/resources/sample.yaml +++ b/java-generator/it/src/it/escape-characters/src/test/resources/sample.yaml @@ -25,3 +25,6 @@ spec: three'quote: "3" four"doublequote: "4" five/slash: "5" + package: + foo: + bar: "6"