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

NullPointerException in the 0.7.7 version of zio-json-golden #1252

Closed
agilesteel opened this issue Jan 26, 2025 · 4 comments · Fixed by #1253
Closed

NullPointerException in the 0.7.7 version of zio-json-golden #1252

agilesteel opened this issue Jan 26, 2025 · 4 comments · Fixed by #1253

Comments

@agilesteel
Copy link

agilesteel commented Jan 26, 2025

https://github.com/zio/zio-json/blob/v0.7.7/zio-json-golden/src/main/scala/zio/json/golden/filehelpers.scala#L18 this line was changed in 0.7.7 and now it throws a NullPointerException for the example from the readme

java.lang.NullPointerException: Cannot invoke "java.net.URL.toURI()" because the return value of "java.lang.Class.getResource(String)" is null
        at zio.json.golden.filehelpers$.createGoldenDirectory(filehelpers.scala:18)

It's all good in 0.7.6 but it's broken in 0.7.7.

@plokhotnyuk
Copy link
Contributor

@agilesteel Could you please share your steps to reproduce, including environment setup (OS, JDK, etc.) 🙏

@erikvanoosten
Copy link
Contributor

In 0.7.6: new File(getClass.getResource("/").toURI)
https://github.com/zio/zio-json/blob/v0.7.6/zio-json-golden/src/main/scala/zio/json/golden/filehelpers.scala#L18-L28

In 0.7.7: new File(getClass.getResource(".").toURI)
https://github.com/zio/zio-json/blob/v0.7.7/zio-json-golden/src/main/scala/zio/json/golden/filehelpers.scala#L16-L22

It seems that / and . do not resolve to the same directory.

The change was part of this PR: #1249

@agilesteel
Copy link
Author

It's a hello-world project with the example from the readme of zio-json-golden:

❯ tree
.
├── build.sbt
├── project
│   └── build.properties
└── src
    └── test
        └── scala
            └── EncodeDecodeSpec.scala

5 directories, 3 files

project/build.properties

sbt.version=1.10.7

build.sbt

scalaVersion := "2.13.16"
libraryDependencies += "dev.zio" %% "zio-json-golden" % "0.7.7"

src/test/scala/EncodeDecodeSpec.scala

import zio.json._
import zio.json.golden._
import zio.test._
import zio.test.magnolia.DeriveGen

object EncodeDecodeSpec extends ZIOSpecDefault {
  case class Banana(curvature: Double)
  object Banana {
    implicit val codec: JsonCodec[Banana] = DeriveJsonCodec.gen[Banana]
  }

  def spec = suite("EncodeDecodeSpec")(
    goldenTest(DeriveGen[Banana])
  )
}

java -version

openjdk version "21.0.2" 2024-01-16
OpenJDK Runtime Environment GraalVM CE 21.0.2+13.1 (build 21.0.2+13-jvmci-23.1-b30)
OpenJDK 64-Bit Server VM GraalVM CE 21.0.2+13.1 (build 21.0.2+13-jvmci-23.1-b30, mixed mode, sharing)

Tested on both Ubuntu 20.04.6 LTS (WSL) and latest MacOS.

@plokhotnyuk
Copy link
Contributor

plokhotnyuk commented Jan 27, 2025

@agilesteel @erikvanoosten Thanks for your feedback!

The release with a fix is already available on the Maven Central: https://github.com/zio/zio-json/releases/tag/v0.7.8

I've reverted my change that lead to NPE.

I added a workaround that handles unexpected "java.lang.IllegalArgumentException: URI is not hierarchical" in unit tests with Scala 2.12.20

I committed a sample project to examples directory.

I will use it for manual testing until find how to automate it.

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 a pull request may close this issue.

3 participants