Skip to content

Commit dd8a42d

Browse files
committed
Merge pull request #44507 from nosan
* gh-44507: Polish "Prevent stack overflow when writing Path" Prevent stack overflow when writing Path Closes gh-44507
2 parents 5616923 + 175c9d3 commit dd8a42d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

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

+5-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,9 @@ else if (value instanceof WritableJson writableJson) {
114115
throw new UncheckedIOException(ex);
115116
}
116117
}
118+
else if (value instanceof Path p) {
119+
writeString(p.toString());
120+
}
117121
else if (value instanceof Iterable<?> iterable) {
118122
writeArray(iterable::forEach);
119123
}

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

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

244+
@Test // gh-44502
245+
void writeJavaNioPathWhenSingleElementShouldBeSerializedAsString() {
246+
assertThat(doWrite((valueWriter) -> valueWriter.write(Path.of("a")))).isEqualTo(quoted("a"));
247+
}
248+
249+
@Test // gh-44502
250+
void writeJavaNioPathShouldBeSerializedAsString() {
251+
assertThat(doWrite((valueWriter) -> valueWriter.write(Path.of("a/b/c")))).isEqualTo(quoted("a\\/b\\/c"));
252+
}
253+
243254
private <V> String write(V value) {
244255
return doWrite((valueWriter) -> valueWriter.write(value));
245256
}

0 commit comments

Comments
 (0)