Skip to content

Commit 8701567

Browse files
committed
Prevent StackOverflowError when serializing Path using JsonValueWriter
Prior to this commit, serializing `java.nio.file.Path` caused a StackOverflowError because `Path.iterator()` always returns at least one element, which results in a StackOverflowError. This commit adds custom serialization logic for `java.nio.file.Path`. Signed-off-by: Dmytro Nosan <[email protected]>
1 parent fa621f2 commit 8701567

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/json/JsonValueWriter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.io.UncheckedIOException;
21+
import java.nio.file.Path;
2122
import java.util.ArrayDeque;
2223
import java.util.Arrays;
2324
import java.util.Deque;
@@ -114,6 +115,10 @@ else if (value instanceof WritableJson writableJson) {
114115
throw new UncheckedIOException(ex);
115116
}
116117
}
118+
// https://github.com/spring-projects/spring-boot/issues/44502
119+
else if (value instanceof Path p) {
120+
writeArray((consumer) -> p.forEach((element) -> consumer.accept(element.toString())));
121+
}
117122
else if (value instanceof Iterable<?> iterable) {
118123
writeArray(iterable::forEach);
119124
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/json/JsonValueWriterTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.json;
1818

19+
import java.nio.file.Path;
1920
import java.util.LinkedHashMap;
2021
import java.util.LinkedHashSet;
2122
import java.util.List;
@@ -240,6 +241,20 @@ void endWhenNotStartedThrowsException() {
240241
.isThrownBy(() -> valueWriter.end(Series.ARRAY)));
241242
}
242243

244+
// https://github.com/spring-projects/spring-boot/issues/44502
245+
@Test
246+
void writeJavaNioPathWhenSingleElementShouldNotCauseStackOverflow() {
247+
assertThat(doWrite((valueWriter) -> valueWriter.write(Path.of("foo")))).isEqualTo("""
248+
["foo"]""");
249+
}
250+
251+
// https://github.com/spring-projects/spring-boot/issues/44502
252+
@Test
253+
void writeJavaNioPathShouldNotCauseStackOverflow() {
254+
assertThat(doWrite((valueWriter) -> valueWriter.write(Path.of("stack/over/flow")))).isEqualTo("""
255+
["stack","over","flow"]""");
256+
}
257+
243258
private <V> String write(V value) {
244259
return doWrite((valueWriter) -> valueWriter.write(value));
245260
}

0 commit comments

Comments
 (0)