Skip to content

Commit 6849356

Browse files
committed
Update tests for CI
Signed-off-by: Dmytro Nosan <[email protected]>
1 parent ea08a97 commit 6849356

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@
4646
*/
4747
class JsonValueWriter {
4848

49-
private static final int MAX_NESTING_DEPTH = 1000;
49+
private static final int DEFAULT_MAX_NESTING_DEPTH = 1000;
5050

5151
private final Appendable out;
5252

53+
private final int maxNestingDepth;
54+
5355
private MemberPath path = MemberPath.ROOT;
5456

5557
private final Deque<JsonWriterFiltersAndProcessors> filtersAndProcessors = new ArrayDeque<>();
@@ -61,7 +63,18 @@ class JsonValueWriter {
6163
* @param out the {@link Appendable} used to receive the JSON output
6264
*/
6365
JsonValueWriter(Appendable out) {
66+
this(out, DEFAULT_MAX_NESTING_DEPTH);
67+
}
68+
69+
/**
70+
* Create a new {@link JsonValueWriter} instance.
71+
* @param out the {@link Appendable} used to receive the JSON output
72+
* @param maxNestingDepth the maximum allowed nesting depth for JSON objects and
73+
* arrays
74+
*/
75+
JsonValueWriter(Appendable out, int maxNestingDepth) {
6476
this.out = out;
77+
this.maxNestingDepth = maxNestingDepth;
6578
}
6679

6780
void pushProcessors(JsonWriterFiltersAndProcessors jsonProcessors) {
@@ -271,9 +284,9 @@ private void writeString(Object value) {
271284
}
272285

273286
private void validateNestingDepth() {
274-
if (this.activeSeries.size() > MAX_NESTING_DEPTH) {
287+
if (this.activeSeries.size() > this.maxNestingDepth) {
275288
throw new IllegalStateException("JSON nesting depth (%s) exceeds maximum depth of %s (current path: %s)"
276-
.formatted(this.activeSeries.size(), MAX_NESTING_DEPTH, this.path));
289+
.formatted(this.activeSeries.size(), this.maxNestingDepth, this.path));
277290
}
278291
}
279292

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -243,30 +243,33 @@ void endWhenNotStartedThrowsException() {
243243

244244
@Test
245245
void illegalStateExceptionShouldBeThrownWhenCollectionExceededNestingDepth() {
246+
JsonValueWriter writer = new JsonValueWriter(new StringBuilder(), 128);
246247
List<Object> list = new ArrayList<>();
247248
list.add(list);
248-
doWrite((valueWriter) -> assertThatIllegalStateException().isThrownBy(() -> valueWriter.write(list))
249+
doWrite((valueWriter) -> assertThatIllegalStateException().isThrownBy(() -> writer.write(list))
249250
.withMessageStartingWith(
250-
"JSON nesting depth (1001) exceeds maximum depth of 1000 (current path: [0][0][0][0][0][0][0][0][0][0][0][0]"));
251+
"JSON nesting depth (129) exceeds maximum depth of 128 (current path: [0][0][0][0][0][0][0][0][0][0][0][0]"));
251252
}
252253

253254
@Test
254255
void illegalStateExceptionShouldBeThrownWhenMapExceededNestingDepth() {
256+
JsonValueWriter writer = new JsonValueWriter(new StringBuilder(), 128);
255257
Map<String, Object> map = new LinkedHashMap<>();
256258
map.put("foo", Map.of("bar", map));
257-
doWrite((valueWriter) -> assertThatIllegalStateException().isThrownBy(() -> valueWriter.write(map))
259+
doWrite((valueWriter) -> assertThatIllegalStateException().isThrownBy(() -> writer.write(map))
258260
.withMessageStartingWith(
259-
"JSON nesting depth (1001) exceeds maximum depth of 1000 (current path: foo.bar.foo.bar.foo.bar.foo"));
261+
"JSON nesting depth (129) exceeds maximum depth of 128 (current path: foo.bar.foo.bar.foo.bar.foo"));
260262
}
261263

262264
@Test
263265
void illegalStateExceptionShouldBeThrownWhenIterableExceededNestingDepth() {
266+
JsonValueWriter writer = new JsonValueWriter(new StringBuilder(), 128);
264267
List<Object> list = new ArrayList<>();
265268
list.add(list);
266269
doWrite((valueWriter) -> assertThatIllegalStateException()
267-
.isThrownBy(() -> valueWriter.write((Iterable<Object>) list::iterator))
270+
.isThrownBy(() -> writer.write((Iterable<Object>) list::iterator))
268271
.withMessageStartingWith(
269-
"JSON nesting depth (1001) exceeds maximum depth of 1000 (current path: [0][0][0][0][0][0][0][0][0][0][0][0]"));
272+
"JSON nesting depth (129) exceeds maximum depth of 128 (current path: [0][0][0][0][0][0][0][0][0][0][0][0]"));
270273
}
271274

272275
private <V> String write(V value) {

0 commit comments

Comments
 (0)