Skip to content

Commit

Permalink
Merge pull request #3371 from semyon-zvyagin/bugfix-JSON-serialize-st…
Browse files Browse the repository at this point in the history
…atic-method-NPE

Fix bug: JSON initialization block should be static
  • Loading branch information
k8s-ci-robot committed May 1, 2024
2 parents 452fc32 + 61e05b6 commit 6e94a51
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri
return clazz;
}

{
static {
GsonBuilder gsonBuilder = createGson();
gsonBuilder.registerTypeAdapter(Date.class, dateTypeAdapter);
gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,26 @@
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import io.kubernetes.client.openapi.models.V1ListMeta;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.openapi.models.V1Status;
import okio.ByteString;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

@TestMethodOrder(OrderAnnotation.class)
class JSONTest {

private final JSON json = new JSON();
@Order(0)
@Test
void serializeWithStaticMethod() {
JSON.serialize(new V1ObjectMeta());
}

@Test
void serializeByteArray() {
JSON json = new JSON();
final String plainText = "string that contains '=' when encoded";
final String base64String = json.serialize(plainText.getBytes());
// serialize returns string surrounded by quotes: "\"[base64]\""
Expand All @@ -48,6 +58,7 @@ void serializeByteArray() {

@Test
void offsetDateTime1e6Parse() {
JSON json = new JSON();
String timeStr = "\"2018-04-03T11:32:26.123456Z\"";
OffsetDateTime dateTime = json.deserialize(timeStr, OffsetDateTime.class);
String serializedTsStr = json.serialize(dateTime);
Expand All @@ -56,6 +67,7 @@ void offsetDateTime1e6Parse() {

@Test
void offsetDateTime1e4Parse() {
JSON json = new JSON();
String timeStr = "\"2018-04-03T11:32:26.1234Z\"";
OffsetDateTime dateTime = json.deserialize(timeStr, OffsetDateTime.class);
String serializedTsStr = json.serialize(dateTime);
Expand All @@ -65,6 +77,7 @@ void offsetDateTime1e4Parse() {

@Test
void offsetDateTime1e3Parse() {
JSON json = new JSON();
String timeStr = "\"2018-04-03T11:32:26.123Z\"";
OffsetDateTime dateTime = json.deserialize(timeStr, OffsetDateTime.class);
String serializedTsStr = json.serialize(dateTime);
Expand All @@ -74,6 +87,7 @@ void offsetDateTime1e3Parse() {

@Test
void offsetDateTimeNoFractionParse() {
JSON json = new JSON();
String timeStr = "\"2018-04-03T11:32:26Z\"";
OffsetDateTime dateTime = json.deserialize(timeStr, OffsetDateTime.class);
String serializedTsStr = json.serialize(dateTime);
Expand Down

0 comments on commit 6e94a51

Please sign in to comment.