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

Null in array serialized wrongly #488

Closed
Katrix opened this issue Aug 4, 2020 · 3 comments
Closed

Null in array serialized wrongly #488

Katrix opened this issue Aug 4, 2020 · 3 comments

Comments

@Katrix
Copy link

Katrix commented Aug 4, 2020

Using code like this

implicit private val listOptionStrType: DriverJdbcType[List[Option[String]]] =
  new SimpleArrayJdbcType[String]("text")
    .mapTo[Option[String]](Option(_), _.orNull)
    .to(_.toList)

Trying to insert a None in the list produces an empty string for that element, and not the NULL identifier. For example, trying to insert List(Some("[2.3,)"), Some("[0.3.0,)"), None, Some("7.1.0"), None)
produces the error org.postgresql.util.PSQLException: ERROR: malformed array literal: "{"[2.3,)","[0.3.0,)",,7.1.0,}".

From the Postgres docs, NULL values should be written as null, and not as an empty string

To set an element of an array constant to NULL, write NULL for the element value. (Any upper- or lower-case variant of NULL will do.)

@Katrix
Copy link
Author

Katrix commented Aug 4, 2020

Here's a working workaround which I assume works on the fact that String is a primitive

implicit private val listOptionStrType: DriverJdbcType[List[Option[String]]] =
  new SimpleArrayJdbcType[String]("text")
    .to[List](
      _.map(Option(_)).toList.asInstanceOf[List[String]],
      _.asInstanceOf[List[Option[String]]].map(_.orNull)
    )
    .asInstanceOf[DriverJdbcType[List[Option[String]]]]

@tminglei
Copy link
Owner

tminglei commented Aug 8, 2020

@Katrix well, I missed to test List[Option[_]].

tminglei added a commit that referenced this issue Aug 8, 2020
@tminglei
Copy link
Owner

tminglei commented Aug 8, 2020

@Katrix fixed, pls help double check. Thanks!

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