r2dbc-h2 icon indicating copy to clipboard operation
r2dbc-h2 copied to clipboard

fails to encode string to varchar due to precedence of JsonCodec over StringCodec

Open dragneelfps opened this issue 4 years ago • 1 comments
trafficstars

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.

dragneelfps avatar Jun 24 '21 06:06 dragneelfps

Meanwhile, ClobToStringCodec has a precedence over JsonCodec to encode String value. So we will never save JSON into database.

JohnNiang avatar Jun 13 '25 15:06 JohnNiang