Skip to content

Commit 9b7e992

Browse files
authored
fix test case where scala user wants alphabetic sorting of properties (#773)
1 parent 09f6d07 commit 9b7e992

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/main/scala/tools/jackson/module/scala/introspect/ScalaAnnotationIntrospectorModule.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ScalaAnnotationIntrospectorInstance(scalaAnnotationIntrospectorModule: Sca
4242
override def findSerializationSortAlphabetically(config: MapperConfig[_], ann: Annotated): java.lang.Boolean = {
4343
ann match {
4444
case ac: AnnotatedClass if scalaAnnotationIntrospectorModule.isMaybeScalaBeanType(ac.getAnnotated) =>
45-
java.lang.Boolean.FALSE
45+
!config.isEnabled(MapperFeature.SORT_CREATOR_PROPERTIES_FIRST)
4646
case _ => None.orNull
4747
}
4848
}

src/test/scala/tools/jackson/module/scala/ser/CaseClassSerializerTest.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package tools.jackson.module.scala.ser
33
import com.fasterxml.jackson.annotation.JsonProperty.Access
44
import com.fasterxml.jackson.annotation._
55
import tools.jackson.databind.cfg.MutableConfigOverride
6-
import tools.jackson.databind.{ObjectMapper, PropertyNamingStrategies}
6+
import tools.jackson.databind.{MapperFeature, ObjectMapper, PropertyNamingStrategies}
77
import tools.jackson.module.scala.DefaultScalaModule
88

99
import scala.beans.BeanProperty
@@ -66,6 +66,7 @@ case class ClassWithOnlyUnitField(field: Unit)
6666

6767
object CaseClassSerializerTest {
6868
case class BigDecimalHolder(bigDecimal: BigDecimal)
69+
case class ClassWithUnorderedFields(f3: Int = 3, f2: Int = 2, f0: Int = 0, f1: Int = 1)
6970
}
7071

7172
class CaseClassSerializerTest extends SerializerTest {
@@ -229,4 +230,12 @@ class CaseClassSerializerTest extends SerializerTest {
229230
(c: MutableConfigOverride) => c.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.STRING)))
230231
serialize(BigDecimalHolder(BigDecimal("123.456")), builder.build()) shouldEqual """{"bigDecimal":"123.456"}"""
231232
}
233+
234+
it should "sort properties of the case class" in {
235+
val mapper = newBuilder
236+
.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
237+
.disable(MapperFeature.SORT_CREATOR_PROPERTIES_FIRST)
238+
.build()
239+
serialize(ClassWithUnorderedFields(), mapper) shouldEqual """{"f0":0,"f1":1,"f2":2,"f3":3}"""
240+
}
232241
}

0 commit comments

Comments
 (0)