Skip to content

Fix #668: JSON codec built from auto-derived schema fails for enumerat#1050

Open
JoshKappler wants to merge 1 commit intozio:mainfrom
JoshKappler:algora/fix-668-json-codec-built-from-auto-derived-schem
Open

Fix #668: JSON codec built from auto-derived schema fails for enumerat#1050
JoshKappler wants to merge 1 commit intozio:mainfrom
JoshKappler:algora/fix-668-json-codec-built-from-auto-derived-schem

Conversation

@JoshKappler
Copy link

Summary

The JSON codec's enumDecoder used isInstanceOf[CaseClass0[_]] checks with unsafe asInstanceOf casts to determine simple enum handling, which was misaligned with the encoder's simpleEnum annotation check. This caused a ClassCastException when an enumeration contained intermediate sealed traits (producing Schema.Enum schemas instead of Schema.CaseClass0). The fix aligns the decoder with the encoder by using the simpleEnum annotation check and replaces unsafe casts with the existing type-safe constructEnumCase helper.

Changes

  • zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala: Changed enumDecoder to check the simpleEnum annotation (matching enumEncoder behavior) instead of checking whether all cases are CaseClass0 instances. Replaced unsafe asInstanceOf[Schema.CaseClass0[...]] casts with calls to the existing constructEnumCase helper, which safely handles both CaseClass0 and Schema.Enum cases.

Testing

  • Added JsonCodecSpec668 with 10 test cases covering enumerations with intermediate types — all pass on Scala 2.13 and Scala 3.3.7.
  • Full zioSchemaJsonJVM/test suite passes (298 tests on Scala 2.13, 304 on Scala 3).
  • Full zioSchemaDerivationJVM/test suite passes (91 on Scala 2.13, 101 on Scala 3).

/claim #668

…s for enumeration with intermediate type
@CLAassistant
Copy link

CLAassistant commented Mar 18, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


JoshKappler seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@JoshKappler
Copy link
Author

This is an automated CLA (Contributor License Agreement) check from the CLA assistant bot, not a code review comment. It's asking you to sign the CLA before the contribution can be accepted.

To resolve this:

  1. Visit the link: https://cla-assistant.io/zio/zio-schema?pullRequest=1050
  2. Sign the CLA with your GitHub account
  3. If you've already signed, click the "recheck" link to update the status

This is a standard requirement for most open source projects — no code changes needed. Would you like me to help with anything else on this PR?

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