forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix fabric8io#3240: MicroTime serialises incorrectly
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
1 parent
189adf4
commit 4fb5b3e
Showing
8 changed files
with
199 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 0 additions & 72 deletions
72
...r/kubernetes-model-core/src/generated/java/io/fabric8/kubernetes/api/model/MicroTime.java
This file was deleted.
Oops, something went wrong.
115 changes: 115 additions & 0 deletions
115
...erator/kubernetes-model-core/src/main/java/io/fabric8/kubernetes/api/model/MicroTime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...erator/kubernetes-model-core/src/test/java/io/fabric8/kubernetes/api/model/EventTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
kubernetes-model-generator/kubernetes-model-core/src/test/resources/valid-event.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters