Skip to content

Commit 1dc7aae

Browse files
committed
refactor: Refactor RegistrationDeserializer's snake case handling
1 parent f7ae423 commit 1dc7aae

File tree

2 files changed

+29
-34
lines changed

2 files changed

+29
-34
lines changed

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/utils/jackson/RegistrationDeserializer.java

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@
1717
package de.codecentric.boot.admin.server.utils.jackson;
1818

1919
import java.io.IOException;
20-
import java.util.HashMap;
2120
import java.util.Iterator;
2221
import java.util.Map;
2322

2423
import com.fasterxml.jackson.core.JsonParser;
25-
import com.fasterxml.jackson.core.JsonToken;
2624
import com.fasterxml.jackson.databind.DeserializationContext;
2725
import com.fasterxml.jackson.databind.JsonNode;
2826
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
29-
import org.springframework.util.ObjectUtils;
3027

3128
import de.codecentric.boot.admin.server.domain.values.Registration;
3229

@@ -41,26 +38,18 @@ public RegistrationDeserializer() {
4138
@Override
4239
public Registration deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
4340
JsonNode node = p.readValueAsTree();
44-
Map<String, String> normalizedKvPair = getNormalizedKvPair(node);
4541
Registration.Builder builder = Registration.builder();
4642

47-
if (node.hasNonNull("name")) {
48-
builder.name(node.get("name").asText());
49-
}
43+
builder.name(firstNonNullAsText(node, "name"));
44+
5045
if (node.hasNonNull("url")) {
51-
String url = node.get("url").asText();
46+
String url = firstNonNullAsText(node, "url");
5247
builder.healthUrl(url.replaceFirst("/+$", "") + "/health").managementUrl(url);
5348
}
5449
else {
55-
if (!ObjectUtils.isEmpty(normalizedKvPair.get("healthurl"))) {
56-
builder.healthUrl(normalizedKvPair.get("healthurl"));
57-
}
58-
if (!ObjectUtils.isEmpty(normalizedKvPair.get("managementurl"))) {
59-
builder.managementUrl(normalizedKvPair.get("managementurl"));
60-
}
61-
if (!ObjectUtils.isEmpty(normalizedKvPair.get("serviceurl"))) {
62-
builder.serviceUrl(normalizedKvPair.get("serviceurl"));
63-
}
50+
builder.healthUrl(firstNonNullAsText(node, "healthUrl", "health_url"));
51+
builder.managementUrl(firstNonNullAsText(node, "managementUrl", "management_url"));
52+
builder.serviceUrl(firstNonNullAsText(node, "serviceUrl", "service_url"));
6453
}
6554

6655
if (node.has("metadata")) {
@@ -71,29 +60,18 @@ public Registration deserialize(JsonParser p, DeserializationContext ctxt) throw
7160
}
7261
}
7362

74-
if (node.hasNonNull("source")) {
75-
builder.source(node.get("source").asText());
76-
}
63+
builder.source(firstNonNullAsText(node, "source"));
7764

7865
return builder.build();
7966
}
8067

81-
private Map<String, String> getNormalizedKvPair(JsonNode jn) throws IOException {
82-
Map<String, String> normalizedKvPair = new HashMap<>();
83-
JsonParser jp = jn.traverse();
84-
while (!jp.isClosed()) {
85-
if (jp.nextToken() == JsonToken.FIELD_NAME) {
86-
String fieldName = jp.currentName();
87-
if (!ObjectUtils.isEmpty(fieldName)) {
88-
JsonToken jsonValueToken = jp.nextValue();
89-
if (jsonValueToken == JsonToken.VALUE_STRING) {
90-
normalizedKvPair.putIfAbsent(fieldName.replaceAll("[_-]", "").toLowerCase(),
91-
jp.getValueAsString());
92-
}
93-
}
68+
private String firstNonNullAsText(JsonNode node, String... fieldNames) {
69+
for (String fieldName : fieldNames) {
70+
if (node.hasNonNull(fieldName)) {
71+
return node.get(fieldName).asText();
9472
}
9573
}
96-
return normalizedKvPair;
74+
return null;
9775
}
9876

9977
}

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/server/utils/jackson/RegistrationDeserializerTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,21 @@ public void test_sanitize_metadata() throws JsonProcessingException {
123123
assertThat(json).contains("humptydumpty");
124124
}
125125

126+
@Test
127+
void test_snake_case() throws Exception {
128+
String json = new JSONObject().put("name", "test")
129+
.put("management_url", "http://test")
130+
.put("health_url", "http://health")
131+
.put("service_url", "http://service")
132+
.put("metadata", new JSONObject().put("labels", "foo,bar"))
133+
.toString();
134+
Registration value = objectMapper.readValue(json, Registration.class);
135+
assertThat(value.getName()).isEqualTo("test");
136+
assertThat(value.getManagementUrl()).isEqualTo("http://test");
137+
assertThat(value.getHealthUrl()).isEqualTo("http://health");
138+
assertThat(value.getServiceUrl()).isEqualTo("http://service");
139+
assertThat(value.getMetadata()).isEqualTo(singletonMap("labels", "foo,bar"));
140+
141+
}
142+
126143
}

0 commit comments

Comments
 (0)