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 3e89c4d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@

#### Bugs
* Fix #4666: fixed okhttp calls not explicitly closing
* Fix #4677: [java-generator] Fix default encoding of enums

### 6.3.0 (2022-12-12)

Expand Down
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 3e89c4d

Please sign in to comment.