Skip to content

Commit daf79b2

Browse files
committed
[682] Correct the fix for 670 and return the type if the type has already been resolved.
Signed-off-by: James R. Perkins <jperkins@ibm.com>
1 parent 40c0444 commit daf79b2

File tree

5 files changed

+69
-20
lines changed

5 files changed

+69
-20
lines changed

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

Lines changed: 5 additions & 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 || resolvedArgs[i].equals(typeToResolve)) {
255+
if (resolvedArgs[i] == null) {
256256
if (typeToSearch instanceof Class) {
257257
return Object.class;
258258
}
@@ -262,6 +262,10 @@ public static Type resolveTypeArguments(ParameterizedType typeToResolve, Type ty
262262
typeToSearch));
263263
}
264264
}
265+
// The expected type and the resolved type are the same, simply return the type
266+
if (resolvedArgs[i].equals(typeToResolve)) {
267+
return typeToResolve;
268+
}
265269
if (resolvedArgs[i] instanceof ParameterizedType) {
266270
resolvedArgs[i] = resolveTypeArguments((ParameterizedType) resolvedArgs[i], typeToSearch);
267271
} else if (unresolvedArg instanceof GenericArrayType) {

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.eclipse.yasson.defaultmapping.generics.model.GenericArrayClass;
5353
import org.eclipse.yasson.defaultmapping.generics.model.GenericTestClass;
5454
import org.eclipse.yasson.defaultmapping.generics.model.GenericWithUnboundedWildcardClass;
55+
import org.eclipse.yasson.defaultmapping.generics.model.ListContainer;
5556
import org.eclipse.yasson.defaultmapping.generics.model.LowerBoundTypeVariableWithCollectionAttributeClass;
5657
import org.eclipse.yasson.defaultmapping.generics.model.MultiLevelExtendedGenericTestClass;
5758
import org.eclipse.yasson.defaultmapping.generics.model.MultipleBoundsContainer;
@@ -516,14 +517,32 @@ public void wildcardCollectionContainer() {
516517
}
517518

518519
@Test
519-
public void genericUpperBoundContainer() {
520-
final String expectedJson = "{\"tree\":{\"children\":[{\"children\":[],\"name\":\"child\"}],\"name\":\"parent\"}}";
520+
public void genericUpperBoundContainer() throws Exception {
521+
final String expectedJson = "{\"tree\":{\"children\":[{\"name\":\"child\"}],\"name\":\"parent\"}}";
521522
final TreeContainer<TreeElement> container = new TreeContainer<>();
522523
final TreeElement parent = new TreeElement("parent");
523524
parent.setChildren(List.of(new TreeElement("child")));
524525
container.setTree(parent);
525526

526-
assertEquals(expectedJson, defaultJsonb.toJson(container));
527+
// Use a new instance of Jsonb to avoid any caching
528+
try (var jsonb = JsonbBuilder.create()) {
529+
assertEquals(expectedJson, jsonb.toJson(container));
530+
}
531+
532+
}
533+
534+
@Test
535+
public void genericUpperBoundContainerWithListContainer() throws Exception {
536+
final String expectedJson = "{\"list\":[{\"children\":[{\"name\":\"child\"}],\"name\":\"parent\"}]}";
537+
final ListContainer<TreeElement> container = new ListContainer<>();
538+
final TreeElement parent = new TreeElement("parent");
539+
parent.setChildren(List.of(new TreeElement("child")));
540+
container.setList(List.of(parent));
541+
542+
// Use a new instance of Jsonb to avoid any caching
543+
try (var jsonb = JsonbBuilder.create()) {
544+
assertEquals(expectedJson, jsonb.toJson(container));
545+
}
527546

528547
}
529548

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2025 IBM, 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+
*
19+
* @author <a href="mailto:jperkins@ibm.com">James R. Perkins</a>
20+
*/
21+
public class ListContainer<T> {
22+
23+
private List<T> list;
24+
25+
public List<T> getList() {
26+
return list;
27+
}
28+
29+
public void setList(List<T> list) {
30+
this.list = list;
31+
}
32+
33+
}

src/test/java/org/eclipse/yasson/defaultmapping/generics/model/TreeElement.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@
1212

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

15-
import java.util.ArrayList;
16-
import java.util.List;
17-
1815
/**
1916
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
2017
*/
21-
public class TreeElement implements TreeTypeContainer<TreeElement> {
18+
public class TreeElement extends TreeTypeContainer<TreeElement> {
2219

2320
private final String name;
24-
private List<TreeElement> children = new ArrayList<>();
2521

2622
public TreeElement(final String name) {
2723
this.name = name;
@@ -30,12 +26,4 @@ public TreeElement(final String name) {
3026
public String getName() {
3127
return name;
3228
}
33-
34-
public List<TreeElement> getChildren() {
35-
return children;
36-
}
37-
38-
public void setChildren(final List<TreeElement> children) {
39-
this.children = children;
40-
}
4129
}

src/test/java/org/eclipse/yasson/defaultmapping/generics/model/TreeTypeContainer.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
/**
1818
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
1919
*/
20-
public interface TreeTypeContainer<T extends TreeTypeContainer<T>> {
20+
public class TreeTypeContainer<T extends TreeTypeContainer<T>> {
21+
private List<T> children;
2122

22-
List<T> getChildren();
23+
public List<T> getChildren() {
24+
return children;
25+
}
2326

24-
void setChildren(List<T> children);
27+
public void setChildren(final List<T> children) {
28+
this.children = children;
29+
}
2530
}

0 commit comments

Comments
 (0)