Skip to content

Commit 5fb9f86

Browse files
authored
Value access calls toString() on failure, potentially leading to infinite recursion #456 (#570)
Signed-off-by: Jorge Bescos Gascon <[email protected]>
1 parent b65fb10 commit 5fb9f86

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/main/java/org/eclipse/yasson/internal/serializer/ValueGetterSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void serialize(Object value, JsonGenerator generator, SerializationContex
3838
try {
3939
object = valueGetter.invoke(value);
4040
} catch (Throwable e) {
41-
throw new JsonbException("Error getting value on: " + value, e);
41+
throw new JsonbException("Error getting value on: " + value.getClass().getName(), e);
4242
}
4343
delegate.serialize(object, generator, context);
4444
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
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;
14+
15+
import static org.junit.jupiter.api.Assertions.fail;
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
import jakarta.json.bind.JsonbBuilder;
20+
import jakarta.json.bind.JsonbException;
21+
22+
public class Issue456Test {
23+
24+
@Test
25+
public void dontInvokeToString() {
26+
try {
27+
JsonbBuilder.create().toJson(new Example());
28+
fail("JsonbException is expected");
29+
} catch (JsonbException e) {
30+
// Expected
31+
}
32+
}
33+
34+
public static class Example {
35+
36+
public String getProperty() {
37+
throw new RuntimeException("some error");
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return JsonbBuilder.create().toJson(this);
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)