Skip to content

Commit 13e3348

Browse files
authored
Merge pull request #2062 from wind57/fix-1641-part-1
fix-1641: serialization concerns (1)
2 parents 8729e1f + f6bcde7 commit 13e3348

File tree

5 files changed

+147
-6
lines changed

5 files changed

+147
-6
lines changed

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/DefaultKubernetesServiceInstance.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
package org.springframework.cloud.kubernetes.commons.discovery;
1818

19+
import java.io.Serializable;
1920
import java.net.URI;
2021
import java.util.Map;
2122

23+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
24+
2225
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTP;
2326
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTPS;
2427
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.NAMESPACE_METADATA_KEY;
@@ -34,9 +37,10 @@
3437
* @param namespace the namespace of the service.
3538
* @param cluster the cluster the service resides in.
3639
*/
40+
@JsonIgnoreProperties(ignoreUnknown = true)
3741
public record DefaultKubernetesServiceInstance(String instanceId, String serviceId, String host, int port,
3842
Map<String, String> metadata, boolean secure, String namespace, String cluster,
39-
Map<String, Map<String, String>> podMetadata) implements KubernetesServiceInstance {
43+
Map<String, Map<String, String>> podMetadata) implements KubernetesServiceInstance, Serializable {
4044

4145
@Override
4246
public String getInstanceId() {

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/KubernetesExternalNameServiceInstance.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.cloud.kubernetes.commons.discovery;
1818

19+
import java.io.Serializable;
1920
import java.net.URI;
2021
import java.util.Map;
2122

@@ -28,7 +29,7 @@
2829
* @author wind57
2930
*/
3031
public record KubernetesExternalNameServiceInstance(String serviceId, String host, String instanceId,
31-
Map<String, String> metadata) implements ServiceInstance {
32+
Map<String, String> metadata) implements ServiceInstance, Serializable {
3233

3334
@Override
3435
public String getServiceId() {

spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/configdata/KubernetesConfigDataLocationResolverTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,7 @@ void testResolveProfileSpecificSix() {
291291

292292
Profiles profiles = Mockito.mock(Profiles.class);
293293
ConfigDataLocation configDataLocation = ConfigDataLocation.of("kubernetes:abc");
294-
storePropertiesResolver.resolveProfileSpecific(RESOLVER_CONTEXT,
295-
configDataLocation, profiles);
294+
storePropertiesResolver.resolveProfileSpecific(RESOLVER_CONTEXT, configDataLocation, profiles);
296295

297296
// on the other hand, @Default will be picked here
298297
Assertions.assertThat(storePropertiesResolver.configMapConfigProperties.enabled()).isTrue();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright 2019-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.commons.discovery;
18+
19+
import java.util.Map;
20+
21+
import com.fasterxml.jackson.core.JsonProcessingException;
22+
import com.fasterxml.jackson.databind.ObjectMapper;
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.boot.test.json.BasicJsonTester;
26+
import org.springframework.cloud.client.ServiceInstance;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* @author wind57
32+
*/
33+
class ServiceInstanceSerializationTests {
34+
35+
private static final BasicJsonTester BASIC_JSON_TESTER = new BasicJsonTester(
36+
ServiceInstanceSerializationTests.class);
37+
38+
/**
39+
* <pre>
40+
* {
41+
* "instanceId": "instanceId",
42+
* "serviceId": "serviceId",
43+
* "host": "host",
44+
* "port": 8080,
45+
* "metadata": {
46+
* "k8s_namespace": "spring-k8s"
47+
* },
48+
* "secure": true,
49+
* "namespace": "namespace",
50+
* "cluster": "cluster",
51+
* "podMetadata": {
52+
* "pod_root": {
53+
* "pod_key": "pod_value"
54+
* }
55+
* },
56+
* "scheme": "https",
57+
* "uri": "https://host:8080"
58+
* }
59+
* </pre>
60+
*/
61+
@Test
62+
void defaultKubernetesServiceInstanceSerializationTest() throws JsonProcessingException {
63+
64+
DefaultKubernetesServiceInstance instance = new DefaultKubernetesServiceInstance("instanceId", "serviceId",
65+
"host", 8080, Map.of("k8s_namespace", "spring-k8s"), true, "namespace", "cluster",
66+
Map.of("pod_root", Map.of("pod_key", "pod_value")));
67+
68+
String serialized = new ObjectMapper().writeValueAsString(instance);
69+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.instanceId")
70+
.isEqualTo("instanceId");
71+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.serviceId")
72+
.isEqualTo("serviceId");
73+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.host").isEqualTo("host");
74+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathNumberValue("$.port").isEqualTo(8080);
75+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathMapValue("$.metadata")
76+
.containsExactlyInAnyOrderEntriesOf(Map.of("k8s_namespace", "spring-k8s"));
77+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathBooleanValue("$.secure").isTrue();
78+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.namespace")
79+
.isEqualTo("namespace");
80+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.cluster").isEqualTo("cluster");
81+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathMapValue("$.podMetadata")
82+
.containsExactlyInAnyOrderEntriesOf(Map.of("pod_root", Map.of("pod_key", "pod_value")));
83+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.scheme").isEqualTo("https");
84+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.uri")
85+
.isEqualTo("https://host:8080");
86+
}
87+
88+
@Test
89+
void defaultKubernetesServiceInstanceDeserializationTest() throws JsonProcessingException {
90+
91+
String serialized = """
92+
{
93+
"instanceId": "instanceId",
94+
"serviceId": "serviceId",
95+
"host": "host",
96+
"port": 8080,
97+
"metadata": {
98+
"k8s_namespace": "spring-k8s"
99+
},
100+
"secure": true,
101+
"namespace": "namespace",
102+
"cluster": "cluster",
103+
"podMetadata": {
104+
"pod_root": {
105+
"pod_key": "pod_value"
106+
}
107+
},
108+
"scheme": "https",
109+
"uri": "https://host:8080"
110+
}
111+
""";
112+
113+
ServiceInstance deserialized = new ObjectMapper().readValue(serialized, DefaultKubernetesServiceInstance.class);
114+
assertThat(deserialized.getInstanceId()).isEqualTo("instanceId");
115+
assertThat(deserialized.getServiceId()).isEqualTo("serviceId");
116+
assertThat(deserialized.getScheme()).isEqualTo("https");
117+
assertThat(deserialized.getHost()).isEqualTo("host");
118+
assertThat(deserialized.getPort()).isEqualTo(8080);
119+
assertThat(deserialized.getUri().toASCIIString()).isEqualTo("https://host:8080");
120+
assertThat(deserialized.getMetadata())
121+
.containsExactlyInAnyOrderEntriesOf(Map.of("k8s_namespace", "spring-k8s"));
122+
}
123+
124+
@Test
125+
void externalNameServiceInstanceSerializationTest() throws JsonProcessingException {
126+
KubernetesExternalNameServiceInstance instance = new KubernetesExternalNameServiceInstance("serviceId", "host",
127+
"instanceId", Map.of("a", "b"));
128+
String serialized = new ObjectMapper().writeValueAsString(instance);
129+
130+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.serviceId")
131+
.isEqualTo("serviceId");
132+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.host").isEqualTo("host");
133+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.instanceId")
134+
.isEqualTo("instanceId");
135+
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathMapValue("$.metadata")
136+
.containsExactlyInAnyOrderEntriesOf(Map.of("a", "b"));
137+
}
138+
139+
}

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-discovery/src/test/java/org/springframework/cloud/kubernetes/k8s/client/discovery/TestAssertions.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ static void assertBlockingConfiguration(CapturedOutput output, int port) {
8484

8585
assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathStringValue(BLOCKING_STATUS).isEqualTo("UP");
8686

87-
assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathStringValue(BLOCKING_STATUS).isEqualTo("UP");
88-
8987
assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathArrayValue(BLOCKING_SERVICES)
9088
.containsExactlyInAnyOrder("kubernetes", "service-wiremock");
9189

0 commit comments

Comments
 (0)