Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(temp) Support for JsonTypeInfo annotations in UnmatchedFieldTypeModule #5028

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Fix #4963: Openshift Client return 403 when use websocket
* Fix #4985: triggering the immediate cleanup of the okhttp idle task
* fix #5002: Jetty response completion accounts for header processing
* Fix #5009: addressing issue with serialization of wrapped polymophic types

#### Improvements
* Fix #4434: Update CronJobIT to use `batch/v1` CronJob instead
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* 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.client.utils;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.fabric8.kubernetes.api.model.KubernetesResource;
import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

class SerializationWrappedPolymorphicTest {

@Group("example.com")
@Version("v1alpha1")
static class TestCR extends CustomResource<TestCR.TestCRSpec, Void> implements Namespaced {

@JsonDeserialize(using = JsonDeserializer.None.class)
static class TestCRSpec implements KubernetesResource {

@JsonIgnore
private final Map<String, Object> additionalProperties = new HashMap<>();

private List<Child> children = new ArrayList<>();

public List<Child> getChildren() {
return children;
}

public void setChildren(List<Child> children) {
this.children = children;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}

@Override
protected TestCRSpec initSpec() {
return new TestCRSpec();
}
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({ //
@JsonSubTypes.Type(FooChild.class), //
@JsonSubTypes.Type(BarChild.class), //
})
@JsonDeserialize(using = JsonDeserializer.None.class)
interface Child extends KubernetesResource {
}

@JsonTypeName("foo")
@Data
static class FooChild implements Child {
private String name;
}

@JsonTypeName("bar")
@Data
static class BarChild implements Child {
private String file;
}

@Test
void testPolyRoundTrip() {
TestCR original = new TestCR();
FooChild foo = new FooChild();
foo.setName("alice");
BarChild bar = new BarChild();
bar.setFile("bob");
original.getSpec().setChildren(Arrays.asList(foo, bar));
String json = Serialization.asJson(original);
assertThat(json).isEqualTo(
"{\"apiVersion\":\"example.com/v1alpha1\",\"kind\":\"TestCR\",\"metadata\":{},\"spec\":{\"children\":[{\"foo\":{\"name\":\"alice\"}},{\"bar\":{\"file\":\"bob\"}}]}}");
TestCR deserialized = Serialization.unmarshal(json, TestCR.class);
assertEquals(deserialized.getSpec().getChildren(), original.getSpec().getChildren());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package io.fabric8.kubernetes.model.jackson;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -69,4 +71,19 @@ public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider
delegate.getName(), valueInAnyGetter, bean.getClass().getName());
}
}

@Override
public void assignNullSerializer(JsonSerializer<Object> nullSer) {
delegate.assignNullSerializer(nullSer);
}

@Override
public void assignSerializer(JsonSerializer<Object> ser) {
delegate.assignSerializer(ser);
}

@Override
public void assignTypeSerializer(TypeSerializer typeSer) {
delegate.assignTypeSerializer(typeSer);
}
}