Skip to content

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

@dragneelfps

Description

@dragneelfps

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

public boolean canEncode(Object value) {
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions