Skip to content

Commit

Permalink
[java-generator] Correctly handle defaults for IntOrString types (#5581)
Browse files Browse the repository at this point in the history
* [java-generator] Correctly handle defaults for IntOrString types

* changelog
  • Loading branch information
andreaTP authored Nov 13, 2023
1 parent 5ad76bd commit 46ef7e9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#### Bugs

* Fix #5580: [java-generator] Correctly handle defaults for IntOrString types

#### Improvements
* Fix #5535: Add lombok and sundrio dependencies to the generated bom

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.stream.Collectors;

import static io.fabric8.java.generator.nodes.JPrimitiveNameAndType.DATETIME_NAME;
import static io.fabric8.java.generator.nodes.JPrimitiveNameAndType.INT_OR_STRING;

public class JObject extends AbstractJSONSchema2Pojo implements JObjectExtraAnnotations {

Expand Down Expand Up @@ -361,6 +362,11 @@ private Expression generatePrimitiveDefaultInitializerExpression(AbstractJSONSch
} else if (prop.getClassType().equals(DATETIME_NAME) && prop.getDefaultValue().isTextual()) {
return new NameExpr(DATETIME_NAME + ".parse(" + prop.getDefaultValue()
+ ", java.time.format.DateTimeFormatter.ofPattern(\"" + config.getDeserDatetimeFormat() + "\"))");
} else if (prop.getClassType().equals(INT_OR_STRING.getName())) {
return (prop.getDefaultValue().isInt())
? new NameExpr("new " + INT_OR_STRING.getName() + "(" + prop.getDefaultValue().intValue() + ")")
: new NameExpr("new " + INT_OR_STRING.getName() + "(\""
+ StringEscapeUtils.escapeJava(prop.getDefaultValue().asText()) + "\")");
} else {
return new NameExpr(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import io.cert_manager.v1.CertificateRequest;
import io.cert_manager.v1.certificaterequestspec.Ten;
import io.fabric8.kubernetes.api.model.IntOrString;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -52,6 +53,8 @@ void testDefaultValues() throws Exception {
Eleven eleven = cr.getSpec().getEleven();
ZonedDateTime twelve = cr.getSpec().getTwelve();
Thirteen thirteen = cr.getSpec().getThirteen();
IntOrString fourteen = cr.getSpec().getFourteen();
IntOrString fifteen = cr.getSpec().getFifteen();

// Assert
assertEquals("one", one);
Expand All @@ -71,5 +74,7 @@ void testDefaultValues() throws Exception {
assertEquals(ZonedDateTime.parse("2017-07-21T17:32:28Z", formatter), twelve);
assertEquals(Thirteen.V__302, thirteen);
assertEquals(302L, thirteen.getValue());
assertEquals("10Gi", fourteen.getStrVal());
assertEquals(11, fifteen.getIntVal());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,17 @@ spec:
- 302
- 303
default: 302
fourteen:
anyOf:
- type: integer
- type: string
x-kubernetes-int-or-string: true
default: 10Gi
fifteen:
anyOf:
- type: integer
- type: string
x-kubernetes-int-or-string: true
default: 11
served: true
storage: true

0 comments on commit 46ef7e9

Please sign in to comment.