klonq klonq - 3 months ago 9
JSON Question

OmniFaces Json#encode() stack overflow with IllegalArgumentException: Cannot invoke getter of property 'classes' of bean 'class java.lang.Class'

I am experiencing a stackoverflow exception doing exactly as explained in the documentation (here)

The error occurs when sending anything other than a String (ie a bean, or HashMap).

@Push
@Inject
private PushContext console;

void onEvent(@Observes ApplicationEvent event) {

final Map<String, Object> dto = new HashMap<>();
dto.put("timestamp", event.getTimestamp());
dto.put("message", event.getMessage());
dto.put("severity", event.getSeverity());
console.send(dto);

// console.send(event); This line does not work
// console.send(event.getMessage()); This line works
}


.

Stack trace

Caused by: java.lang.IllegalArgumentException: Cannot invoke getter of property 'classes' of bean 'class java.lang.Class'.
at org.omnifaces.util.Json.encodeBean(Json.java:178)
at org.omnifaces.util.Json.encode(Json.java:89)
at org.omnifaces.util.Json.encodeBean(Json.java:191)
at org.omnifaces.util.Json.encode(Json.java:89)
at org.omnifaces.util.Json.encodeArray(Json.java:123)
at org.omnifaces.util.Json.encode(Json.java:83)
at org.omnifaces.util.Json.encodeBean(Json.java:191)
at org.omnifaces.util.Json.encode(Json.java:89)
at org.omnifaces.util.Json.encodeBean(Json.java:191)
at org.omnifaces.util.Json.encode(Json.java:89)
at org.omnifaces.util.Json.encodeArray(Json.java:123)
at org.omnifaces.util.Json.encode(Json.java:83)
at org.omnifaces.util.Json.encodeBean(Json.java:191)
at org.omnifaces.util.Json.encode(Json.java:89)
at org.omnifaces.util.Json.encodeBean(Json.java:191)
at org.omnifaces.util.Json.encode(Json.java:89)
at org.omnifaces.util.Json.encodeBean(Json.java:191)
at org.omnifaces.util.Json.encode(Json.java:89)
at org.omnifaces.util.Json.encodeArray(Json.java:123)
// Repeats to infinity & beyond

Answer

It will indeed fail that way when one of map/bean properties is a Class object whose classes property in turn references itself. Perhaps it's the severity?

I have as per this commit improved Json#encode() to just print Class#getName() when a Class instance is encountered instead of handling it as a bean. The fix is available as per OmniFaces 2.5.

Comments