Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#180] Add a JsonCodec #191

Merged
merged 1 commit into from
Jun 9, 2021
Merged

[#180] Add a JsonCodec #191

merged 1 commit into from
Jun 9, 2021

Conversation

lukaseder
Copy link
Contributor

Issue description

Fixes #180

New Public APIs

None

@martypitt
Copy link

Would love to see this merged and released, as it's blocking us. In the mean time, I'll use the excellent examples here to work-around. Thanks to @lukaseder for this!

@gregturn gregturn merged commit 7e9d60e into r2dbc:main Jun 9, 2021
@gregturn
Copy link
Contributor

gregturn commented Jun 9, 2021

Thanks @lukaseder.

If you could give the snapshots a spin @martypitt, let us know if you see any issues.

@cbrunsdon
Copy link

@gregturn I pulled down the snapshot and ran into problems.

JsonCodec extends AbstractCodec<String>, which means the canEncode call here will return true for any strings passed in.

This causes issues when calling .bind with a string parameter.

Since the JsonCodec is earlier in the list of DefaultCodecs, it will be the first one picked up here causing a DbException when the JsonCodec can't encode a non-json string.

You end up with a stack trace of something like:

 Data conversion error converting "MyString" [22018-200]
org.h2.message.DbException: Data conversion error converting "MyString" [22018-200]
	at org.h2.message.DbException.get(DbException.java:205)
	at org.h2.message.DbException.get(DbException.java:181)
	at org.h2.value.ValueJson.fromJson(ValueJson.java:161)
	at io.r2dbc.h2.codecs.JsonCodec.doEncode(JsonCodec.java:41)
	at io.r2dbc.h2.codecs.JsonCodec.doEncode(JsonCodec.java:23)
	at io.r2dbc.h2.codecs.AbstractCodec.encode(AbstractCodec.java:68)
	at io.r2dbc.h2.codecs.DefaultCodecs.encode(DefaultCodecs.java:70)
	at io.r2dbc.h2.H2Statement.addIndex(H2Statement.java:130)
	at io.r2dbc.h2.H2Statement.bind(H2Statement.java:75)
	at io.r2dbc.h2.H2Statement.bind(H2Statement.java:39)

@lukaseder
Copy link
Contributor Author

Since the JsonCodec is earlier in the list of DefaultCodecs, it will be the first one picked up here causing a DbException when the JsonCodec can't encode a non-json string.

Ah, nice catch. Did I overlook something, or do the tests not catch this problem?

@dragneelfps
Copy link

Hi @lukaseder , any idea when this might be released on maven central?

@lukaseder
Copy link
Contributor Author

Hi @lukaseder , any idea when this might be released on maven central?

Nope. For I am not in charge here ;-)

@dragneelfps
Copy link

dragneelfps commented Jun 23, 2021

So I tried using io.r2dbc:r2dbc-h2:0.9.0.BUILD-SNAPSHOT and it fails to encode varchar(in database) into string(in entity class), since JsonCodec tries to encode before StringCodec.

cbrunsdon added a commit to cbrunsdon/r2dbc-h2 that referenced this pull request Jun 23, 2021
Previously, no tests would encode a string with all DefaultCodecs
loaded, leaving a hole in tests when multiple codecs would try and
handle encoding of Strings.

Exposes a bug in r2dbc#191.
@cbrunsdon
Copy link

Since the JsonCodec is earlier in the list of DefaultCodecs, it will be the first one picked up here causing a DbException when the JsonCodec can't encode a non-json string.

Ah, nice catch. Did I overlook something, or do the tests not catch this problem?

The test's don't catch the problem. From what I can tell, nothing in DefaultCodecs (or anything else) does a test of string conversion with all the default codecs loaded. Not 100% sure this is the right test for it, but threw up a commit with a test that exposes the problem:

cbrunsdon@0d644c1

@dragneelfps
Copy link

dragneelfps commented Jun 23, 2021 via email

@jkamcc
Copy link

jkamcc commented Mar 28, 2022

Hello, this PR would be also good to be merged to 0.8.x branch, due that 0.9.x changes are used by an spi and r2dbc version currently (March, 22) that is only supported by a spring boot data r2dbc version not compatible with current stable release of spring boot 2.6.x, and only with 2.7.x which is in M3 stage and not released yet to a stable version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Selecting from a table with JSON column is not possible.
6 participants