doriordan / skuber

A Scala Kubernetes client library
Apache License 2.0
326 stars 97 forks source link

skuber.json.base64format is fundamentally broken #402

Open jroper opened 1 month ago

jroper commented 1 month ago

This code:

  implicit val base64Format: Format[Array[Byte]] = (
      JsPath.format[String].inmap(s => Base64.decodeBase64(s), (bytes: Array[Byte]) => Base64.encodeBase64String(bytes))
  )

https://github.com/doriordan/skuber/blob/master/client/src/main/scala/skuber/json/package.scala#L1070-L1072

Is fundamentally broken. JsPath.format is used to read and produce JSON objects, but this formatter is producing a JsString. The result, when you attempt to serialise an object that uses this formatter, is this error:

java.lang.RuntimeException: when empty JsPath, expecting JsObject
    at play.api.libs.json.JsPath$.step$1(JsPath.scala:141)
    at play.api.libs.json.JsPath$.buildSubPath$1(JsPath.scala:156)
    at play.api.libs.json.JsPath$.$anonfun$createObj$2(JsPath.scala:173)
    at scala.collection.immutable.ArraySeq.foldLeft(ArraySeq.scala:222)
    at play.api.libs.json.JsPath$.createObj(JsPath.scala:172)
    at play.api.libs.json.PathWrites.$anonfun$at$3(JsConstraints.scala:188)
        ...

What it should be is this:

  implicit val base64Format: Format[Array[Byte]] = (
      implicitly[Format[String]].inmap(s => Base64.decodeBase64(s), (bytes: Array[Byte]) => Base64.encodeBase64String(bytes))
  )