Skip to content

Commit fa70292

Browse files
committed
feat: extend the existingJavaTypes to support use of existing enumerations fabric8io#7045
Signed-off-by: Keith Wall <[email protected]>
1 parent 4963c71 commit fa70292

File tree

7 files changed

+57
-4
lines changed

7 files changed

+57
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#### New Features
3131
* Fix #6827: (crd-generator) Add CRDPostProcessor to process generated CRDs before they are written out
32+
* Fix #7045: (java-generator) Extend the existingJavaTypes to support use of existing enumerations
3233

3334
#### _**Note**_: Breaking changes
3435

java-generator/core/src/main/java/io/fabric8/java/generator/nodes/AbstractJSONSchema2Pojo.java

+1
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ private static AbstractJSONSchema2Pojo fromJsonSchema(
327327
throw new JavaGeneratorException("Unsupported enumeration type/format: " + prop.getType() + "/" + prop.getFormat());
328328
}
329329
return new JEnum(
330+
parentPkg,
330331
key,
331332
enumType,
332333
prop.getEnum(),

java-generator/core/src/main/java/io/fabric8/java/generator/nodes/JEnum.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,29 @@ public class JEnum extends AbstractJSONSchema2Pojo {
4747
private final String underlyingType;
4848
private final Set<String> values; //Let's prevent duplicates
4949

50-
public JEnum(String type, String underlyingType, List<JsonNode> values, Config config, String description,
51-
final boolean isNullable,
52-
JsonNode defaultValue) {
50+
// Used for matching against existing types.
51+
private final String pkgPrefixedType;
52+
53+
public JEnum(String pkg, String type, String underlyingType, List<JsonNode> values, Config config, String description,
54+
final boolean isNullable, JsonNode defaultValue) {
5355
super(config, description, isNullable, defaultValue, null);
5456
this.type = AbstractJSONSchema2Pojo.sanitizeString(
5557
type.substring(0, 1).toUpperCase() + type.substring(1));
5658
this.underlyingType = underlyingType;
5759
//Tests assume order so let's use LinkedHashSet instead of just using Collectors.toSet()
5860
this.values = values.stream().map(JsonNode::asText).collect(Collectors.toCollection(LinkedHashSet::new));
61+
this.pkgPrefixedType = createPackagePrefixedType(pkg, this.type);
62+
}
63+
64+
private String createPackagePrefixedType(String pkg, String type) {
65+
String p = (pkg == null) ? "" : pkg.trim();
66+
String pkgPrefix = (p.isEmpty()) ? p : p + ".";
67+
return pkgPrefix + type;
5968
}
6069

6170
@Override
6271
public String getType() {
63-
return this.type;
72+
return config.getExistingJavaTypes().getOrDefault(this.pkgPrefixedType, this.type);
6473
}
6574

6675
private String sanitizeEnumEntry(final String str) {
@@ -93,6 +102,10 @@ private Statement generateBooleanCreator(boolean hasTrue, boolean hasFalse) {
93102

94103
@Override
95104
public GeneratorResult generateJava() {
105+
if (config.getExistingJavaTypes().containsKey(pkgPrefixedType)) {
106+
return new GeneratorResult(Collections.emptyList());
107+
}
108+
96109
CompilationUnit cu = new CompilationUnit();
97110
EnumDeclaration en = cu.addEnum(this.type);
98111

java-generator/core/src/test/java/io/fabric8/java/generator/GeneratorTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ void testDefaultEnum() {
448448
enumValues.add(new TextNode("baz"));
449449
props.put("e1", newEnum);
450450
JEnum enu = new JEnum(
451+
"pkg",
451452
"t",
452453
JAVA_LANG_STRING,
453454
enumValues,
@@ -484,6 +485,7 @@ void testLongEnum() {
484485
enumValues.add(new TextNode("3"));
485486
props.put("e1", newEnum);
486487
JEnum enu = new JEnum(
488+
"pkg",
487489
"t",
488490
JAVA_LANG_LONG,
489491
enumValues,
@@ -524,6 +526,7 @@ void testIntEnum() {
524526
enumValues.add(new TextNode("3"));
525527
props.put("e1", newEnum);
526528
JEnum enu = new JEnum(
529+
"pkg",
527530
"t",
528531
JAVA_LANG_INTEGER,
529532
enumValues,
@@ -562,6 +565,7 @@ void testBooleanEnum() {
562565
enumValues.add(new TextNode("false"));
563566
props.put("e1", newEnum);
564567
JEnum enu = new JEnum(
568+
"pkg",
565569
"t",
566570
JAVA_PRIMITIVE_BOOLEAN,
567571
enumValues,
@@ -600,6 +604,7 @@ void testNotUppercaseEnum() {
600604
enumValues.add(new TextNode("baz"));
601605
props.put("e1", newEnum);
602606
JEnum enu = new JEnum(
607+
"pkg",
603608
"t",
604609
JAVA_LANG_STRING,
605610
enumValues,
@@ -1087,4 +1092,27 @@ void testExistingJavaTypeObject() {
10871092
assertEquals("org.test.ExistingJavaType", obj.getType());
10881093
assertEquals(0, res.getTopLevelClasses().size());
10891094
}
1095+
1096+
@Test
1097+
void testExistingJavaTypeEnum() {
1098+
// Arrange
1099+
Config config = Config.builder()
1100+
.existingJavaTypes(Collections.singletonMap("v1alpha1.E", "org.test.ExistingJavaEnum")).build();
1101+
JEnum obj = new JEnum(
1102+
"v1alpha1",
1103+
"E",
1104+
JAVA_LANG_STRING,
1105+
List.of(),
1106+
config,
1107+
null,
1108+
Boolean.FALSE,
1109+
null);
1110+
1111+
// Act
1112+
GeneratorResult res = obj.generateJava();
1113+
1114+
// Assert
1115+
assertEquals("org.test.ExistingJavaEnum", obj.getType());
1116+
assertEquals(0, res.getTopLevelClasses().size());
1117+
}
10901118
}

java-generator/it/src/it/existing-java-types/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,14 @@
7272
<source>src/main/resources/existing-java-type-crd.yml</source>
7373
<generatedAnnotations>false</generatedAnnotations>
7474
<existingJavaTypes>
75+
<!-- Java Class -->
7576
<com.example.v1.existingjavatypespec.Affinity>
7677
io.fabric8.kubernetes.api.model.Affinity
7778
</com.example.v1.existingjavatypespec.Affinity>
79+
<!-- Java Enum -->
80+
<com.example.v1.existingjavatypespec.DeletionPropagation>
81+
io.fabric8.kubernetes.api.model.DeletionPropagation
82+
</com.example.v1.existingjavatypespec.DeletionPropagation>
7883
</existingJavaTypes>
7984
</configuration>
8085
</plugin>

java-generator/it/src/it/existing-java-types/src/main/java/io/fabric8/test/ExistingJavaTypes.java

+2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
import com.example.v1.ExistingJavaTypeSpec;
1919
import io.fabric8.kubernetes.api.model.Affinity;
20+
import io.fabric8.kubernetes.api.model.DeletionPropagation;
2021

2122
public class ExistingJavaTypes {
2223
public void example() {
2324
ExistingJavaTypeSpec existingJavaTypeSpec = new ExistingJavaTypeSpec();
2425
existingJavaTypeSpec.setAffinity(new Affinity());
26+
existingJavaTypeSpec.setDeletionPropagation(DeletionPropagation.ORPHAN);
2527
}
2628
}

java-generator/it/src/it/existing-java-types/src/main/resources/existing-java-type-crd.yml

+3
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ spec:
3737
properties:
3838
affinity:
3939
type: object
40+
deletionPropagation:
41+
type: string
42+
enum: [ "Orphan" ]

0 commit comments

Comments
 (0)