Skip to content

Set preferFlat = false for circe and spray-json Scala 2 macros - #534

Merged
luksow merged 1 commit into
masterfrom
fix/prefer-flat-default
Mar 28, 2026
Merged

luksow merged 1 commit into
masterfrom
fix/prefer-flat-default

Conversation

@luksow

@luksow luksow commented Mar 28, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Set preferFlat = false in KebsCirceMacros and KebsSprayMacros (Scala 2) so that 1-element case classes serialize as objects ({"i": 1}) by default
  • With CaseClass1ToValueClass mixed in, they still serialize flat (1) — matching the Scala 3 behavior which has no preferFlat mechanism at all
  • Added test cases for both circe and spray-json verifying non-flat format without CaseClass1ToValueClass

Context

In kebs 2.x, flattening 1-element case classes was made optional via CaseClass1ToValueClass. However, the Scala 2 macros had preferFlat hardcoded to true, which caused the macro to abort for 1-element case classes and effectively required CaseClass1ToValueClass to be mixed in — contradicting the opt-in design.

Test plan

  • New CirceFormatNoFlatTests — verifies 1-element case classes serialize as objects without CaseClass1ToValueClass
  • New SprayJsonFormatNoFlatTests — same for spray-json
  • Existing CirceFormatTests (with CaseClass1ToValueClass) still pass — flat format preserved
  • Existing SprayJsonFormatTests (with CaseClass1ToValueClass) still pass — flat format preserved
  • Full circeSupport/test (76 tests) and sprayJsonSupport/test (90 tests) pass

@luksow
luksow force-pushed the fix/prefer-flat-default branch from 47b6aca to d42aaab Compare March 28, 2026 23:14
In kebs 2.x, flattening 1-element case classes was made optional via
CaseClass1ToValueClass. However, the Scala 2 macros for circe and
spray-json had preferFlat hardcoded to true, which caused the macro to
always abort for 1-element case classes and effectively required
CaseClass1ToValueClass to be mixed in.

Replace the static preferFlat flag with a dynamic check: the macro now
detects whether ValueClassLike is available (i.e., CaseClass1ToValueClass
is mixed in) and only aborts in that case. This aligns Scala 2 behavior
with Scala 3 which has no preferFlat mechanism at all.

Without CaseClass1ToValueClass: case class C(i: Int) → {"i": 1}
With CaseClass1ToValueClass: case class C(i: Int) → 1
@luksow
luksow force-pushed the fix/prefer-flat-default branch from d42aaab to c2ea0a9 Compare March 28, 2026 23:22
@luksow
luksow merged commit 09d793a into master Mar 28, 2026
16 checks passed
@luksow
luksow deleted the fix/prefer-flat-default branch March 28, 2026 23:29
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 this pull request may close these issues.

1 participant