Skip to content

Commit a69991b

Browse files
nosanwilkinsona
authored andcommitted
Prevent stack overflow when writing Path
Prior to this commit, serializing `java.nio.file.Path` caused a StackOverflowError because `Path.iterator()` always returns itself as the first element of the iterator, which results in a StackOverflowError. This commit serializes `java.nio.file.Path` as JSON String. See gh-44507 Signed-off-by: Dmytro Nosan <[email protected]>
1 parent 5616923 commit a69991b

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

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

+6-1
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+
writeString(p.toString());
121+
}
117122
else if (value instanceof Iterable<?> iterable) {
118123
writeArray(iterable::forEach);
119124
}

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

+14-1
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,18 @@ void endWhenNotStartedThrowsException() {
240241
.isThrownBy(() -> valueWriter.end(Series.ARRAY)));
241242
}
242243

244+
// https://github.com/spring-projects/spring-boot/issues/44502
245+
@Test
246+
void writeJavaNioPathWhenSingleElementShouldBeSerializedAsString() {
247+
assertThat(doWrite((valueWriter) -> valueWriter.write(Path.of("overflow")))).isEqualTo(quoted("overflow"));
248+
}
249+
250+
@Test
251+
void writeJavaNioPathShouldShouldBeSerializedAsString() {
252+
assertThat(doWrite((valueWriter) -> valueWriter.write(Path.of("stack/overflow/error"))))
253+
.isEqualTo(quoted("stack\\/overflow\\/error"));
254+
}
255+
243256
private <V> String write(V value) {
244257
return doWrite((valueWriter) -> valueWriter.write(value));
245258
}

0 commit comments

Comments
 (0)