From 774f71646e85938971c9297c1b8fcd0c460a3904 Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Mon, 29 Aug 2022 12:05:48 +0200 Subject: [PATCH] chore: Fabric8NameHelper renames fields with reserved keywords Signed-off-by: Marc Nuri --- extensions/certmanager/model-v1/pom.xml | 3 - .../v1/ACMEChallengeSolverHTTP01Ingress.java | 169 ----------------- .../api/model/v1/CertificateRequestSpec.java | 175 ------------------ .../jsonschema2pojo/Fabric8NameHelper.java | 16 +- .../Fabric8NameHelperTest.java | 36 ++++ 5 files changed, 51 insertions(+), 348 deletions(-) delete mode 100644 extensions/certmanager/model-v1/src/main/java/io/fabric8/certmanager/api/model/acme/v1/ACMEChallengeSolverHTTP01Ingress.java delete mode 100644 extensions/certmanager/model-v1/src/main/java/io/fabric8/certmanager/api/model/v1/CertificateRequestSpec.java diff --git a/extensions/certmanager/model-v1/pom.xml b/extensions/certmanager/model-v1/pom.xml index 25b153f4602..babebdc9ef4 100755 --- a/extensions/certmanager/model-v1/pom.xml +++ b/extensions/certmanager/model-v1/pom.xml @@ -93,9 +93,6 @@ removing the duplicate generated class - diff --git a/extensions/certmanager/model-v1/src/main/java/io/fabric8/certmanager/api/model/acme/v1/ACMEChallengeSolverHTTP01Ingress.java b/extensions/certmanager/model-v1/src/main/java/io/fabric8/certmanager/api/model/acme/v1/ACMEChallengeSolverHTTP01Ingress.java deleted file mode 100644 index 1fa9b0a9e0e..00000000000 --- a/extensions/certmanager/model-v1/src/main/java/io/fabric8/certmanager/api/model/acme/v1/ACMEChallengeSolverHTTP01Ingress.java +++ /dev/null @@ -1,169 +0,0 @@ -/** - * 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.certmanager.api.model.acme.v1; - -import com.fasterxml.jackson.annotation.JsonAnyGetter; -import com.fasterxml.jackson.annotation.JsonAnySetter; -import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; -import io.fabric8.kubernetes.api.model.Container; -import io.fabric8.kubernetes.api.model.ContainerPort; -import io.fabric8.kubernetes.api.model.EnvVar; -import io.fabric8.kubernetes.api.model.KubernetesResource; -import io.fabric8.kubernetes.api.model.LabelSelector; -import io.fabric8.kubernetes.api.model.ObjectMeta; -import io.fabric8.kubernetes.api.model.Volume; -import io.fabric8.kubernetes.api.model.VolumeMount; -import io.sundr.builder.annotations.Buildable; -import io.sundr.builder.annotations.BuildableReference; -import lombok.EqualsAndHashCode; -import lombok.Setter; -import lombok.ToString; -import lombok.experimental.Accessors; - -import java.util.HashMap; -import java.util.Map; - -@JsonInclude(JsonInclude.Include.NON_NULL) -@JsonPropertyOrder({ - "class", - "ingressTemplate", - "name", - "podTemplate", - "serviceType" -}) -@JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class) -@ToString -@EqualsAndHashCode -@Setter -@Accessors(prefix = { - "_", - "" -}) -@Buildable(editableEnabled = true, validationEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder", refs = { - @BuildableReference(ObjectMeta.class), - @BuildableReference(LabelSelector.class), - @BuildableReference(Container.class), - @BuildableReference(EnvVar.class), - @BuildableReference(ContainerPort.class), - @BuildableReference(Volume.class), - @BuildableReference(VolumeMount.class) -}) -public class ACMEChallengeSolverHTTP01Ingress implements KubernetesResource -{ - - @JsonProperty("class") - private String className; - @JsonProperty("ingressTemplate") - private ACMEChallengeSolverHTTP01IngressTemplate ingressTemplate; - @JsonProperty("name") - private java.lang.String name; - @JsonProperty("podTemplate") - private ACMEChallengeSolverHTTP01IngressPodTemplate podTemplate; - @JsonProperty("serviceType") - private java.lang.String serviceType; - @JsonIgnore - private Map additionalProperties = new HashMap<>(); - - /** - * No args constructor for use in serialization - * - */ - public ACMEChallengeSolverHTTP01Ingress() { - } - - /** - * - * @param serviceType - * @param ingressTemplate - * @param name - * @param podTemplate - * @param className - */ - public ACMEChallengeSolverHTTP01Ingress(String className, ACMEChallengeSolverHTTP01IngressTemplate ingressTemplate, java.lang.String name, ACMEChallengeSolverHTTP01IngressPodTemplate podTemplate, java.lang.String serviceType) { - super(); - this.className = className; - this.ingressTemplate = ingressTemplate; - this.name = name; - this.podTemplate = podTemplate; - this.serviceType = serviceType; - } - - @JsonProperty("class") - public String getClassName() { - return className; - } - - @JsonProperty("class") - public void setClassName(String _class) { - this.className = _class; - } - - @JsonProperty("ingressTemplate") - public ACMEChallengeSolverHTTP01IngressTemplate getIngressTemplate() { - return ingressTemplate; - } - - @JsonProperty("ingressTemplate") - public void setIngressTemplate(ACMEChallengeSolverHTTP01IngressTemplate ingressTemplate) { - this.ingressTemplate = ingressTemplate; - } - - @JsonProperty("name") - public java.lang.String getName() { - return name; - } - - @JsonProperty("name") - public void setName(java.lang.String name) { - this.name = name; - } - - @JsonProperty("podTemplate") - public ACMEChallengeSolverHTTP01IngressPodTemplate getPodTemplate() { - return podTemplate; - } - - @JsonProperty("podTemplate") - public void setPodTemplate(ACMEChallengeSolverHTTP01IngressPodTemplate podTemplate) { - this.podTemplate = podTemplate; - } - - @JsonProperty("serviceType") - public java.lang.String getServiceType() { - return serviceType; - } - - @JsonProperty("serviceType") - public void setServiceType(java.lang.String serviceType) { - this.serviceType = serviceType; - } - - @JsonAnyGetter - public Map getAdditionalProperties() { - return this.additionalProperties; - } - - @JsonAnySetter - public void setAdditionalProperty(java.lang.String name, Object value) { - this.additionalProperties.put(name, value); - } - -} - diff --git a/extensions/certmanager/model-v1/src/main/java/io/fabric8/certmanager/api/model/v1/CertificateRequestSpec.java b/extensions/certmanager/model-v1/src/main/java/io/fabric8/certmanager/api/model/v1/CertificateRequestSpec.java deleted file mode 100644 index b4b894838f2..00000000000 --- a/extensions/certmanager/model-v1/src/main/java/io/fabric8/certmanager/api/model/v1/CertificateRequestSpec.java +++ /dev/null @@ -1,175 +0,0 @@ -/** - * 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.certmanager.api.model.v1; - - -import com.fasterxml.jackson.annotation.JsonAnyGetter; -import com.fasterxml.jackson.annotation.JsonAnySetter; -import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; -import io.fabric8.kubernetes.api.model.Container; -import io.fabric8.kubernetes.api.model.ContainerPort; -import io.fabric8.kubernetes.api.model.Duration; -import io.fabric8.kubernetes.api.model.EnvVar; -import io.fabric8.kubernetes.api.model.KubernetesResource; -import io.fabric8.kubernetes.api.model.LabelSelector; -import io.fabric8.kubernetes.api.model.ObjectMeta; -import io.fabric8.kubernetes.api.model.Volume; -import io.fabric8.kubernetes.api.model.VolumeMount; -import io.sundr.builder.annotations.Buildable; -import io.sundr.builder.annotations.BuildableReference; -import lombok.EqualsAndHashCode; -import lombok.Setter; -import lombok.ToString; -import lombok.experimental.Accessors; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -@JsonInclude(JsonInclude.Include.NON_NULL) -@JsonPropertyOrder({ - "duration", - "isCA", - "issuerRef", - "request", - "usages" -}) -@JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class) -@ToString -@EqualsAndHashCode -@Setter -@Accessors(prefix = { - "_", - "" -}) -@Buildable(editableEnabled = true, validationEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder", refs = { - @BuildableReference(ObjectMeta.class), - @BuildableReference(io.fabric8.kubernetes.api.model.ObjectReference.class), - @BuildableReference(LabelSelector.class), - @BuildableReference(Container.class), - @BuildableReference(EnvVar.class), - @BuildableReference(ContainerPort.class), - @BuildableReference(Volume.class), - @BuildableReference(VolumeMount.class) -}) -public class CertificateRequestSpec implements KubernetesResource -{ - - @JsonProperty("duration") - private Duration duration; - @JsonProperty("isCA") - private Boolean isCA; - @JsonProperty("issuerRef") - private io.fabric8.certmanager.api.model.meta.v1.ObjectReference issuerRef; - @JsonProperty("request") - private String request; - @JsonProperty("usages") - @JsonInclude(JsonInclude.Include.NON_EMPTY) - private List usages = new ArrayList<>(); - @JsonIgnore - private Map additionalProperties = new HashMap<>(); - - /** - * No args constructor for use in serialization - * - */ - public CertificateRequestSpec() { - } - - /** - * - * @param duration - * @param request - * @param isCA - * @param issuerRef - * @param usages - */ - public CertificateRequestSpec(Duration duration, Boolean isCA, io.fabric8.certmanager.api.model.meta.v1.ObjectReference issuerRef, String request, List usages) { - super(); - this.duration = duration; - this.isCA = isCA; - this.issuerRef = issuerRef; - this.request = request; - this.usages = usages; - } - - @JsonProperty("duration") - public Duration getDuration() { - return duration; - } - - @JsonProperty("duration") - public void setDuration(Duration duration) { - this.duration = duration; - } - - @JsonProperty("isCA") - public Boolean getIsCA() { - return isCA; - } - - @JsonProperty("isCA") - public void setIsCA(Boolean isCA) { - this.isCA = isCA; - } - - @JsonProperty("issuerRef") - public io.fabric8.certmanager.api.model.meta.v1.ObjectReference getIssuerRef() { - return issuerRef; - } - - @JsonProperty("issuerRef") - public void setIssuerRef(io.fabric8.certmanager.api.model.meta.v1.ObjectReference issuerRef) { - this.issuerRef = issuerRef; - } - - @JsonProperty("request") - public String getRequest() { - return request; - } - - @JsonProperty("request") - public void setRequest(String request) { - this.request = request; - } - - @JsonProperty("usages") - public List getUsages() { - return usages; - } - - @JsonProperty("usages") - public void setUsages(List usages) { - this.usages = usages; - } - - @JsonAnyGetter - public Map getAdditionalProperties() { - return this.additionalProperties; - } - - @JsonAnySetter - public void setAdditionalProperty(String name, Object value) { - this.additionalProperties.put(name, value); - } - -} - diff --git a/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8NameHelper.java b/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8NameHelper.java index a20b1cebf16..293059453f9 100644 --- a/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8NameHelper.java +++ b/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/main/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8NameHelper.java @@ -20,24 +20,38 @@ import org.jsonschema2pojo.GenerationConfig; import org.jsonschema2pojo.util.NameHelper; +import java.util.HashMap; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** + * Renames reserved fields name to predefined values (PROTECTED_WORD_MAP) + *

* Overrides default provided NameHelper to overcome getter/setter naming convention mismatch between Sundr.io * and jsonschema2pojo. - * + *

* There are issues with properties such as x-kubernetes-foo or xKubernetesFoo, sundrio expects a getter as * getXKubernetesFoo while jsonschema2pojo produces getxKubernetesFoo. */ public class Fabric8NameHelper extends NameHelper { + private static final Map PROTECTED_WORD_MAP = new HashMap<>(); + static { + PROTECTED_WORD_MAP.put("class", "className"); + } private static final Pattern SINGLE_LETTER_PREFIX_WORD_PROPERTY = Pattern.compile("^[a-z]((-[a-zA-Z])|[A-Z])(.*)$"); public Fabric8NameHelper(GenerationConfig generationConfig) { super(generationConfig); } + @Override + public String getFieldName(String propertyName, JsonNode node) { + final String fieldName = super.getFieldName(propertyName, node); + return PROTECTED_WORD_MAP.getOrDefault(fieldName, fieldName); + } + @Override public String getGetterName(String propertyName, JType type, JsonNode node) { return correctCamelCaseWithPrefix(propertyName, super.getGetterName(propertyName, type, node)); diff --git a/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/test/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8NameHelperTest.java b/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/test/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8NameHelperTest.java index 40f0adfa071..70e88c75fba 100644 --- a/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/test/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8NameHelperTest.java +++ b/kubernetes-model-generator/kubernetes-model-jsonschema2pojo/src/test/java/io/fabric8/kubernetes/jsonschema2pojo/Fabric8NameHelperTest.java @@ -15,13 +15,30 @@ */ package io.fabric8.kubernetes.jsonschema2pojo; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; import static io.fabric8.kubernetes.jsonschema2pojo.Fabric8NameHelper.correctCamelCaseWithPrefix; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.params.provider.Arguments.arguments; class Fabric8NameHelperTest { + private static ObjectMapper M; + + @BeforeAll + static void beforeAll() { + M = new ObjectMapper(); + } + @Test void correctCamelCaseWithPrefixWithSingleLetterDash() { assertEquals("getXField", correctCamelCaseWithPrefix("x-field", "getxField")); @@ -51,4 +68,23 @@ void correctCamelCaseWithPrefixWithCapitalFirstLetter() { assertEquals("getIpfix", correctCamelCaseWithPrefix("Ipfix", "getIpfix")); assertEquals("setIpfix", correctCamelCaseWithPrefix("Ipfix", "setIpfix")); } + + @DisplayName("getFieldName, should use javaName and never reserved words") + @ParameterizedTest(name = "{index}: getFieldName: property ''{0}'' node: ''{1}'', expects: ''{2}''") + @MethodSource("getFieldNameInput") + void getFieldName(String propertyName, ObjectNode node, String expectedName) { + // When + final String result = new Fabric8NameHelper(null).getFieldName(propertyName, node); + // Then + assertEquals(expectedName, result); + } + + static Stream getFieldNameInput() { + return Stream.of( + arguments("field", M.createObjectNode(), "field"), + arguments("class", M.createObjectNode(), "className"), + arguments("class", M.createObjectNode().put("javaName", "theClass"), "theClass"), + arguments("field", M.createObjectNode().put("javaName", "aField"), "aField"), + arguments("field", M.createObjectNode().put("javaName", "class"), "className")); + } }