Skip to content

Commit

Permalink
[java-gen] Fix default value encoding for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Dec 14, 2022
1 parent ffcccb5 commit 729a3cb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Expand Up @@ -168,7 +168,8 @@ public GeneratorResult generateJava() {
GeneratorResult gr = prop.generateJava();

// For now the inner types are only for enums
if (!gr.getInnerClasses().isEmpty()) {
boolean isEnum = !gr.getInnerClasses().isEmpty();
if (isEnum) {
for (GeneratorResult.ClassResult enumCR : gr.getInnerClasses()) {
Optional<EnumDeclaration> ed = enumCR.getCompilationUnit().getEnumByName(enumCR.getName());
if (ed.isPresent()) {
Expand Down Expand Up @@ -244,7 +245,7 @@ public GeneratorResult generateJava() {
}

if (prop.getDefaultValue() != null) {
Expression primitiveDefault = generatePrimitiveDefaultInitializerExpression(prop);
Expression primitiveDefault = (isEnum) ? null : generatePrimitiveDefaultInitializerExpression(prop);

if (primitiveDefault != null) {
objField.getVariable(0).setInitializer(primitiveDefault);
Expand Down
Expand Up @@ -23,6 +23,7 @@

import java.util.List;

import static io.cert_manager.v1.CertificateRequestSpec.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

class TestDefaultValues {
Expand All @@ -44,6 +45,7 @@ void testDefaultValues() throws Exception {
Double eight = cr.getSpec().getEight();
List<String> nine = cr.getSpec().getNine();
Ten ten = cr.getSpec().getTen();
Eleven eleven = cr.getSpec().getEleven();

// Assert
assertEquals("one", one);
Expand All @@ -59,5 +61,6 @@ void testDefaultValues() throws Exception {
assertEquals("nine2", nine.get(1));
assertEquals("tenone", ten.getTenOne());
assertEquals("tentwo", ten.getTenTwo());
assertEquals(Eleven.BAZ, eleven);
}
}
Expand Up @@ -90,5 +90,12 @@ spec:
default:
tenOne: "tenone"
tenTwo: "tentwo"
eleven:
type: string
enum:
- "foo"
- "bar"
- "baz"
default: "baz"
served: true
storage: true

0 comments on commit 729a3cb

Please sign in to comment.