Skip to content

Commit abde7ec

Browse files
jamezpVerdent
authored andcommitted
[670] Do not recursively check the upper bound type if it matches the original type to avoid a StackOverflowError.
Signed-off-by: James R. Perkins <[email protected]>
1 parent a2cfd89 commit abde7ec

File tree

5 files changed

+118
-10
lines changed

5 files changed

+118
-10
lines changed

src/main/java/org/eclipse/yasson/internal/ReflectionUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public static Type resolveTypeArguments(ParameterizedType typeToResolve, Type ty
252252
}
253253
}
254254

255-
if (resolvedArgs[i] == null) {
255+
if (resolvedArgs[i] == null || resolvedArgs[i].equals(typeToResolve)) {
256256
if (typeToSearch instanceof Class) {
257257
return Object.class;
258258
}

src/test/java/org/eclipse/yasson/defaultmapping/generics/GenericsTest.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@
1212

1313
package org.eclipse.yasson.defaultmapping.generics;
1414

15+
import static org.eclipse.yasson.Jsonbs.defaultJsonb;
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertThrows;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
20+
import java.lang.reflect.Field;
1521
import java.lang.reflect.Type;
16-
import java.lang.reflect.WildcardType;
1722
import java.math.BigDecimal;
1823
import java.text.ParseException;
1924
import java.text.SimpleDateFormat;
2025
import java.util.ArrayList;
2126
import java.util.Arrays;
27+
import java.util.Collection;
2228
import java.util.HashMap;
2329
import java.util.HashSet;
2430
import java.util.LinkedList;
@@ -30,8 +36,6 @@
3036
import jakarta.json.bind.Jsonb;
3137
import jakarta.json.bind.JsonbBuilder;
3238
import jakarta.json.bind.JsonbConfig;
33-
import java.lang.reflect.Field;
34-
import java.util.Collection;
3539
import org.eclipse.yasson.TestTypeToken;
3640
import org.eclipse.yasson.adapters.model.GenericBox;
3741
import org.eclipse.yasson.defaultmapping.generics.model.AnotherGenericTestClass;
@@ -48,24 +52,21 @@
4852
import org.eclipse.yasson.defaultmapping.generics.model.GenericArrayClass;
4953
import org.eclipse.yasson.defaultmapping.generics.model.GenericTestClass;
5054
import org.eclipse.yasson.defaultmapping.generics.model.GenericWithUnboundedWildcardClass;
55+
import org.eclipse.yasson.defaultmapping.generics.model.LowerBoundTypeVariableWithCollectionAttributeClass;
5156
import org.eclipse.yasson.defaultmapping.generics.model.MultiLevelExtendedGenericTestClass;
5257
import org.eclipse.yasson.defaultmapping.generics.model.MultipleBoundsContainer;
5358
import org.eclipse.yasson.defaultmapping.generics.model.MyCyclicGenericClass;
5459
import org.eclipse.yasson.defaultmapping.generics.model.PropagatedGenericClass;
5560
import org.eclipse.yasson.defaultmapping.generics.model.Shape;
5661
import org.eclipse.yasson.defaultmapping.generics.model.StaticCreatorContainer;
62+
import org.eclipse.yasson.defaultmapping.generics.model.TreeContainer;
63+
import org.eclipse.yasson.defaultmapping.generics.model.TreeElement;
5764
import org.eclipse.yasson.defaultmapping.generics.model.WildCardClass;
5865
import org.eclipse.yasson.defaultmapping.generics.model.WildcardMultipleBoundsClass;
5966
import org.eclipse.yasson.serializers.model.Box;
6067
import org.eclipse.yasson.serializers.model.Crate;
6168
import org.junit.jupiter.api.Test;
6269

63-
import static org.eclipse.yasson.Jsonbs.defaultJsonb;
64-
import org.eclipse.yasson.defaultmapping.generics.model.LowerBoundTypeVariableWithCollectionAttributeClass;
65-
import static org.junit.jupiter.api.Assertions.assertEquals;
66-
import static org.junit.jupiter.api.Assertions.assertThrows;
67-
import static org.junit.jupiter.api.Assertions.assertTrue;
68-
6970
/**
7071
* This class contains JSONB default mapping generics tests.
7172
*
@@ -513,6 +514,18 @@ public void wildcardCollectionContainer() {
513514
final CollectionContainer result = defaultJsonb.fromJson(expectedJson, CollectionContainer.class);
514515
assertEquals(collectionContainer, result);
515516
}
517+
518+
@Test
519+
public void genericUpperBoundContainer() {
520+
final String expectedJson = "{\"tree\":{\"children\":[{\"children\":[],\"name\":\"child\"}],\"name\":\"parent\"}}";
521+
final TreeContainer<TreeElement> container = new TreeContainer<>();
522+
final TreeElement parent = new TreeElement("parent");
523+
parent.setChildren(List.of(new TreeElement("child")));
524+
container.setTree(parent);
525+
526+
assertEquals(expectedJson, defaultJsonb.toJson(container));
527+
528+
}
516529

517530
public interface FunctionalInterface<T> {
518531
T getValue();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2025 Red Hat, Inc. and/or its affiliates.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
package org.eclipse.yasson.defaultmapping.generics.model;
14+
15+
/**
16+
* @author <a href="mailto:[email protected]">James R. Perkins</a>
17+
*/
18+
public class TreeContainer<T extends TreeTypeContainer<T>> {
19+
20+
private TreeTypeContainer<T> tree;
21+
22+
public TreeTypeContainer<T> getTree() {
23+
return tree;
24+
}
25+
26+
public void setTree(final TreeTypeContainer<T> tree) {
27+
this.tree = tree;
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2025 Red Hat, Inc. and/or its affiliates.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
package org.eclipse.yasson.defaultmapping.generics.model;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
/**
19+
* @author <a href="mailto:[email protected]">James R. Perkins</a>
20+
*/
21+
public class TreeElement implements TreeTypeContainer<TreeElement> {
22+
23+
private final String name;
24+
private List<TreeElement> children = new ArrayList<>();
25+
26+
public TreeElement(final String name) {
27+
this.name = name;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public List<TreeElement> getChildren() {
35+
return children;
36+
}
37+
38+
public void setChildren(final List<TreeElement> children) {
39+
this.children = children;
40+
}
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2025 Red Hat, Inc. and/or its affiliates.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
package org.eclipse.yasson.defaultmapping.generics.model;
14+
15+
import java.util.List;
16+
17+
/**
18+
* @author <a href="mailto:[email protected]">James R. Perkins</a>
19+
*/
20+
public interface TreeTypeContainer<T extends TreeTypeContainer<T>> {
21+
22+
List<T> getChildren();
23+
24+
void setChildren(List<T> children);
25+
}

0 commit comments

Comments
 (0)