From 5d92bcf60aa5b835fd17e6717d0c11c8239b8aa2 Mon Sep 17 00:00:00 2001 From: Steve Hawkins Date: Wed, 14 Dec 2022 14:30:56 -0500 Subject: [PATCH] fix #3972: preparing for removal of unneeded serialization logic. --- CHANGELOG.md | 2 + .../client/dsl/ParameterMixedOperation.java | 5 +- ...ServerGetDeleteRecreateWaitApplicable.java | 6 ++ .../client/dsl/Parameterizable.java | 5 ++ .../client/utils/Serialization.java | 61 +++++++++++-------- ...ializationSingleDocumentUnmarshalTest.java | 5 +- 6 files changed, 52 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cd908d5bc1..990f1c2c262 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ #### New Features #### _**Note**_: Breaking changes +* Fix #3972: deprecated Parameterizable and methods on Serialization accepting parameters - that was only needed as a workaround for non-string parameters. You should instead include those parameter values in the map passed to processLocally. +* Fix #3972: WARNING: future client versions will not implicitly process templates as part of the load operation nor will the static yaml and json ObjectMappers be provided via Serialization. ### 6.3.1-SNAPSHOT diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/ParameterMixedOperation.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/ParameterMixedOperation.java index 3213b4c47b0..c46197a6287 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/ParameterMixedOperation.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/ParameterMixedOperation.java @@ -17,12 +17,15 @@ package io.fabric8.kubernetes.client.dsl; /** - * A {@link Parameterizable} {@link MixedOperation} + * It is no longer necessary to associate parameters prior to deserialization. + *

+ * reference {@link MixedOperation} instead * * @param The Kubernetes resource type. * @param The list variant of the Kubernetes resource type. * @param The resource operations. */ +@Deprecated public interface ParameterMixedOperation> extends MixedOperation, Parameterizable> { } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable.java index f8c39a58fbc..fac443b891b 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable.java @@ -16,6 +16,12 @@ package io.fabric8.kubernetes.client.dsl; +/** + * It is no longer necessary to associate parameters prior to deserialization. + *

+ * reference {@link NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable} instead + */ +@Deprecated public interface ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable extends NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable, Parameterizable> { diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Parameterizable.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Parameterizable.java index 3dfdd74d083..8e4acfb694c 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Parameterizable.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Parameterizable.java @@ -17,6 +17,11 @@ import java.util.Map; +/** + * It is no longer necessary to associate parameters prior to deserialization. Please + * provide the parameters to one of the process methods instead + */ +@Deprecated public interface Parameterizable { T withParameters(Map parameters); diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/Serialization.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/Serialization.java index a5ffeb936b5..d747ee3f099 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/Serialization.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/utils/Serialization.java @@ -35,7 +35,6 @@ import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Type; @@ -180,17 +179,13 @@ public static T unmarshal(InputStream is) { * @param parameters A {@link Map} with parameters for placeholder substitution. * @param The target type. * @return returns returns de-serialized object + * + * @deprecated please directly apply {@link Utils#interpolateString(String, Map)} instead of passing parameters here */ + @Deprecated @SuppressWarnings("unchecked") public static T unmarshal(InputStream is, Map parameters) { - String specFile = readSpecFileFromInputStream(is); - if (containsMultipleDocuments(specFile)) { - return (T) getKubernetesResourceList(parameters, specFile); - } else if (specFile.contains(DOCUMENT_DELIMITER)) { - specFile = specFile.replaceAll("^---([ \\t].*?)?\\r?\\n", ""); - specFile = specFile.replaceAll("\\n---([ \\t].*?)?\\r?\\n?$", "\n"); - } - return unmarshal(new ByteArrayInputStream(specFile.getBytes()), JSON_MAPPER, parameters); + return unmarshal(is, JSON_MAPPER, parameters); } /** @@ -217,9 +212,27 @@ public static T unmarshal(InputStream is, ObjectMapper mapper) { * @param parameters A {@link Map} with parameters for placeholder substitution. * @param The target type. * @return returns de-serialized object + * + * @deprecated please directly apply {@link Utils#interpolateString(String, Map)} instead of passing parameters here */ + @Deprecated public static T unmarshal(InputStream is, ObjectMapper mapper, Map parameters) { - return unmarshal(is, mapper, new TypeReference() { + // it's not well documented which Serialization methods are aware of input that can contain + // multiple docs + String specFile; + try { + specFile = IOHelpers.readFully(is); + } catch (IOException e1) { + throw new RuntimeException("Could not read stream"); + } + if (containsMultipleDocuments(specFile)) { + return (T) getKubernetesResourceList(Collections.emptyMap(), specFile); + } else if (specFile.contains(DOCUMENT_DELIMITER)) { + specFile = specFile.replaceAll("^---([ \\t].*?)?\\r?\\n", ""); + specFile = specFile.replaceAll("\\n---([ \\t].*?)?\\r?\\n?$", "\n"); + } + + return unmarshal(new ByteArrayInputStream(specFile.getBytes(StandardCharsets.UTF_8)), mapper, new TypeReference() { @Override public Type getType() { return KubernetesResource.class; @@ -228,9 +241,8 @@ public Type getType() { } private static T unmarshal(InputStream is, ObjectMapper mapper, TypeReference type, Map parameters) { - try ( - InputStream wrapped = parameters != null && !parameters.isEmpty() ? ReplaceValueStream.replaceValues(is, parameters) - : is; + try (InputStream wrapped = parameters != null && !parameters.isEmpty() ? ReplaceValueStream.replaceValues(is, parameters) + : is; BufferedInputStream bis = new BufferedInputStream(wrapped)) { bis.mark(-1); int intch; @@ -296,7 +308,10 @@ public static T unmarshal(String str, final Class type) { * @param parameters A hashmap containing parameters * * @return returns de-serialized object + * + * @deprecated please directly apply {@link Utils#interpolateString(String, Map)} instead of passing parameters here */ + @Deprecated public static T unmarshal(String str, final Class type, Map parameters) { try (InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8))) { return unmarshal(is, new TypeReference() { @@ -330,7 +345,10 @@ public static T unmarshal(InputStream is, final Class type) { * @param parameters A {@link Map} with parameters for placeholder substitution. * @param Template argument denoting type * @return returns de-serialized object + * + * @deprecated please directly apply {@link Utils#interpolateString(String, Map)} instead of passing parameters here */ + @Deprecated public static T unmarshal(InputStream is, final Class type, Map parameters) { return unmarshal(is, new TypeReference() { @Override @@ -361,7 +379,10 @@ public static T unmarshal(InputStream is, TypeReference type) { * @param Template argument denoting type * * @return returns de-serialized object + * + * @deprecated please directly apply {@link Utils#interpolateString(String, Map)} instead of passing parameters here */ + @Deprecated public static T unmarshal(InputStream is, TypeReference type, Map parameters) { return unmarshal(is, JSON_MAPPER, type, parameters); } @@ -401,20 +422,6 @@ private static boolean validate(String document) { return !document.isEmpty() && keyValueMatcher.find(); } - private static String readSpecFileFromInputStream(InputStream inputStream) { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int length; - try { - while ((length = inputStream.read(buffer)) != -1) { - outputStream.write(buffer, 0, length); - } - return outputStream.toString(); - } catch (IOException e) { - throw new RuntimeException("Unable to read InputStream." + e); - } - } - /** * Create a copy of the resource via serialization. * diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/utils/SerializationSingleDocumentUnmarshalTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/utils/SerializationSingleDocumentUnmarshalTest.java index d8bf77536ef..936a85fa64b 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/utils/SerializationSingleDocumentUnmarshalTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/utils/SerializationSingleDocumentUnmarshalTest.java @@ -21,8 +21,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import java.util.Collections; - import static org.assertj.core.api.Assertions.assertThat; class SerializationSingleDocumentUnmarshalTest { @@ -36,8 +34,7 @@ class SerializationSingleDocumentUnmarshalTest { void unmarshalWithSingleDocumentWithDocumentDelimiterShouldReturnKubernetesResource(String arg) { // When final KubernetesResource result = Serialization.unmarshal( - SerializationTest.class.getResourceAsStream(String.format("/serialization/%s", arg)), - Collections.emptyMap()); + SerializationTest.class.getResourceAsStream(String.format("/serialization/%s", arg))); // Then assertThat(result) .asInstanceOf(InstanceOfAssertFactories.type(Service.class))