Skip to content

Commit

Permalink
fix fabric8io#3972: preparing for removal of unneeded serialization l…
Browse files Browse the repository at this point in the history
…ogic.
  • Loading branch information
shawkins committed Dec 14, 2022
1 parent e0ccbb0 commit 7dfba49
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 32 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
Expand Up @@ -17,12 +17,15 @@
package io.fabric8.kubernetes.client.dsl;

/**
* A {@link Parameterizable} {@link MixedOperation}
* @deprecated It is no longer necessary to associate parameters prior to deserialization.
* <p>
* reference {@link MixedOperation} instead
*
* @param <T> The Kubernetes resource type.
* @param <L> The list variant of the Kubernetes resource type.
* @param <R> The resource operations.
*/
@Deprecated
public interface ParameterMixedOperation<T, L, R extends Resource<T>>
extends MixedOperation<T, L, R>, Parameterizable<MixedOperation<T, L, R>> {
}
Expand Up @@ -16,6 +16,12 @@

package io.fabric8.kubernetes.client.dsl;

/**
* @deprecated It is no longer necessary to associate parameters prior to deserialization.
* <p>
* reference {@link NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable} instead
*/
@Deprecated
public interface ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<T>
extends NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<T>,
Parameterizable<NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<T>> {
Expand Down
Expand Up @@ -17,6 +17,11 @@

import java.util.Map;

/**
* @deprecated 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> {

T withParameters(Map<String, String> parameters);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -180,17 +179,13 @@ public static <T> T unmarshal(InputStream is) {
* @param parameters A {@link Map} with parameters for placeholder substitution.
* @param <T> 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> T unmarshal(InputStream is, Map<String, String> 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);
}

/**
Expand All @@ -217,9 +212,27 @@ public static <T> T unmarshal(InputStream is, ObjectMapper mapper) {
* @param parameters A {@link Map} with parameters for placeholder substitution.
* @param <T> 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> T unmarshal(InputStream is, ObjectMapper mapper, Map<String, String> parameters) {
return unmarshal(is, mapper, new TypeReference<T>() {
// 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<T>() {
@Override
public Type getType() {
return KubernetesResource.class;
Expand All @@ -228,9 +241,8 @@ public Type getType() {
}

private static <T> T unmarshal(InputStream is, ObjectMapper mapper, TypeReference<T> type, Map<String, String> 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;
Expand Down Expand Up @@ -296,7 +308,10 @@ public static <T> T unmarshal(String str, final Class<T> 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> T unmarshal(String str, final Class<T> type, Map<String, String> parameters) {
try (InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8))) {
return unmarshal(is, new TypeReference<T>() {
Expand Down Expand Up @@ -330,7 +345,10 @@ public static <T> T unmarshal(InputStream is, final Class<T> type) {
* @param parameters A {@link Map} with parameters for placeholder substitution.
* @param <T> 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> T unmarshal(InputStream is, final Class<T> type, Map<String, String> parameters) {
return unmarshal(is, new TypeReference<T>() {
@Override
Expand Down Expand Up @@ -361,7 +379,10 @@ public static <T> T unmarshal(InputStream is, TypeReference<T> type) {
* @param <T> 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> T unmarshal(InputStream is, TypeReference<T> type, Map<String, String> parameters) {
return unmarshal(is, JSON_MAPPER, type, parameters);
}
Expand Down Expand Up @@ -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.
*
Expand Down
Expand Up @@ -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 {
Expand All @@ -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))
Expand Down

0 comments on commit 7dfba49

Please sign in to comment.