mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-17 12:09:16 +00:00
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 <dimanosan@gmail.com>
This commit is contained in:
committed by
Andy Wilkinson
parent
5616923569
commit
a69991b261
+6
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2024 the original author or authors.
|
* Copyright 2012-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,6 +18,7 @@ package org.springframework.boot.json;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Deque;
|
import java.util.Deque;
|
||||||
@@ -114,6 +115,10 @@ class JsonValueWriter {
|
|||||||
throw new UncheckedIOException(ex);
|
throw new UncheckedIOException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// https://github.com/spring-projects/spring-boot/issues/44502
|
||||||
|
else if (value instanceof Path p) {
|
||||||
|
writeString(p.toString());
|
||||||
|
}
|
||||||
else if (value instanceof Iterable<?> iterable) {
|
else if (value instanceof Iterable<?> iterable) {
|
||||||
writeArray(iterable::forEach);
|
writeArray(iterable::forEach);
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2024 the original author or authors.
|
* Copyright 2012-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.springframework.boot.json;
|
package org.springframework.boot.json;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -240,6 +241,18 @@ class JsonValueWriterTests {
|
|||||||
.isThrownBy(() -> valueWriter.end(Series.ARRAY)));
|
.isThrownBy(() -> valueWriter.end(Series.ARRAY)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://github.com/spring-projects/spring-boot/issues/44502
|
||||||
|
@Test
|
||||||
|
void writeJavaNioPathWhenSingleElementShouldBeSerializedAsString() {
|
||||||
|
assertThat(doWrite((valueWriter) -> valueWriter.write(Path.of("overflow")))).isEqualTo(quoted("overflow"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void writeJavaNioPathShouldShouldBeSerializedAsString() {
|
||||||
|
assertThat(doWrite((valueWriter) -> valueWriter.write(Path.of("stack/overflow/error"))))
|
||||||
|
.isEqualTo(quoted("stack\\/overflow\\/error"));
|
||||||
|
}
|
||||||
|
|
||||||
private <V> String write(V value) {
|
private <V> String write(V value) {
|
||||||
return doWrite((valueWriter) -> valueWriter.write(value));
|
return doWrite((valueWriter) -> valueWriter.write(value));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user