|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2024 the original author or authors. |
| 2 | + * Copyright 2012-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.json;
|
18 | 18 |
|
| 19 | +import java.util.ArrayList; |
19 | 20 | import java.util.LinkedHashMap;
|
20 | 21 | import java.util.LinkedHashSet;
|
21 | 22 | import java.util.List;
|
@@ -240,6 +241,36 @@ void endWhenNotStartedThrowsException() {
|
240 | 241 | .isThrownBy(() -> valueWriter.end(Series.ARRAY)));
|
241 | 242 | }
|
242 | 243 |
|
| 244 | + @Test |
| 245 | + void illegalStateExceptionShouldBeThrownWhenCollectionExceededNestingDepth() { |
| 246 | + JsonValueWriter writer = new JsonValueWriter(new StringBuilder(), 128); |
| 247 | + List<Object> list = new ArrayList<>(); |
| 248 | + list.add(list); |
| 249 | + assertThatIllegalStateException().isThrownBy(() -> writer.write(list)) |
| 250 | + .withMessageStartingWith( |
| 251 | + "JSON nesting depth (129) exceeds maximum depth of 128 (current path: [0][0][0][0][0][0][0][0][0][0][0][0]"); |
| 252 | + } |
| 253 | + |
| 254 | + @Test |
| 255 | + void illegalStateExceptionShouldBeThrownWhenMapExceededNestingDepth() { |
| 256 | + JsonValueWriter writer = new JsonValueWriter(new StringBuilder(), 128); |
| 257 | + Map<String, Object> map = new LinkedHashMap<>(); |
| 258 | + map.put("foo", Map.of("bar", map)); |
| 259 | + assertThatIllegalStateException().isThrownBy(() -> writer.write(map)) |
| 260 | + .withMessageStartingWith( |
| 261 | + "JSON nesting depth (129) exceeds maximum depth of 128 (current path: foo.bar.foo.bar.foo.bar.foo"); |
| 262 | + } |
| 263 | + |
| 264 | + @Test |
| 265 | + void illegalStateExceptionShouldBeThrownWhenIterableExceededNestingDepth() { |
| 266 | + JsonValueWriter writer = new JsonValueWriter(new StringBuilder(), 128); |
| 267 | + List<Object> list = new ArrayList<>(); |
| 268 | + list.add(list); |
| 269 | + assertThatIllegalStateException().isThrownBy(() -> writer.write((Iterable<Object>) list::iterator)) |
| 270 | + .withMessageStartingWith( |
| 271 | + "JSON nesting depth (129) exceeds maximum depth of 128 (current path: [0][0][0][0][0][0][0][0][0][0][0][0]"); |
| 272 | + } |
| 273 | + |
243 | 274 | private <V> String write(V value) {
|
244 | 275 | return doWrite((valueWriter) -> valueWriter.write(value));
|
245 | 276 | }
|
|
0 commit comments