Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#### New Features

* Fix #7045: (java-generator) Extend the existingJavaTypes to support use of existing enumerations

#### _**Note**_: Breaking changes

### 6.14.0 (2025-06-10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ private static AbstractJSONSchema2Pojo fromJsonSchema(
throw new JavaGeneratorException("Unsupported enumeration type/format: " + prop.getType() + "/" + prop.getFormat());
}
return new JEnum(
parentPkg,
key,
enumType,
prop.getEnum(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,38 @@ public class JEnum extends AbstractJSONSchema2Pojo {
private final String underlyingType;
private final Set<String> values; //Let's prevent duplicates

public JEnum(String type, String underlyingType, List<JsonNode> values, Config config, String description,
final boolean isNullable,
JsonNode defaultValue) {
// Used for matching against existing types.
private final String pkgPrefixedType;

public JEnum(String pkg, String type, String underlyingType, List<JsonNode> values, Config config, String description,
final boolean isNullable, JsonNode defaultValue) {
super(config, description, isNullable, defaultValue, null);
this.type = AbstractJSONSchema2Pojo.sanitizeString(
type.substring(0, 1).toUpperCase() + type.substring(1));
this.underlyingType = underlyingType;
//Tests assume order so let's use LinkedHashSet instead of just using Collectors.toSet()
this.values = values.stream().map(JsonNode::asText).collect(Collectors.toCollection(LinkedHashSet::new));
this.pkgPrefixedType = createPackagePrefixedType(pkg, this.type);
}

/**
* @deprecated use {@link #JEnum(String, String, String, List, Config, String, boolean, JsonNode)}
*/
@Deprecated
public JEnum(String type, String underlyingType, List<JsonNode> values, Config config, String description,
final boolean isNullable, JsonNode defaultValue) {
this(null, type, underlyingType, values, config, description, isNullable, defaultValue);
}

private String createPackagePrefixedType(String pkg, String type) {
String p = (pkg == null) ? "" : pkg.trim();
String pkgPrefix = (p.isEmpty()) ? p : p + ".";
return pkgPrefix + type;
}

@Override
public String getType() {
return this.type;
return config.getExistingJavaTypes().getOrDefault(this.pkgPrefixedType, this.type);
}

private String sanitizeEnumEntry(final String str) {
Expand Down Expand Up @@ -93,6 +111,10 @@ private Statement generateBooleanCreator(boolean hasTrue, boolean hasFalse) {

@Override
public GeneratorResult generateJava() {
if (config.getExistingJavaTypes().containsKey(pkgPrefixedType)) {
return new GeneratorResult(Collections.emptyList());
}

CompilationUnit cu = new CompilationUnit();
EnumDeclaration en = cu.addEnum(this.type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ void testDefaultEnum() {
enumValues.add(new TextNode("baz"));
props.put("e1", newEnum);
JEnum enu = new JEnum(
"pkg",
"t",
JAVA_LANG_STRING,
enumValues,
Expand Down Expand Up @@ -484,6 +485,7 @@ void testLongEnum() {
enumValues.add(new TextNode("3"));
props.put("e1", newEnum);
JEnum enu = new JEnum(
"pkg",
"t",
JAVA_LANG_LONG,
enumValues,
Expand Down Expand Up @@ -524,6 +526,7 @@ void testIntEnum() {
enumValues.add(new TextNode("3"));
props.put("e1", newEnum);
JEnum enu = new JEnum(
"pkg",
"t",
JAVA_LANG_INTEGER,
enumValues,
Expand Down Expand Up @@ -562,6 +565,7 @@ void testBooleanEnum() {
enumValues.add(new TextNode("false"));
props.put("e1", newEnum);
JEnum enu = new JEnum(
"pkg",
"t",
JAVA_PRIMITIVE_BOOLEAN,
enumValues,
Expand Down Expand Up @@ -600,6 +604,7 @@ void testNotUppercaseEnum() {
enumValues.add(new TextNode("baz"));
props.put("e1", newEnum);
JEnum enu = new JEnum(
"pkg",
"t",
JAVA_LANG_STRING,
enumValues,
Expand All @@ -624,6 +629,32 @@ void testNotUppercaseEnum() {
assertEquals("baz", en.get().getEntries().get(2).getName().asString());
}

@Test
void testEnumDeprecatedConstructor() {
// Arrange
JSONSchemaProps newEnum = new JSONSchemaProps();
newEnum.setType("string");
JEnum enu = new JEnum(
"t",
JAVA_LANG_STRING,
List.of(),
defaultConfig,
null,
Boolean.FALSE,
null);

// Act
GeneratorResult res = enu.generateJava();

// Assert
assertEquals("T", enu.getType());
assertEquals(1, res.getInnerClasses().size());
assertEquals("T", res.getInnerClasses().get(0).getName());

Optional<EnumDeclaration> en = res.getInnerClasses().get(0).getEnumByName("T");
assertTrue(en.isPresent());
}

@Test
void testArrayOfObjects() {
// Arrange
Expand Down Expand Up @@ -1087,4 +1118,27 @@ void testExistingJavaTypeObject() {
assertEquals("org.test.ExistingJavaType", obj.getType());
assertEquals(0, res.getTopLevelClasses().size());
}

@Test
void testExistingJavaTypeEnum() {
// Arrange
Config config = Config.builder()
.existingJavaTypes(Collections.singletonMap("v1alpha1.E", "org.test.ExistingJavaEnum")).build();
JEnum obj = new JEnum(
"v1alpha1",
"E",
JAVA_LANG_STRING,
List.of(),
config,
null,
Boolean.FALSE,
null);

// Act
GeneratorResult res = obj.generateJava();

// Assert
assertEquals("org.test.ExistingJavaEnum", obj.getType());
assertEquals(0, res.getTopLevelClasses().size());
}
}
5 changes: 5 additions & 0 deletions java-generator/it/src/it/existing-java-types/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@
<source>src/main/resources/existing-java-type-crd.yml</source>
<generatedAnnotations>false</generatedAnnotations>
<existingJavaTypes>
<!-- Java Class -->
<com.example.v1.existingjavatypespec.Affinity>
io.fabric8.kubernetes.api.model.Affinity
</com.example.v1.existingjavatypespec.Affinity>
<!-- Java Enum -->
<com.example.v1.existingjavatypespec.DeletionPropagation>
io.fabric8.kubernetes.api.model.DeletionPropagation
</com.example.v1.existingjavatypespec.DeletionPropagation>
</existingJavaTypes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

import com.example.v1.ExistingJavaTypeSpec;
import io.fabric8.kubernetes.api.model.Affinity;
import io.fabric8.kubernetes.api.model.DeletionPropagation;

public class ExistingJavaTypes {
public void example() {
ExistingJavaTypeSpec existingJavaTypeSpec = new ExistingJavaTypeSpec();
existingJavaTypeSpec.setAffinity(new Affinity());
existingJavaTypeSpec.setDeletionPropagation(DeletionPropagation.ORPHAN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ spec:
properties:
affinity:
type: object
deletionPropagation:
type: string
enum: [ "Orphan" ]
Loading