|
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,34 @@ void endWhenNotStartedThrowsException() {
|
240 | 241 | .isThrownBy(() -> valueWriter.end(Series.ARRAY)));
|
241 | 242 | }
|
242 | 243 |
|
| 244 | + @Test |
| 245 | + void illegalStateExceptionShouldBeThrownWhenCollectionExceededNestingDepth() { |
| 246 | + List<Object> list = new ArrayList<>(); |
| 247 | + list.add(list); |
| 248 | + doWrite((valueWriter) -> assertThatIllegalStateException().isThrownBy(() -> valueWriter.write(list)) |
| 249 | + .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 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + void illegalStateExceptionShouldBeThrownWhenMapExceededNestingDepth() { |
| 255 | + Map<String, Object> map = new LinkedHashMap<>(); |
| 256 | + map.put("foo", Map.of("bar", map)); |
| 257 | + doWrite((valueWriter) -> assertThatIllegalStateException().isThrownBy(() -> valueWriter.write(map)) |
| 258 | + .withMessageStartingWith( |
| 259 | + "JSON nesting depth (1001) exceeds maximum depth of 1000 (current path: foo.bar.foo.bar.foo.bar.foo")); |
| 260 | + } |
| 261 | + |
| 262 | + @Test |
| 263 | + void illegalStateExceptionShouldBeThrownWhenIterableExceededNestingDepth() { |
| 264 | + List<Object> list = new ArrayList<>(); |
| 265 | + list.add(list); |
| 266 | + doWrite((valueWriter) -> assertThatIllegalStateException() |
| 267 | + .isThrownBy(() -> valueWriter.write((Iterable<Object>) list::iterator)) |
| 268 | + .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]")); |
| 270 | + } |
| 271 | + |
243 | 272 | private <V> String write(V value) {
|
244 | 273 | return doWrite((valueWriter) -> valueWriter.write(value));
|
245 | 274 | }
|
|
0 commit comments