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

Add tests for scala3 enum derivation and fix writer derivation for scala3 enums #224

Merged
merged 2 commits into from
Jul 17, 2023

Conversation

Alexxand
Copy link
Contributor

@Alexxand Alexxand commented May 22, 2023

Add tests and fix bugs for #177
Should be merged after #177 .

Tests for simple and parametrized enums:

  • semiauto derivation
  • semiauto derivation with discriminator
  • auto derivation

Bug:
Behaviour of derived writer before this pr (the same for parametrised enum):
SimpleEnum.ONE -> { "ONE" }
SimpleEnum.ONE (with discriminator) -> { "ONE", "__type": "ONE" }
Behaviour of derived writer after this pr (the same for parametrised enum):
SimpleEnum.ONE -> "ONE"
SimpleEnum.ONE (with discriminator) -> { "__type": "ONE" }

Behaviour of reader derivation was not changed.
Derived reader works like "ONE" -> SimpleEnum.ONE (the same for parametrised enum)

However if you will try to use JsonWriter.obj for enum values, but not for the enum itself, like:

implicit val oneWriter: JsonObjectWriter[SimpleEnum.ONE.type] = JsonWriter.obj[SimpleEnum.ONE.type].addField("type")(_ => "ONE")  
implicit val twoWriter: JsonObjectWriter[SimpleEnum.TWO.type] = JsonWriter.obj[SimpleEnum.TWO.type].addField("type")(_ => "TWO")  
implicit val simpleEnumWriter: JsonWriter[SimpleEnum] = jsonWriter[SimpleEnum]

in version after this pr it will produce "type": "ONE" or "type": "TWO" without object start and object end
(in version before this pr it would produce {"type": "ONE"} or {"type": "TWO"})

But you can use JsonWriter.obj for just SimpleEnum, not for its values:

implicit val simpleEnumWriter: JsonWriter[SimpleEnum] = JsonWriter[SimpleEnum].addField("type")(_.toString)

and get the correct behaviour of such writer: SimpleEnum.ONE -> {"type": "ONE"}

Aleksandr Petukhov added 2 commits July 13, 2023 21:04
Enums should be converted into StringValueNode without object start and object end,
but when discriminator is used, it should be converted into object with discriminator
@Alexxand Alexxand changed the title Add tests for scala3 enum derivation Add tests for scala3 enum derivation and fix writer derivation for scala3 enums Jul 14, 2023
@REDNBLACK REDNBLACK merged commit 3059341 into tethys-json:master Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants