r2dbc-h2
r2dbc-h2 copied to clipboard
fails to encode string to varchar due to precedence of JsonCodec over StringCodec
Bug Report
Versions
- Driver: 0.9.0.BUILD-SNAPSHOT
- Database: h2
- Java: 11
- OS: Win10
Current Behavior
JsonCodec tries to encode/decode for non-json string values and fails with exception.
This is because JsonCodec and StringCodec both have supported type as String, and JsonCodec is before StringCodec in the codec list. It throws exception at https://github.com/r2dbc/r2dbc-h2/blob/main/src/main/java/io/r2dbc/h2/codecs/DefaultCodecs.java#L68-L72
Expected behavior/code
It should not use JsonCodec in case the value is not a JSON-string.
Possible Solution
Solution 1
Override https://github.com/r2dbc/r2dbc-h2/blob/4863ba0d4e9735237c24b1cb2205cb428d55fbdb/src/main/java/io/r2dbc/h2/codecs/AbstractCodec.java#L40 in JsonCodec and check if the string is a json serialized string or not
Cons:
- it does not take into account when the someone is storing json serialized string as varchar.
Meanwhile, ClobToStringCodec has a precedence over JsonCodec to encode String value. So we will never save JSON into database.