Skip to content

Commit

Permalink
fix fabric8io#3972: removing the need for parameter processing in Ser…
Browse files Browse the repository at this point in the history
…ialization
  • Loading branch information
shawkins committed Dec 14, 2022
1 parent e0ccbb0 commit 0f7da0a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -55,7 +55,11 @@
* Fix #4515: files located at the root of jars named model.properties, e.g. core.properties, have been removed
* Fix #4579: the implicit registration of resource and list types that happens when using the resource(class) methods has been removed. This makes the behavior of the client more predictable as that was an undocumented side-effect. If you expect to see instances of a custom type from an untyped api call - typically KubernetesClient.load, KubernetesClient.resourceList, KubernetesClient.resource(InputStream|String), then you must either create a META-INF/services/io.fabric8.kubernetes.api.model.KubernetesResource file (see above #3923), or make calls to KubernetesDeserializer.registerCustomKind - however since KubernetesDeserializer is an internal class that mechanism is not preferred.
* Fix #4597: remove the deprecated support for `javax.validation.constraints.NotNull` in the `crd-generator`, to mark a property as `required` it needs to be annotated with `io.fabric8.generator.annotation.Required`
<<<<<<< HEAD
* Fix #4363: deprecated existing ResourceCompare methods such as compareKubernetesResource as they are not for general use
=======
* Fix #3973: removed support for Parameterizable - that was only needed as a workaround for non-string parameters. You should instead include those parameter values in the map passed to processLocally.
>>>>>>> b8f6fb444 (Fix #3972: removing the need for parameter processing in Serialization)
### 6.2.0 (2022-10-20)

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

/**
* A {@link Parameterizable} {@link MixedOperation}
* 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.
* @param <T>
*/
@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,15 @@

package io.fabric8.kubernetes.client.dsl;

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

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
*
* @param <T>
*/
@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 0f7da0a

Please sign in to comment.