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

fix: EXPOSED-130 Logger throws ClassCastException with JSON and ListSerializer #1835

Merged
merged 2 commits into from
Aug 17, 2023

Conversation

bog-walk
Copy link
Member

When a JSON/JSONB column that takes a List or ListSerializer is created, it correctly deserializes/serializes the JSON array to and from the database. However, attempting to log any SQL statements with the collection as a String throws a ClassCastException.

If the column is created to take an Array or ArraySerializer instead, then no exception is thrown when the logger is used.

This happens because lists are processed by ColumnType.valueToString() as ArrayList, which extends the Iterable superclass and is handled separately:

fun valueToString(value: Any?): String = when (value) {
    null -> {
        check(nullable) { "NULL in non-nullable column" }
        "NULL"
    }
    DefaultValueMarker -> "DEFAULT"
    is Iterable<*> -> value.joinToString(",", transform = ::valueToString)
    else -> nonNullValueToString(value)
}

Whereas arrays are processed as Array (or another type-specific class), which does not extend Iterable, so the else branch is reached.

This function now has an override in JsonColumnType.

…erializer

When a json/jsonb column is created that takes a List or ListSerializer, it correctly
deserializes/serializes the json array to and from the database. However, attempting
to log the statements involved throws ClassCastException, whereas there is no
issue if the column is created to take an Array or ArraySerializer instead.

The former extend Iterable, which is being handled separately in ColumnType.valueToString().
Overriding this branch to directly use the result of nonNullValueToString() fixes
the logger issue.
@bog-walk bog-walk requested review from e5l and joc-a August 16, 2023 19:51
@bog-walk bog-walk merged commit fb94e6a into main Aug 17, 2023
3 checks passed
@bog-walk bog-walk deleted the bog-walk/fix-json-logger-list branch August 17, 2023 12:57
saral pushed a commit to saral/Exposed that referenced this pull request Oct 3, 2023
…erializer (JetBrains#1835)

* fix: EXPOSED-130 Logger throws ClassCastException with JSON and ListSerializer

When a json/jsonb column is created that takes a List or ListSerializer, it correctly
deserializes/serializes the json array to and from the database. However, attempting
to log the statements involved throws ClassCastException, whereas there is no
issue if the column is created to take an Array or ArraySerializer instead.

The former extend Iterable, which is being handled separately in ColumnType.valueToString().
Overriding this branch to directly use the result of nonNullValueToString() fixes
the logger issue.
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.

3 participants