Skip to content

Generics not correctly deserialized for Spanner JSON columns #4317

@hesch

Description

@hesch

Describe the bug
Deserializing generic types with spring cloud gcp spanner does not work.
If I have classes like this:

@Table(name = "MyEntity")
class MyEntity (
    val id: String,
    @Column(name = "jsonField", spannerType = TypeCode.JSON)
    val myGenericJsonField: GenericType<SimpleType>,
)

data class GenericType<T> (
    val genericField: T
)

data class SimpleType(
    val test: String
)

with the following schema:

CREATE TABLE IF NOT EXISTS MyEntity(
  id STRING(36) PRIMARY KEY,
  jsonField JSON
)

After writing to spanner and reading the entity back it will not restore the generic type correctly. So instead of genericField being SimpleType, it fills it with a com.google.gson.internal.LinkedTreeMap.

The problem seems to be that in SpannerPersistenPropertyImpl.getColumnInnerType not the whole TypeInformation is preserved, so later there is no way to give gson a TypeToken to actually reconstruct the correct type.

Sample
There is a reproduction repo here: https://github.com/hesch/spring-cloud-gcp-generic-repro

With a test that fails here: https://github.com/hesch/spring-cloud-gcp-generic-repro/blob/main/src/test/kotlin/org/example/GenericTest.kt

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions