Skip to content

Commit db593a1

Browse files
authored
feat(java-generator): boolean enum support
1 parent 63bbe56 commit db593a1

File tree

8 files changed

+156
-1
lines changed

8 files changed

+156
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#### Bugs
66
* Fix #6892: rolling().restart() doesn't remove preexistent pod template annotations
77
* Fix #6906: Knative VolatileTime should be serialized as String
8+
* Fix #6930: Add support for Boolean enums in the java-generator
89

910
#### Improvements
1011
* Fix #6863: ensuring SerialExecutor does not throw RejectedExecutionException to prevent unnecessary error logs

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,11 @@ private static AbstractJSONSchema2Pojo fromJsonSchema(
320320
break;
321321
case STRING_CRD_TYPE:
322322
break;
323+
case BOOLEAN_CRD_TYPE:
324+
enumType = JAVA_PRIMITIVE_BOOLEAN;
325+
break;
323326
default:
324-
throw new JavaGeneratorException("Unsupported enumeration type/format" + prop.getType() + "/" + prop.getFormat());
327+
throw new JavaGeneratorException("Unsupported enumeration type/format: " + prop.getType() + "/" + prop.getFormat());
325328
}
326329
return new JEnum(
327330
key,

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import com.github.javaparser.ast.body.*;
2222
import com.github.javaparser.ast.expr.*;
2323
import com.github.javaparser.ast.stmt.BlockStmt;
24+
import com.github.javaparser.ast.stmt.IfStmt;
2425
import com.github.javaparser.ast.stmt.ReturnStmt;
26+
import com.github.javaparser.ast.stmt.Statement;
2527
import io.fabric8.java.generator.Config;
2628

2729
import java.util.ArrayList;
@@ -35,6 +37,7 @@
3537

3638
import static io.fabric8.java.generator.nodes.Keywords.JAVA_LANG_LONG;
3739
import static io.fabric8.java.generator.nodes.Keywords.JAVA_LANG_STRING;
40+
import static io.fabric8.java.generator.nodes.Keywords.JAVA_PRIMITIVE_BOOLEAN;
3841

3942
public class JEnum extends AbstractJSONSchema2Pojo {
4043

@@ -72,6 +75,22 @@ private String sanitizeEnumEntry(final String str) {
7275
}
7376
}
7477

78+
private Statement generateBooleanCreator(boolean hasTrue, boolean hasFalse) {
79+
IfStmt result = new IfStmt();
80+
result.setCondition(new NameExpr("value"));
81+
if (hasTrue) {
82+
result.setThenStmt(new ReturnStmt(new NameExpr(this.type + ".TRUE")));
83+
} else {
84+
result.setThenStmt(new ReturnStmt(new NullLiteralExpr()));
85+
}
86+
if (hasFalse) {
87+
result.setElseStmt(new ReturnStmt(new NameExpr(this.type + ".FALSE")));
88+
} else {
89+
result.setElseStmt(new ReturnStmt(new NullLiteralExpr()));
90+
}
91+
return result;
92+
}
93+
7594
@Override
7695
public GeneratorResult generateJava() {
7796
CompilationUnit cu = new CompilationUnit();
@@ -97,6 +116,28 @@ public GeneratorResult generateJava() {
97116
.setBody(new BlockStmt().addStatement(new ReturnStmt(VALUE)));
98117
getValue.addAnnotation("com.fasterxml.jackson.annotation.JsonValue");
99118

119+
if (underlyingType.equals(JAVA_PRIMITIVE_BOOLEAN)) {
120+
MethodDeclaration fromValue = en
121+
.addMethod("fromValue", Modifier.Keyword.PUBLIC, Modifier.Keyword.STATIC);
122+
fromValue.setType(this.type);
123+
fromValue.addParameter(JAVA_PRIMITIVE_BOOLEAN, "value");
124+
fromValue.addAnnotation("com.fasterxml.jackson.annotation.JsonCreator");
125+
126+
boolean hasTrue = false;
127+
boolean hasFalse = false;
128+
for (String v : values) {
129+
boolean value = Boolean.valueOf(v);
130+
if (value) {
131+
hasTrue = true;
132+
} else {
133+
hasFalse = true;
134+
}
135+
}
136+
137+
fromValue.setBody(new BlockStmt().addStatement(
138+
generateBooleanCreator(hasTrue, hasFalse)));
139+
}
140+
100141
Set<String> constantNames = new HashSet<>(values.size());
101142
for (String k : values) {
102143
StringBuilder constantNameBuilder = new StringBuilder();
@@ -136,6 +177,7 @@ public GeneratorResult generateJava() {
136177
decl.setName(constantName);
137178
decl.addArgument(valueArgument);
138179
en.addEntry(decl);
180+
139181
}
140182

141183
return new GeneratorResult(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,6 @@ private Keywords() {
8484
public static final String JAVA_LANG_STRING = "java.lang.String";
8585
public static final String JAVA_LANG_LONG = "java.lang.Long";
8686
public static final String JAVA_LANG_INTEGER = "java.lang.Integer";
87+
public static final String JAVA_PRIMITIVE_BOOLEAN = "boolean";
8788
public static final String ADDITIONAL_PROPERTIES = "additionalProperties";
8889
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,42 @@ void testIntEnum() {
551551
assertEquals("3", en.get().getEntries().get(2).getArgument(0).toString());
552552
}
553553

554+
@Test
555+
void testBooleanEnum() {
556+
// Arrange
557+
Map<String, JSONSchemaProps> props = new HashMap<>();
558+
JSONSchemaProps newEnum = new JSONSchemaProps();
559+
newEnum.setType("boolean");
560+
List<JsonNode> enumValues = new ArrayList<>();
561+
enumValues.add(new TextNode("true"));
562+
enumValues.add(new TextNode("false"));
563+
props.put("e1", newEnum);
564+
JEnum enu = new JEnum(
565+
"t",
566+
JAVA_PRIMITIVE_BOOLEAN,
567+
enumValues,
568+
defaultConfig,
569+
null,
570+
Boolean.FALSE,
571+
null);
572+
573+
// Act
574+
GeneratorResult res = enu.generateJava();
575+
576+
// Assert
577+
assertEquals("T", enu.getType());
578+
assertEquals(1, res.getInnerClasses().size());
579+
assertEquals("T", res.getInnerClasses().get(0).getName());
580+
581+
Optional<EnumDeclaration> en = res.getInnerClasses().get(0).getEnumByName("T");
582+
assertTrue(en.isPresent());
583+
assertEquals(2, en.get().getEntries().size());
584+
assertEquals("TRUE", en.get().getEntries().get(0).getName().asString());
585+
assertEquals("FALSE", en.get().getEntries().get(1).getName().asString());
586+
assertEquals("true", en.get().getEntries().get(0).getArgument(0).toString());
587+
assertEquals("false", en.get().getEntries().get(1).getArgument(0).toString());
588+
}
589+
554590
@Test
555591
void testNotUppercaseEnum() {
556592
// Arrange

java-generator/it/src/it/ser-deser/src/test/java/io/fabric8/it/certmanager/TestSerialization.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
import com.fasterxml.jackson.databind.JsonNode;
1919
import io.cert_manager.v1.CertificateRequest;
2020
import io.cert_manager.v1.CertificateRequestSpec;
21+
import io.cert_manager.v1.CertificateRequestSpec.BooleanEnum;
2122
import io.cert_manager.v1.CertificateRequestSpec.IntEnum;
2223
import io.cert_manager.v1.CertificateRequestSpec.LongEnum;
24+
import io.cert_manager.v1.CertificateRequestSpec.OnlyFalseBoolEnum;
25+
import io.cert_manager.v1.CertificateRequestSpec.OnlyTrueBoolEnum;
2326
import io.fabric8.java.generator.testing.KubernetesResourceDiff;
2427
import io.fabric8.kubernetes.client.utils.Serialization;
2528
import org.junit.jupiter.api.Test;
@@ -100,6 +103,23 @@ void testNumericEnum() {
100103
assertEquals(203, intValue.getValue());
101104
}
102105

106+
@Test
107+
void testBooleanEnum() {
108+
// Arrange
109+
CertificateRequest sample8 = Serialization.unmarshal(getClass().getResourceAsStream("/sample9.yaml"),
110+
CertificateRequest.class);
111+
112+
// Act
113+
BooleanEnum booleanValue = sample8.getSpec().getBooleanEnum();
114+
OnlyFalseBoolEnum onlyFalse = sample8.getSpec().getOnlyFalseBoolEnum();
115+
OnlyTrueBoolEnum onlyTrue = sample8.getSpec().getOnlyTrueBoolEnum();
116+
117+
// Assert
118+
assertEquals(true, booleanValue.getValue());
119+
assertEquals(true, onlyTrue.getValue());
120+
assertEquals(false, onlyFalse.getValue());
121+
}
122+
103123
@Test
104124
void testIntEnumSerDeser() throws Exception {
105125
// Arrange
@@ -115,6 +135,21 @@ void testIntEnumSerDeser() throws Exception {
115135
assertEquals(0, aggregatedDiffs.size());
116136
}
117137

138+
@Test
139+
void testBooleanEnumSerDeser() throws Exception {
140+
// Arrange
141+
Path resPath = Paths.get(getClass().getResource("/sample9.yaml").toURI());
142+
String yamlContent = new String(Files.readAllBytes(resPath), "UTF8");
143+
CertificateRequest sample = Serialization.unmarshal(yamlContent, CertificateRequest.class);
144+
KubernetesResourceDiff diff = new KubernetesResourceDiff(yamlContent, Serialization.asYaml(sample));
145+
146+
// Act
147+
List<JsonNode> aggregatedDiffs = diff.getListOfDiffs();
148+
149+
// Assert
150+
assertEquals(0, aggregatedDiffs.size());
151+
}
152+
118153
@Test
119154
void testInvalidAlternativeDateDeserialization() {
120155
assertThrows(IllegalArgumentException.class,

java-generator/it/src/it/ser-deser/src/test/resources/cert-manager.crds.1.7.1.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@ spec:
136136
uid:
137137
description: UID contains the uid of the user that created the CertificateRequest. Populated by the cert-manager webhook on creation and immutable.
138138
type: string
139+
booleanEnum:
140+
type: boolean
141+
enum:
142+
- true
143+
- false
144+
onlyTrueBoolEnum:
145+
type: boolean
146+
enum:
147+
- true
148+
onlyFalseBoolEnum:
149+
type: boolean
150+
enum:
151+
- false
139152
longEnum:
140153
type: integer
141154
enum:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# Copyright (C) 2015 Red Hat, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
apiVersion: cert-manager.io/v1
18+
kind: CertificateRequest
19+
metadata:
20+
name: my-ca-cr
21+
spec:
22+
booleanEnum: true
23+
onlyTrueBoolEnum: true
24+
onlyFalseBoolEnum: false

0 commit comments

Comments
 (0)