Skip to content

Commit

Permalink
Fix primitive types encoding of default values
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP authored and manusa committed Nov 16, 2022
1 parent fc0bda5 commit 3edb1b2
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,19 @@ public GeneratorResult generateJava() {
}

if (prop.getDefaultValue() != null) {
objField.getVariable(0).setInitializer(generateDefaultInitializerExpression(prop));
Expression primitiveDefault = generatePrimitiveDefaultInitializerExpression(prop);

if (primitiveDefault != null) {
objField.getVariable(0).setInitializer(primitiveDefault);
} else {
objField.getVariable(0).setInitializer(
new NameExpr(
"io.fabric8.kubernetes.client.utils.Serialization.unmarshal("
+ "\"" + StringEscapeUtils.escapeJava(Serialization.asJson(prop.getDefaultValue())) + "\""
+ ", "
+ prop.getClassType() + ".class"
+ ")"));
}
}
} catch (Exception cause) {
throw new JavaGeneratorException(
Expand Down Expand Up @@ -286,17 +298,25 @@ public GeneratorResult generateJava() {
}

/**
* This method is responsible for creating an expression that will initialize the default value.
* This method is responsible for creating an expression that will initialize the default value if primitive
*
* @return a {@link Expression} instance that contains a call to the
* {@link Serialization#unmarshal(String, Class)} method.
*/
private Expression generateDefaultInitializerExpression(AbstractJSONSchema2Pojo prop) {
return new NameExpr(
"io.fabric8.kubernetes.client.utils.Serialization.unmarshal("
+ "\"" + StringEscapeUtils.escapeJava(Serialization.asJson(prop.getDefaultValue())) + "\""
+ ", "
+ prop.getClassType() + ".class"
+ ")");
private Expression generatePrimitiveDefaultInitializerExpression(AbstractJSONSchema2Pojo prop) {
if (prop.getDefaultValue().isValueNode()) {
String value = prop.getDefaultValue().toString();
if (prop.getClassType().equals("Long") && prop.getDefaultValue().canConvertToLong()) {
return new LongLiteralExpr(value + "L");
} else if (prop.getClassType().equals("Float") && prop.getDefaultValue().isFloatingPointNumber()) {
return new DoubleLiteralExpr(value + "f");
} else if (prop.getClassType().equals("Boolean") && prop.getDefaultValue().isBoolean()) {
return new BooleanLiteralExpr(prop.getDefaultValue().booleanValue());
} else {
return new NameExpr(value);
}
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
@io.fabric8.generator.annotation.Pattern("^(off)$|^(102[4-9]|10[3-9][0-9]|1[1-9][0-9]{2}|[2-9][0-9]{3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$")
@com.fasterxml.jackson.annotation.JsonPropertyDescription("Akka Management (Cluster Bootstrap) HTTP port in the range 1024 - 65535. Can be disabled with \"off\". This port is exposed as HTTP_MGMT_PORT environment variable if it is enabled.")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private String akkaManagementPort = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("\"8558\"", String.class);
private String akkaManagementPort = "8558";

public String getAkkaManagementPort() {
return akkaManagementPort;
Expand Down Expand Up @@ -186,7 +186,7 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
@io.fabric8.generator.annotation.Pattern("^(off)$|^(102[4-9]|10[3-9][0-9]|1[1-9][0-9]{2}|[2-9][0-9]{3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$")
@com.fasterxml.jackson.annotation.JsonPropertyDescription("gRPC port in the range 1024 - 65535. Can be disabled with \"off\". This port is exposed as GRPC_PORT environment variable if it is enabled.")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private String grpcPort = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("\"8101\"", String.class);
private String grpcPort = "8101";

public String getGrpcPort() {
return grpcPort;
Expand Down Expand Up @@ -219,7 +219,7 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
@io.fabric8.generator.annotation.Pattern("^(off)$|^(102[4-9]|10[3-9][0-9]|1[1-9][0-9]{2}|[2-9][0-9]{3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$")
@com.fasterxml.jackson.annotation.JsonPropertyDescription("HTTP port in the range 1024 - 65535. Disabled by default. This port is exposed as HTTP_PORT environment variable if it is enabled.")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private String httpPort = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("\"off\"", String.class);
private String httpPort = "off";

public String getHttpPort() {
return httpPort;
Expand Down Expand Up @@ -285,7 +285,7 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
@com.fasterxml.jackson.annotation.JsonProperty("javaOptions")
@com.fasterxml.jackson.annotation.JsonPropertyDescription("Additional arguments to the application JVM. It will be added to the `JAVA_TOOL_OPTIONS` environment variable, which will be used by most JVM implementations.")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private String javaOptions = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("\"\"", String.class);
private String javaOptions = "";

public String getJavaOptions() {
return javaOptions;
Expand Down Expand Up @@ -398,7 +398,7 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
@io.fabric8.generator.annotation.Pattern("^(off)$|^(102[4-9]|10[3-9][0-9]|1[1-9][0-9]{2}|[2-9][0-9]{3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$")
@com.fasterxml.jackson.annotation.JsonPropertyDescription("HTTP port to expose metrics for Prometheus to scrape. Must be in the range 1024 - 65535. Disabled by default. This port is exposed as HTTP_PROMETHEUS_PORT environment variable if it is enabled.")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private String prometheusPort = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("\"off\"", String.class);
private String prometheusPort = "off";

public String getPrometheusPort() {
return prometheusPort;
Expand Down Expand Up @@ -432,7 +432,7 @@ public class AkkaMicroserviceSpec implements io.fabric8.kubernetes.api.model.Kub
@io.fabric8.generator.annotation.Min(0.0)
@com.fasterxml.jackson.annotation.JsonPropertyDescription("Number of desired pods.")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
private Long replicas = io.fabric8.kubernetes.client.utils.Serialization.unmarshal("1", Long.class);
private Long replicas = 1L;

public Long getReplicas() {
return replicas;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

invoker.goals=test
106 changes: 106 additions & 0 deletions java-generator/it/src/it/default-values-instantiation/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2015 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project>

<modelVersion>4.0.0</modelVersion>

<artifactId>default-values-instantiation</artifactId>
<groupId>io.fabric8.it</groupId>
<version>0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>@maven.compiler.source@</maven.compiler.source>
<maven.compiler.target>@maven.compiler.target@</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>io.sundr</groupId>
<artifactId>sundr-adapter-reflect</artifactId>
<version>@sundrio.version@</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.sundr</groupId>
<artifactId>builder-annotations</artifactId>
<version>@sundrio.version@</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>@lombok.version@</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>java-generator-integration-tests</artifactId>
<version>@project.version@</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>generator-annotations</artifactId>
<version>@project.version@</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>@junit.version@</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>@junit.version@</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>java-generator-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<source>src/test/resources/example.yaml</source>
<generatedAnnotations>false</generatedAnnotations>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<useFile>false</useFile>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.it.certmanager;

import com.fasterxml.jackson.databind.JsonNode;
import io.cert_manager.v1.CertificateRequest;
import io.cert_manager.v1.certificaterequestspec.Ten;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

class TestDefaultValues {

@Test
void testDefaultValues() throws Exception {
// Arrange
CertificateRequest cr =
Serialization.unmarshal(getClass().getResourceAsStream("/empty.yaml"), CertificateRequest.class);

// Act
String one = cr.getSpec().getOne();
Boolean two = cr.getSpec().getTwo();
Integer three = cr.getSpec().getThree();
Long four = cr.getSpec().getFour();
Long five = cr.getSpec().getFive();
Float six = cr.getSpec().getSix();
Double seven = cr.getSpec().getSeven();
Double eight = cr.getSpec().getEight();
List<String> nine = cr.getSpec().getNine();
Ten ten = cr.getSpec().getTen();

// Assert
assertEquals("one", one);
assertEquals(true, two);
assertEquals(3, three);
assertEquals(4L, four);
assertEquals(5L, five);
assertEquals(6,1f, six);
assertEquals(7.2d, seven);
assertEquals(8.2d, eight);
assertEquals(2, nine.size());
assertEquals("nine1", nine.get(0));
assertEquals("nine2", nine.get(1));
assertEquals("tenone", ten.getTenOne());
assertEquals("tentwo", ten.getTenTwo());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

apiVersion: cert-manager.io/v1
kind: CertificateRequest
metadata:
name: my-ca-cr
spec: {}

0 comments on commit 3edb1b2

Please sign in to comment.