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

All modules: importing derivation.instances.given can cause an issue as it overrides all type-classes defined in the companion objects #163

Closed
4 tasks done
kevin-lee opened this issue Dec 23, 2023 · 0 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@kevin-lee
Copy link
Owner

kevin-lee commented Dec 23, 2023

Summary

All modules: importing derivation.instances.given can cause an issue as it overrides all type-classes defined in the companion objects

Version

0.5.0 but all other versions with modules containg derivation.instances

Scala Version

3

Java Version

n/a

Description

Given

import cats.*
import io.circe.*

import refined4s.modules.circe.derivation.instances.given
import java.time.Instant

final case class Response(created: Response.Created) derives Codec.AsObject

object Response {
  type Created = Created.Type
  object Created extends Newtype[Instant] {

    inline given createdEncoder: Encoder[Created] =
      Encoder[Long].contramap(_.value.getEpochSecond)

    inline given createdDecoder: Decoder[Created] =
      Decoder[Long].map(Instant.ofEpochSecond).map(Created(_))
  }
}

When encoding the Response, it generates JSON like

{
  "created": "2023-12-23T12:07:03Z"
}

but the expected one should be

{
  "created": 1703333223
}

How to Reproduce

Check out the description

Possible Cause

It looks like the Encoder and Decoder from import refined4s.modules.circe.derivation.instances.given have higher precedence than the ones defined in the companion object.

Possible Solution

Since the following import is required for the pre-defined types (e.g. NonEmptyString, refined numeric, network types, etc.), this is a very common issue.

import refined4s.modules.circe.derivation.instances.given

So, a possible solution might be to provide explicit type-class instances for the pre-defined types and separate them from the generic one (refined4s.modules.circe.derivation.instances).


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant