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

nullable @SqlJsonValue and json 'null' #5

Open
arvearve opened this issue Nov 27, 2024 · 1 comment
Open

nullable @SqlJsonValue and json 'null' #5

arvearve opened this issue Nov 27, 2024 · 1 comment

Comments

@arvearve
Copy link

In a scenario where we try to deserialize a @SqlJsonValue to a nullable field, where the database row contains the jsonb value
'null', (i.e. not SQL-null but json-null), the deserialization process crashes.

data class NullableExample(val id: Int, @SqlJsonValue val value: MyPersonJson?)

Sql("""INSERT INTO JsonbExample (id, value) VALUES (1, 'null')""").action().runOn(ctx)

Sql("SELECT id, value FROM JsonbExample").queryOf<NullableExample>().runOn(ctx)
Unexpected JSON token at offset 0: Expected start of the object '{', but had 'n' instead at path: $
JSON input: null
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 0: Expected start of the object '{', but had 'n' instead at path: $
JSON input: null

It appears that in RowDecoder, after validNullOrElse check the result is passed to the decoder; however at some point in the call stack the nullability of the target type is lost.

A minimal example of what is happening at kotlinx.serialization level:

Json.decodeFromString<JsonSpecData.A.MyPerson?>("null") shouldBe null // ok
Json.decodeFromString<JsonSpecData.A.MyPerson>("null") shouldBe null // exception as above

I was unable to figure out where this happens, so no fix is included - however a branch with a test for this case can be found here:
main...arvearve:terpal-sql:json-nullable

@deusaquilus
Copy link
Member

I see. @SqlJsonValue takes a slightly different code path. Let me have a closer look.

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

No branches or pull requests

2 participants