Skip to content

Commit

Permalink
Fix fabric8io#3240: MicroTime serialises incorrectly
Browse files Browse the repository at this point in the history
Add Custom MicroTime with custom Serializer/Deserializers in order to
convert to a timestamp string

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Jul 6, 2021
1 parent 189adf4 commit 4fb5b3e
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 79 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#### Bugs
* Fix #3083: CertificateException due to PEM being decoded in CertUtils
* Fix #3240: MicroTime serialises incorrectly; add custom serializer/deserializer for MicroTime

#### Improvements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.fabric8.kubernetes.api.model.Event;
import io.fabric8.kubernetes.api.model.EventBuilder;
import io.fabric8.kubernetes.api.model.EventList;
import io.fabric8.kubernetes.api.model.ObjectReference;
import io.fabric8.kubernetes.api.model.MicroTimeBuilder;
import io.fabric8.kubernetes.api.model.ObjectReferenceBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.arquillian.cube.kubernetes.api.Session;
Expand All @@ -28,6 +28,9 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -89,6 +92,9 @@ private Event createEvent(String name) {
.withNamespace(session.getNamespace())
.build())
.withReason("Custom Event")
.withEventTime(new MicroTimeBuilder().withTime("2021-07-06T16:38:47.986439Z").build())
.withReportingComponent("foo")
.withReportingInstance("foo")
.build();
}
}
3 changes: 3 additions & 0 deletions kubernetes-model-generator/kubernetes-model-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@
<delete
file="${generate.targetDirectory}/io/fabric8/kubernetes/api/model/WatchEvent.java"
verbose="true" />
<delete
file="${generate.targetDirectory}/io/fabric8/kubernetes/api/model/MicroTime.java"
verbose="true" />
<delete verbose="true">
<fileset dir="${generate.targetDirectory}">
<include name="*.java" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* 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.api.model;

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.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.sundr.builder.annotations.Buildable;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonDeserialize(using = MicroTime.Deserializer.class)
@JsonSerialize(using = MicroTime.Serializer.class)
@JsonPropertyOrder({
"apiVersion",
"kind",
"metadata",
"Time"
})
@ToString
@EqualsAndHashCode
@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = true, lazyCollectionInitEnabled = false, builderPackage = "io.fabric8.kubernetes.api.builder")
public class MicroTime implements KubernetesResource
{

@JsonProperty("Time")
private String time;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<>();

public MicroTime() { }

public MicroTime(String time) {
super();
this.time = time;
}

@JsonProperty("Time")
public String getTime() {
return time;
}

@JsonProperty("Time")
public void setTime(String time) {
this.time = time;
}

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

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

public static class Serializer extends JsonSerializer<MicroTime> {
@Override
public void serialize(MicroTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
if (value != null) {
if (value.getTime() != null) {
jgen.writeString(value.getTime());
}
} else {
jgen.writeNull();
}
}
}

public static class Deserializer extends JsonDeserializer<MicroTime> {
@Override
public MicroTime deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
MicroTime microTime = null;
if (node != null) {
microTime = new MicroTime(node.asText());
}
return microTime;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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.api.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.fabric8.kubernetes.model.util.Helper;
import org.junit.jupiter.api.Test;

import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER;
import static net.javacrumbs.jsonunit.core.Option.IGNORING_EXTRA_FIELDS;
import static net.javacrumbs.jsonunit.core.Option.TREATING_NULL_AS_ABSENT;
import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson;

class EventTest {
private final ObjectMapper mapper = new ObjectMapper();

@Test
void testEventSerializationDeserialization() throws JsonProcessingException {
// given
final String originalJson = Helper.loadJson("/valid-event.json");

// when
final Event event = mapper.readValue(originalJson, Event.class);
final String serializedJson = mapper.writeValueAsString(event);

// then
assertThatJson(serializedJson).when(IGNORING_ARRAY_ORDER, TREATING_NULL_AS_ABSENT, IGNORING_EXTRA_FIELDS)
.isEqualTo(originalJson);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"action": "test",
"apiVersion": "v1",
"eventTime": "2021-07-06T08:55:29.000000Z",
"involvedObject": {
"name": "spring-boot-55f686b994-6xn5b",
"namespace": "default"
},
"kind": "Event",
"metadata": {
"creationTimestamp": "2021-07-06T11:27:51Z",
"generateName": "fabric8-client-restart-event2",
"name": "fabric8-client-restart-event22mlmc",
"namespace": "default",
"resourceVersion": "80681",
"uid": "08342bf0-e80a-4759-a029-06876ab04307"
},
"reason": "TODO",
"reportingComponent": "foo",
"reportingInstance": "foo-123",
"source": {},
"type": "Normal"
}
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@
},
"apiVersion": {
"type": "string",
"default": "events.k8s.io/v1beta1",
"default": "events.k8s.io/v1",
"required": true
},
"deprecatedCount": {
Expand Down Expand Up @@ -1331,8 +1331,8 @@
"type": "string"
},
"series": {
"$ref": "#/definitions/kubernetes_events_v1beta1_EventSeries",
"existingJavaType": "io.fabric8.kubernetes.api.model.events.v1beta1.EventSeries"
"$ref": "#/definitions/kubernetes_events_v1_EventSeries",
"existingJavaType": "io.fabric8.kubernetes.api.model.events.v1.EventSeries"
},
"type": {
"type": "string"
Expand All @@ -1344,14 +1344,14 @@
"properties": {
"apiVersion": {
"type": "string",
"default": "events.k8s.io/v1beta1",
"default": "events.k8s.io/v1",
"required": true
},
"items": {
"type": "array",
"items": {
"$ref": "#/definitions/kubernetes_events_v1beta1_Event",
"existingJavaType": "io.fabric8.kubernetes.api.model.events.v1beta1.Event"
"$ref": "#/definitions/kubernetes_events_v1_Event",
"existingJavaType": "io.fabric8.kubernetes.api.model.events.v1.Event"
}
},
"kind": {
Expand Down

0 comments on commit 4fb5b3e

Please sign in to comment.