Skip to content

Commit b7234c3

Browse files
committed
fixes gson-syntax-exception upon deletion even if the deletion succeeds
1 parent 49cf295 commit b7234c3

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.kubernetes.client.e2e.basic
15+
16+
import io.kubernetes.client.openapi.Configuration
17+
import io.kubernetes.client.openapi.apis.CoreV1Api
18+
import io.kubernetes.client.openapi.models.V1Namespace
19+
import io.kubernetes.client.openapi.models.V1ObjectMeta
20+
import io.kubernetes.client.openapi.models.V1Status
21+
import io.kubernetes.client.util.ClientBuilder
22+
import spock.lang.Specification
23+
24+
class CoreV1ApiTest extends Specification {
25+
def "Create Namespace then Delete should work"() {
26+
given:
27+
def apiClient = ClientBuilder.defaultClient()
28+
def corev1api = new CoreV1Api(apiClient)
29+
Configuration.setDefaultApiClient(apiClient)
30+
def namespaceFoo = new V1Namespace().metadata(new V1ObjectMeta().name("foo"))
31+
when:
32+
V1Namespace created = corev1api.createNamespace(namespaceFoo, null, null, null)
33+
then:
34+
created != null
35+
when:
36+
V1Status deleted = corev1api.deleteNamespace("foo", null, null, null, null, null, null)
37+
then:
38+
deleted != null
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.gson;
14+
15+
import com.google.gson.Gson;
16+
import com.google.gson.JsonElement;
17+
import com.google.gson.JsonObject;
18+
import io.gsonfire.PreProcessor;
19+
import io.kubernetes.client.openapi.models.V1Status;
20+
21+
/** Suppresses runtime exceptions due to https://github.com/kubernetes-client/java/issues/86. */
22+
public class V1StatusPreProcessor implements PreProcessor<V1Status> {
23+
24+
@Override
25+
public void preDeserialize(Class<? extends V1Status> aClass, JsonElement jsonElement, Gson gson) {
26+
if (isLegitKubernetesStatus(jsonElement)) {
27+
return;
28+
}
29+
30+
JsonObject obj = jsonElement.getAsJsonObject();
31+
JsonElement statusField = obj.get("status");
32+
if (statusField != null && statusField.isJsonObject()) {
33+
obj.addProperty("status", (String) null);
34+
}
35+
}
36+
37+
private static boolean isLegitKubernetesStatus(JsonElement jsonElement) {
38+
String apiVersion =
39+
jsonElement.getAsJsonObject().get("apiVersion") != null
40+
? jsonElement.getAsJsonObject().get("apiVersion").getAsString()
41+
: null;
42+
String kind =
43+
jsonElement.getAsJsonObject().get("kind") != null
44+
? jsonElement.getAsJsonObject().get("kind").getAsString()
45+
: null;
46+
return "v1".equals(apiVersion) && "Status".equals(kind);
47+
}
48+
}

kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.google.gson.stream.JsonReader;
2121
import com.google.gson.stream.JsonWriter;
2222
import io.gsonfire.GsonFireBuilder;
23+
import io.kubernetes.client.gson.V1StatusPreProcessor;
24+
import io.kubernetes.client.openapi.models.V1Status;
2325
import java.io.IOException;
2426
import java.io.StringReader;
2527
import java.lang.reflect.Type;
@@ -46,7 +48,10 @@ public class JSON {
4648

4749
public static GsonBuilder createGson() {
4850
GsonFireBuilder fireBuilder = new GsonFireBuilder();
49-
GsonBuilder builder = fireBuilder.createGsonBuilder();
51+
GsonBuilder builder =
52+
fireBuilder
53+
.registerPreProcessor(V1Status.class, new V1StatusPreProcessor())
54+
.createGsonBuilder();
5055
return builder;
5156
}
5257

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.gson;
14+
15+
import static org.junit.Assert.assertNotNull;
16+
17+
import com.google.gson.Gson;
18+
import io.gsonfire.GsonFireBuilder;
19+
import io.kubernetes.client.openapi.models.V1Status;
20+
import org.junit.Test;
21+
22+
public class V1StatusJsonDeserializerTest {
23+
24+
private final Gson gson =
25+
new GsonFireBuilder()
26+
.registerPreProcessor(V1Status.class, new V1StatusPreProcessor())
27+
.createGsonBuilder()
28+
.create();
29+
30+
private static final String JSON_DEPLOYMENT =
31+
"{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"spec\":null,\"status\":{}}";
32+
private static final String JSON_STATUS =
33+
"{\"apiVersion\":\"v1\",\"kind\":\"Status\",\"status\":\"True\"}";
34+
private static final String JSON_STATUS_NULL =
35+
"{\"apiVersion\":\"v1\",\"kind\":\"Status\",\"status\":null}";
36+
37+
@Test
38+
public void testDeserializeNormalStatusIntoStatus() {
39+
V1Status status = gson.fromJson(JSON_STATUS, V1Status.class);
40+
assertNotNull(status);
41+
}
42+
43+
@Test
44+
public void testDeserializeNullStatusIntoStatus() {
45+
V1Status status = gson.fromJson(JSON_STATUS_NULL, V1Status.class);
46+
assertNotNull(status);
47+
}
48+
49+
@Test
50+
public void testDeserializeDeploymentIntoStatus() {
51+
V1Status status = gson.fromJson(JSON_DEPLOYMENT, V1Status.class);
52+
assertNotNull(status);
53+
}
54+
}

0 commit comments

Comments
 (0)