Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit bc92311

Browse files
author
Marek Potociar
committed
Fixed JERSEY-2946: StackOverFlowError in Resource.Builder.toString().
Change-Id: Ia94cedd8b5355496a741976f3a40600922a20f3c
1 parent 591a613 commit bc92311

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

core-server/src/main/java/org/glassfish/jersey/server/model/Resource.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private Data(
171171

172172
this.extended = extended;
173173

174-
this.names = immutableCopy(names);
174+
this.names = Resource.immutableCopy(names);
175175
this.path = path;
176176
this.pathPattern = (path == null || path.isEmpty())
177177
? PathPattern.OPEN_ROOT_PATH_PATTERN
@@ -578,6 +578,7 @@ private List<Resource.Data> mergeResources(List<Resource.Data> resources) {
578578
}
579579
builder.mergeWith(inner);
580580
resources.remove(j);
581+
//noinspection AssignmentToForLoopParameter
581582
j--;
582583
}
583584
}
@@ -683,10 +684,14 @@ public String toString() {
683684
+ ", subResourceLocator=" + subResourceLocator
684685
+ ", handlerClasses=" + handlerClasses
685686
+ ", handlerInstances=" + handlerInstances
686-
+ ", parentResource=" + parentResource
687+
+ ", parentResource=" + ((parentResource == null) ? "<no parent>" : parentResource.shortToString())
687688
+ ", extended=" + extended
688689
+ '}';
689690
}
691+
692+
private String shortToString() {
693+
return "Builder{names=" + names + ", path='" + path + "'}";
694+
}
690695
}
691696

692697
/**
@@ -1056,7 +1061,7 @@ public String toString() {
10561061

10571062
@Override
10581063
public List<? extends ResourceModelComponent> getComponents() {
1059-
List<ResourceModelComponent> components = new LinkedList<ResourceModelComponent>();
1064+
List<ResourceModelComponent> components = new LinkedList<>();
10601065

10611066
components.addAll(getChildResources());
10621067
components.addAll(getResourceMethods());

core-server/src/test/java/org/glassfish/jersey/server/model/ResourceModelTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
import org.junit.Test;
5353
import static org.junit.Assert.assertEquals;
54+
import static org.junit.Assert.assertNotNull;
5455
import static org.junit.Assert.fail;
5556

5657
import jersey.repackaged.com.google.common.collect.Lists;
@@ -183,6 +184,20 @@ public void testResourceModel() {
183184
"another-child"), false, "PUT");
184185
}
185186

187+
/**
188+
* Reproducer for JERSEY-2946: StackOverFlowError in Resource.toString()
189+
*/
190+
@Test
191+
public void testResourceWithChildrenDoesNotOverflowToString() {
192+
final Resource.Builder parentBuilder = Resource.builder("parent");
193+
final Resource.Builder childBuilder = parentBuilder.addChildResource("child");
194+
final Resource.Builder locatorBuilder = parentBuilder.addChildResource("child");
195+
196+
// In Jersey 2.21 this throws StackOverFlowError.
197+
assertNotNull(childBuilder.toString());
198+
assertNotNull(parentBuilder.toString());
199+
}
200+
186201
@Test
187202
public void testRuntimeResourceModel() {
188203
ResourceModel resourceModel = getResourceModel();

0 commit comments

Comments
 (0)