Search code examples
jooqjooq-codegen-mavenjooq-codegen

jooq json converter not working when query: class org.jooq.JSONB cannot be cast to class java.util.List


I can save data into postgres using jsonConverter with following setting

 <forcedType>
     <userType>java.util.List&lt;com.sify.MyType&gt;</userType>
     <jsonConverter>true</jsonConverter>
     <includeExpression>table\.field</includeExpression>
</forcedType>

However, if I select data from postgres, jsonb columns aren't converted to the List<MyType>. It reports error: class org.jooq.JSONB cannot be cast to class java.util.List, when calling TableRecord.getField() method.


Edit: I tried using a custom binding like this: https://github.com/jOOQ/jOOQ/issues/9679, the behavior is the same


Edit: I checked the generated code, TableRecord.getField() is just

    public List<Field> getField() {
        return (List<Field>) get(12);
    }

Maybe it should be get(12, (Class<List<Field>>) (Class<?>) List.class) instead?


Solution

  • jOOQ cannot pass along a Class<List<MyType>> reference to Jackson, because the generics are erased by the Java compiler, so Jackson will simply receive Class<List>. This is different if you map your JSONB document to a com.sify.MyType[] array type, which is still available at runtime.

    I don't think there will be an easy solution using the out-of-the-box JSONBtoJacksonConverter implementation in jOOQ 3.20, but do have a look at its implementation. You can probably easily implement a Converter<JSONB, List<MyType>> by working with the Jackson API directly.

    For a future version of jOOQ, there might be an out-of-the-box heuristic that tries to pass List type information to Jackson CollectionType, or similar: