Skip to content

Commit 3aa77f4

Browse files
committed
fix(core): MapNode#getString should check if the node is StringRepresentable
1 parent e397b88 commit 3aa77f4

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

core/src/main/java/com/github/siroshun09/configapi/core/node/MapNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public sealed interface MapNode extends CommentableNode<Map<Object, Node<?>>> pe
294294
* @return the {@link String} to which the specified key is mapped, or the specified value if the key is not mapped to {@link StringValue}
295295
*/
296296
default @NotNull String getString(@NotNull Object key, @NotNull String def) {
297-
return this.raw(key) instanceof StringValue value ? value.asString() : def;
297+
return this.raw(key) instanceof StringRepresentable value ? value.asString() : def;
298298
}
299299

300300
/**
@@ -304,7 +304,7 @@ public sealed interface MapNode extends CommentableNode<Map<Object, Node<?>>> pe
304304
* @return the {@link String} to which the specified key is mapped, or {@code null} if the key is not mapped to {@link StringValue}
305305
*/
306306
default @Nullable String getStringOrNull(@NotNull Object key) {
307-
return this.raw(key) instanceof StringValue value ? value.asString() : null;
307+
return this.raw(key) instanceof StringRepresentable value ? value.asString() : null;
308308
}
309309

310310
/**

core/src/test/java/com/github/siroshun09/configapi/core/node/MapNodeTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ protected Stream<NodeTestCase<MapNode>> nodeTestCases() {
184184
assertSame(COMMENT, initial.getComment());
185185
assertSame(COMMENT, view.getComment());
186186
}
187+
),
188+
nodeTest(
189+
"MapNode#getString(String)",
190+
MapNode.create(Map.of("a", "b", 1, 2)),
191+
node -> {
192+
assertEquals("b", node.getString("a"));
193+
assertEquals("2", node.getString(1));
194+
assertEquals("", node.getString("unknown"));
195+
assertEquals("default", node.getString("unknown", "default"));
196+
}
187197
)
188198
);
189199
}

0 commit comments

Comments
 (0)