From 67d3c9619146c5e719ae3f92fde6d01a32f02bc9 Mon Sep 17 00:00:00 2001 From: Steve Hawkins Date: Tue, 20 Jun 2023 09:36:17 -0400 Subject: [PATCH] making pojos editable and adds a toBuiler method alias closes #5135 --- CHANGELOG.md | 1 + .../kubernetes-model-common/pom.xml | 27 ++++++++++++++++++- .../fabric8/kubernetes/model/util/Dummy.java | 27 +++++++++++++++++++ .../fabric8/kubernetes/api/model/AnyType.java | 4 +-- .../api/model/GenericKubernetesResource.java | 2 +- .../kubernetes/api/model/IntOrString.java | 2 +- .../kubernetes/api/model/KubernetesList.java | 2 +- .../kubernetes/api/model/Quantity.java | 4 +-- .../api/model/runtime/RawExtension.java | 2 +- .../KubernetesCoreTypeAnnotator.java | 27 +++++++++++++++++-- 10 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/util/Dummy.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a7136cf93a..aab6823fd72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * Fix #5368: added support for additional ListOptions fields * Fix #5377: added a createOr and unlock function to provide a straight-forward replacement for createOrReplace. * Fix #5388: [crd-generator] Generate deterministic CRDs +* Fix #5135: added per instance methods to create a builder - instead on new PodBuilder(pod), you may use pod.toBuilder() * Fix #5257: Add ErrorStreamMessage and StatusStreamMessage to ease mocking of pods/exec requests * Fix #5220: refinements and clarifications to the validation of names diff --git a/kubernetes-model-generator/kubernetes-model-common/pom.xml b/kubernetes-model-generator/kubernetes-model-common/pom.xml index b5e5ad30659..a2152f5dff5 100644 --- a/kubernetes-model-generator/kubernetes-model-common/pom.xml +++ b/kubernetes-model-generator/kubernetes-model-common/pom.xml @@ -67,11 +67,36 @@ ${project.groupId}.${project.artifactId} * - io.fabric8.kubernetes.model** + io.fabric8.kubernetes.model**, + io.fabric8.kubernetes.api.builder** + + maven-antrun-plugin + + + package + + + Removing Unneeded Classes + + + + + + + + + + run + + + + diff --git a/kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/util/Dummy.java b/kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/util/Dummy.java new file mode 100644 index 00000000000..c215d4e55b7 --- /dev/null +++ b/kubernetes-model-generator/kubernetes-model-common/src/main/java/io/fabric8/kubernetes/model/util/Dummy.java @@ -0,0 +1,27 @@ +/** + * 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.kubernetes.model.util; + +import io.sundr.builder.annotations.Buildable; + +/** + * Only exists to trigger builder package generation + */ +@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, builderPackage = "io.fabric8.kubernetes.api.builder") +class Dummy { + +} diff --git a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/AnyType.java b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/AnyType.java index 4850c823141..3c0300f3b39 100644 --- a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/AnyType.java +++ b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/AnyType.java @@ -40,7 +40,7 @@ @ToString @Setter @EqualsAndHashCode -@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder") +@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder") public class AnyType implements Serializable { protected Object value; @@ -50,7 +50,7 @@ public AnyType() { @JsonCreator //Builders are generated for the first non-empty constructor found. - @Buildable(editableEnabled = false, generateBuilderPackage = true, builderPackage = "io.fabric8.kubernetes.api.builder") + @Buildable(editableEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder") public AnyType(Object value) { this.value = value; } diff --git a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/GenericKubernetesResource.java b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/GenericKubernetesResource.java index 684f1a6fa27..e4491dd27bf 100644 --- a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/GenericKubernetesResource.java +++ b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/GenericKubernetesResource.java @@ -46,7 +46,7 @@ @Setter @ToString @EqualsAndHashCode -@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder") +@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder") public class GenericKubernetesResource implements HasMetadata { private static final ObjectMapper MAPPER = new ObjectMapper(); diff --git a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/IntOrString.java b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/IntOrString.java index 8638dc26438..a76779786fd 100644 --- a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/IntOrString.java +++ b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/IntOrString.java @@ -25,7 +25,7 @@ public IntOrString() { @JsonCreator //Builders are generated for the first non-empty constructor found. - @Buildable(editableEnabled = false, generateBuilderPackage = true, builderPackage = "io.fabric8.kubernetes.api.builder") + @Buildable(editableEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder") public IntOrString(Object value) { setValue(value); } diff --git a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/KubernetesList.java b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/KubernetesList.java index fa003b8a928..b31f9210423 100644 --- a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/KubernetesList.java +++ b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/KubernetesList.java @@ -54,7 +54,7 @@ @Group("") @Kind("List") @JsonDeserialize(using = JsonDeserializer.None.class) -@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, builderPackage = "io.fabric8.kubernetes.api.builder") +@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder") public class KubernetesList extends DefaultKubernetesResourceList implements KubernetesResource { @JsonIgnore diff --git a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/Quantity.java b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/Quantity.java index c79c998f8c1..598f1d0afed 100644 --- a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/Quantity.java +++ b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/Quantity.java @@ -53,7 +53,7 @@ "_", "" }) -@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, builderPackage = "io.fabric8.kubernetes.api.builder") +@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder") public class Quantity implements Serializable { private String amount; private String format = ""; @@ -156,7 +156,7 @@ public static BigDecimal getAmountInBytes(Quantity quantity) throws ArithmeticEx /** * Constructs a new Quantity from the provided amountInBytes. This amount is converted * to a value with the unit provided in desiredFormat. - * + * * @param amountInBytes * @param desiredFormat * @see #getNumericalAmount() diff --git a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/runtime/RawExtension.java b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/runtime/RawExtension.java index cd86f06bcd5..8ec14482753 100644 --- a/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/runtime/RawExtension.java +++ b/kubernetes-model-generator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/runtime/RawExtension.java @@ -25,7 +25,7 @@ @ToString(callSuper = true) @JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class) -@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder") +@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder") public class RawExtension extends AnyType implements KubernetesResource { public RawExtension() { diff --git a/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/KubernetesCoreTypeAnnotator.java b/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/KubernetesCoreTypeAnnotator.java index 4bdae804efd..97404556a20 100644 --- a/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/KubernetesCoreTypeAnnotator.java +++ b/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/KubernetesCoreTypeAnnotator.java @@ -15,6 +15,7 @@ */ package io.fabric8.kubernetes.jsonschema2pojo; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.annotation.JsonUnwrapped; @@ -24,12 +25,18 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.sun.codemodel.JAnnotationArrayMember; import com.sun.codemodel.JAnnotationUse; +import com.sun.codemodel.JClass; import com.sun.codemodel.JClassAlreadyExistsException; import com.sun.codemodel.JCodeModel; import com.sun.codemodel.JDefinedClass; +import com.sun.codemodel.JExpr; import com.sun.codemodel.JExpressionImpl; import com.sun.codemodel.JFieldVar; import com.sun.codemodel.JFormatter; +import com.sun.codemodel.JInvocation; +import com.sun.codemodel.JMethod; +import com.sun.codemodel.JMod; +import io.fabric8.kubernetes.api.builder.Editable; import io.fabric8.kubernetes.model.annotation.Group; import io.fabric8.kubernetes.model.annotation.Version; import io.fabric8.kubernetes.model.jackson.JsonUnwrappedDeserializer; @@ -55,6 +62,8 @@ public class KubernetesCoreTypeAnnotator extends Jackson2Annotator { + private static final String BUILDER_PACKAGE = "io.fabric8.kubernetes.api.builder"; + public static final String BUILDABLE_REFERENCE_VALUE = "value"; protected static final String ANNOTATION_VALUE = "value"; @@ -97,6 +106,7 @@ public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) { clazz.annotate(EqualsAndHashCode.class); clazz.annotate(Setter.class); clazz.annotate(Accessors.class).paramArray("prefix").param("_").param(""); + makeEditable(clazz); processBuildable(clazz); final Map fields = clazz.fields(); @@ -135,6 +145,19 @@ public void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode) { } } + private void makeEditable(JDefinedClass clazz) { + JClass builderType = clazz.owner().ref(clazz.fullName() + "Builder"); + JClass editableType = clazz.owner().ref(Editable.class).narrow(builderType); + clazz._implements(editableType); + JMethod editMethod = clazz.method(JMod.PUBLIC , builderType, "edit"); + editMethod.annotate(JsonIgnore.class); + JInvocation newBuilder = JExpr._new(builderType).arg(JExpr._this()); + editMethod.body()._return(newBuilder); + JMethod toBuilderMethod = clazz.method(JMod.PUBLIC , builderType, "toBuilder"); + toBuilderMethod.annotate(JsonIgnore.class); + toBuilderMethod.body()._return(JExpr.invoke("edit")); + } + private void annotate(JDefinedClass clazz, String apiVersion, String apiGroup) { clazz.annotate(Version.class).param(ANNOTATION_VALUE, apiVersion); clazz.annotate(Group.class).param(ANNOTATION_VALUE, apiGroup); @@ -199,7 +222,7 @@ protected void processBuildable(JDefinedClass clazz) { .param("validationEnabled", false) .param("generateBuilderPackage", generateBuilderPackage()) .param("lazyCollectionInitEnabled", false) - .param("builderPackage", "io.fabric8.kubernetes.api.builder"); + .param("builderPackage", BUILDER_PACKAGE); List types = new ArrayList<>(); addBuildableTypes(clazz, types); @@ -217,7 +240,7 @@ protected void processBuildable(JDefinedClass clazz) { } protected boolean generateBuilderPackage() { - return true; + return false; } protected void addBuildableTypes(JDefinedClass clazz, List types) {