sirthias / borer

Efficient CBOR and JSON (de)serialization in Scala
https://sirthias.github.io/borer/
Mozilla Public License 2.0
224 stars 14 forks source link

Unexpected NPE in runtime when order of derivation of depended codecs is wrong #27

Closed plokhotnyuk closed 5 years ago

plokhotnyuk commented 5 years ago

To reproduce the stacktrace bellow need to move this line to be bellow this derivation and run this test.

java.lang.NullPointerException (input position 35)
io.bullet.borer.Borer$Error$General: java.lang.NullPointerException (input position 35)
    at io.bullet.borer.DecodingSetup$Impl.value(DecodingSetup.scala:101)
    at com.github.plokhotnyuk.jsoniter_scala.benchmark.AnyValsReading.borerJson(AnyValsReading.scala:25)
    at com.github.plokhotnyuk.jsoniter_scala.benchmark.AnyValsReadingSpec.$anonfun$new$2(AnyValsReadingSpec.scala:9)
    at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
    at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
    at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
    at org.scalatest.Transformer.apply(Transformer.scala:22)
    at org.scalatest.Transformer.apply(Transformer.scala:20)
    at org.scalatest.WordSpecLike$$anon$3.apply(WordSpecLike.scala:1075)
    at org.scalatest.TestSuite.withFixture(TestSuite.scala:196)
    at org.scalatest.TestSuite.withFixture$(TestSuite.scala:195)
    at org.scalatest.WordSpec.withFixture(WordSpec.scala:1881)
    at org.scalatest.WordSpecLike.invokeWithFixture$1(WordSpecLike.scala:1073)
    at org.scalatest.WordSpecLike.$anonfun$runTest$1(WordSpecLike.scala:1085)
    at org.scalatest.SuperEngine.runTestImpl(Engine.scala:286)
    at org.scalatest.WordSpecLike.runTest(WordSpecLike.scala:1085)
    at org.scalatest.WordSpecLike.runTest$(WordSpecLike.scala:1067)
    at org.scalatest.WordSpec.runTest(WordSpec.scala:1881)
    at org.scalatest.WordSpecLike.$anonfun$runTests$1(WordSpecLike.scala:1144)
    at org.scalatest.SuperEngine.$anonfun$runTestsInBranch$1(Engine.scala:393)
    at scala.collection.immutable.List.foreach(List.scala:392)
    at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:381)
    at org.scalatest.SuperEngine.runTestsInBranch(Engine.scala:370)
    at org.scalatest.SuperEngine.$anonfun$runTestsInBranch$1(Engine.scala:407)
    at scala.collection.immutable.List.foreach(List.scala:392)
    at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:381)
    at org.scalatest.SuperEngine.runTestsInBranch(Engine.scala:376)
    at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:458)
    at org.scalatest.WordSpecLike.runTests(WordSpecLike.scala:1144)
    at org.scalatest.WordSpecLike.runTests$(WordSpecLike.scala:1143)
    at org.scalatest.WordSpec.runTests(WordSpec.scala:1881)
    at org.scalatest.Suite.run(Suite.scala:1124)
    at org.scalatest.Suite.run$(Suite.scala:1106)
    at org.scalatest.WordSpec.org$scalatest$WordSpecLike$$super$run(WordSpec.scala:1881)
    at org.scalatest.WordSpecLike.$anonfun$run$1(WordSpecLike.scala:1189)
    at org.scalatest.SuperEngine.runImpl(Engine.scala:518)
    at org.scalatest.WordSpecLike.run(WordSpecLike.scala:1189)
    at org.scalatest.WordSpecLike.run$(WordSpecLike.scala:1187)
    at org.scalatest.WordSpec.run(WordSpec.scala:1881)
    at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:45)
    at org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$13(Runner.scala:1349)
    at org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$13$adapted(Runner.scala:1343)
    at scala.collection.immutable.List.foreach(List.scala:392)
    at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1343)
    at org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24(Runner.scala:1033)
    at org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24$adapted(Runner.scala:1011)
    at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1509)
    at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1011)
    at org.scalatest.tools.Runner$.run(Runner.scala:850)
    at org.scalatest.tools.Runner.run(Runner.scala)
    at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:131)
    at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)
Caused by: java.lang.NullPointerException
    at io.bullet.borer.InputReader.read(Reader.scala:352)
    at io.bullet.borer.InputReader.apply(Reader.scala:63)
    at io.bullet.borer.DecoderFromApply.$anonfun$from$2(DecoderFromApply.scala:18)
    at io.bullet.borer.derivation.MapBasedCodecs$deriveDecoder$.fillArgsAndConstruct$1(MapBasedCodecs.scala:207)
    at io.bullet.borer.derivation.MapBasedCodecs$deriveDecoder$.$anonfun$combine$2(MapBasedCodecs.scala:215)
    at io.bullet.borer.DecodingSetup$Impl.decodeFrom(DecodingSetup.scala:160)
    at io.bullet.borer.DecodingSetup$Impl.value(DecodingSetup.scala:98)
    ... 51 more
sirthias commented 5 years ago

Well, this looks like a relatively simple initialization order problem to me. Essentially this is the setup:

class Foo {
  implicit val charCodec: Codec[Char] = ...
  implicit val fooCodec = {
    <expr using charCodec>
  }
}

This works as long as charCodec is initialized before fooCodec but will throw an NPE if you move it below the fooCodec definition. This is not really a BORER issue but an artifact of how Scala works.

Do you have an idea on what we might improve here?

plokhotnyuk commented 5 years ago

Can it be checked in a compile time (with a compilation error throwing) instead of failing differently in the run-time?

As an example, if you remove the lazy keyword in this line you will get the following compilation error:

Error:(1362, 37) forward reference extends over definition of value intCodec
      verifySerDeser(make[List[Int]](CodecMakerConfig()), List(1, 2), "[\"1\",\"2\"]")
sirthias commented 5 years ago

Closing as out-of-scope. Initialization order issues are inherent to Scala and cannot really be solved on the level of this library.