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

Missing keys exception using Option with generic #545

Closed
mobilemindtec opened this issue Jan 8, 2024 · 3 comments · Fixed by #547
Closed

Missing keys exception using Option with generic #545

mobilemindtec opened this issue Jan 8, 2024 · 3 comments · Fixed by #547

Comments

@mobilemindtec
Copy link

mobilemindtec commented Jan 8, 2024

Hello!

I'm getting a missing keys exception when key is a option generic field. Example:

case class Person(id: Int, name: String = "test") derives ReadWriter

case class Person2(id: Int, name: Option[String] = None) derives ReadWriter

case class ApiResult[T](data: Option[T] = None, @key("total_count") totalCount: Int) derives ReadWriter

println(read[Person]("{\"id\":1}"))  // OK

println(read[Person2]("{\"id\":1}"))  // OK

println(read[ApiResult[Person]]("{\"total_count\": 10}")) // Fail

Is there a way to get around this?

@mobilemindtec mobilemindtec changed the title Missing keys exception using Option Missing keys exception using Option with generic Jan 8, 2024
@lihaoyi
Copy link
Member

lihaoyi commented Jan 11, 2024

Seems like a bug in the Scala 3 macros, the Scala 2 equivalent seems to work

import upickle.implicits.key
case class Person(id: Int, name: String = "test") 
implicit val personRw = upickle.default.macroRW[Person]

case class ApiResult[T](data: Option[T] = None, @key("total_count") totalCount: Int) 
implicit def apiResultRw[T: upickle.default.ReadWriter] = upickle.default.macroRW[ApiResult[T]]
println(upickle.default.read[Person]("{\"id\":1}"))  // OK


println(upickle.default.read[ApiResult[Person]]("{\"total_count\": 10}")) // Works

@lihaoyi
Copy link
Member

lihaoyi commented Jan 11, 2024

The problem appears to be that TypeTree.of[T].symbol for ApiResult[T] is val <none>. Exactly why that is the case is unknown to me

@lihaoyi
Copy link
Member

lihaoyi commented Jan 13, 2024

@mobilemindtec This fix should have gone out in the latest version 3.1.4

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.

2 participants