Skip to content

Sorting of case class properties no longer works as of 2.20 #772

@brharrington

Description

@brharrington

Can be reproduced with the following test case:

diff --git a/src/test/scala/com/fasterxml/jackson/module/scala/ser/CaseClassSerializerTest.scala b/src/test/scala/com/fasterxml/jackson/module/scala/ser/CaseClassSerializerTest.scala
index 6dc1d665..33dddf06 100644
--- a/src/test/scala/com/fasterxml/jackson/module/scala/ser/CaseClassSerializerTest.scala
+++ b/src/test/scala/com/fasterxml/jackson/module/scala/ser/CaseClassSerializerTest.scala
@@ -2,6 +2,9 @@ package com.fasterxml.jackson.module.scala.ser
 
 import com.fasterxml.jackson.annotation.JsonProperty.Access
 import com.fasterxml.jackson.annotation._
+import com.fasterxml.jackson.databind.MapperFeature
 import com.fasterxml.jackson.databind.{ObjectMapper, PropertyNamingStrategies}
 import com.fasterxml.jackson.module.scala.DefaultScalaModule
 
@@ -62,6 +65,8 @@ case class PrivateDefaultFields(
 case class ClassWithUnitField(field: Unit, intField: Int)
 case class ClassWithOnlyUnitField(field: Unit)
 
+case class ClassWithUnorderedFields(f3: Int = 3, f2: Int = 2, f0: Int = 0, f1: Int = 1)
+
 class CaseClassSerializerTest extends SerializerTest {
 
   case class NestedClass(field: String)
@@ -207,4 +212,11 @@ class CaseClassSerializerTest extends SerializerTest {
     serialize(GeneratedDefaultArgumentClass()) shouldEqual "{}"
   }
 
+  it should "sort properties of the case class" in {
+    val mapper = newBuilder
+      .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
+      .build()
+    serialize(ClassWithUnorderedFields(), mapper) shouldEqual """{"f0":0,"f1":1,"f2":2,"f3":3}"""
+  }
+
 }

This test cases passes with 2.19.2 and fails with 2.20.0. I thought it might be related to FasterXML/jackson-databind#3900, but disabling MapperFeature.SORT_CREATOR_PROPERTIES_FIRST does not resolve the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions