From d2968dbad327cddc411160648ad466ff2d15ade5 Mon Sep 17 00:00:00 2001 From: Siroshun09 Date: Tue, 20 Feb 2024 19:56:16 +0900 Subject: [PATCH] fix: deserializing commented node in RecordDeserializer --- .../record/RecordDeserializer.java | 5 ++++- .../record/RecordDeserializerTest.java | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/github/siroshun09/configapi/core/serialization/record/RecordDeserializer.java b/core/src/main/java/com/github/siroshun09/configapi/core/serialization/record/RecordDeserializer.java index 12c0e159..03b40487 100644 --- a/core/src/main/java/com/github/siroshun09/configapi/core/serialization/record/RecordDeserializer.java +++ b/core/src/main/java/com/github/siroshun09/configapi/core/serialization/record/RecordDeserializer.java @@ -19,6 +19,7 @@ import com.github.siroshun09.configapi.core.node.BooleanArray; import com.github.siroshun09.configapi.core.node.BooleanValue; import com.github.siroshun09.configapi.core.node.ByteArray; +import com.github.siroshun09.configapi.core.node.CommentedNode; import com.github.siroshun09.configapi.core.node.DoubleArray; import com.github.siroshun09.configapi.core.node.EnumValue; import com.github.siroshun09.configapi.core.node.FloatArray; @@ -293,7 +294,9 @@ private Object processInlinedRecord(@NotNull RecordComponent parent, @NotNull Cl @SuppressWarnings("unchecked") private @Nullable Object deserializeNode(@NotNull Node node, @NotNull Class clazz, @Nullable Object defaultObject) { - if (clazz == boolean.class || clazz == Boolean.class) { + if (node instanceof CommentedNode commentedNode) { + return this.deserializeNode(commentedNode.node(), clazz, defaultObject); + } else if (clazz == boolean.class || clazz == Boolean.class) { return node instanceof BooleanValue booleanValue ? booleanValue.value() : defaultObject; } else if (clazz == String.class) { return node instanceof StringValue stringValue ? stringValue.asString() : defaultObject; diff --git a/core/src/test/java/com/github/siroshun09/configapi/core/serialization/record/RecordDeserializerTest.java b/core/src/test/java/com/github/siroshun09/configapi/core/serialization/record/RecordDeserializerTest.java index 531bc1f7..7f932882 100644 --- a/core/src/test/java/com/github/siroshun09/configapi/core/serialization/record/RecordDeserializerTest.java +++ b/core/src/test/java/com/github/siroshun09/configapi/core/serialization/record/RecordDeserializerTest.java @@ -16,7 +16,10 @@ package com.github.siroshun09.configapi.core.serialization.record; +import com.github.siroshun09.configapi.core.comment.SimpleComment; +import com.github.siroshun09.configapi.core.node.CommentableNode; import com.github.siroshun09.configapi.core.node.MapNode; +import com.github.siroshun09.configapi.core.node.StringValue; import com.github.siroshun09.configapi.core.serialization.SerializationException; import com.github.siroshun09.configapi.core.serialization.key.KeyGenerator; import com.github.siroshun09.configapi.test.shared.data.Samples; @@ -58,4 +61,18 @@ void testCustomObjectAndCustomKeyGenerator() { var deserializer = RecordDeserializer.builder(Samples.UUIDRecord.class).addDeserializer(UUID.class, node -> UUID.fromString(node.value().toString())).keyGenerator(KeyGenerator.CAMEL_TO_KEBAB).build(); Assertions.assertEquals(Samples.uuidRecord(), deserializer.deserialize(Samples.uuidRecordMapNode())); } + + @Test + void testDeserializingCommentedNode() { + var deserializer = RecordDeserializer.create(SimpleRecord.class); + + var mapNode = MapNode.create(); + mapNode.set("value", CommentableNode.withComment(StringValue.fromString("value"), SimpleComment.create("comment"))); + + var actual = deserializer.deserialize(mapNode); + Assertions.assertEquals("value", actual.value()); + } + + private record SimpleRecord(String value) { + } }