Skip to content

Commit d2f0eb1

Browse files
authored
support workaround to allow alphabetic ordering (#775)
1 parent f58f76e commit d2f0eb1

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/main/scala/com/fasterxml/jackson/module/scala/introspect/ScalaAnnotationIntrospectorModule.scala

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ object ScalaAnnotationIntrospector extends NopAnnotationIntrospector with ValueI
3939
// For Scala, we want to use the declared order of the fields in the class
4040
override def findSerializationSortAlphabetically(ann: Annotated): java.lang.Boolean = {
4141
ann match {
42-
case ac: AnnotatedClass if isMaybeScalaBeanType(ac.getAnnotated) =>
42+
case ac: AnnotatedClass if
43+
ScalaAnnotationIntrospectorModule.isCaseClassDefaultSerializationOrderBasedOnDeclaredParamOrder &&
44+
isMaybeScalaBeanType(ac.getAnnotated) =>
4345
val annotation = _findAnnotation(ac, classOf[JsonPropertyOrder])
4446
if (annotation != null) {
4547
// delegate to JacksonAnnotationIntrospector
@@ -468,7 +470,27 @@ trait ScalaAnnotationIntrospectorModule extends JacksonModule {
468470
def shouldSupportScala3Classes(): Boolean = _shouldSupportScala3Classes
469471
}
470472

471-
object ScalaAnnotationIntrospectorModule extends ScalaAnnotationIntrospectorModule
473+
object ScalaAnnotationIntrospectorModule extends ScalaAnnotationIntrospectorModule {
474+
private var caseClassDefaultOrderBasedOnDeclaredParamOrder = true
475+
476+
/**
477+
* @return Whether to default the serialization order of Case Class params to the defined order in the class.
478+
* This should be set to false if you want to enable <code>MapperFeature.SORT_PROPERTIES_ALPHABETICALLY</code>.
479+
* This is not needed in Jackson 3.
480+
* @since 2.20.1
481+
*/
482+
def isCaseClassDefaultSerializationOrderBasedOnDeclaredParamOrder: Boolean =
483+
caseClassDefaultOrderBasedOnDeclaredParamOrder
484+
485+
/**
486+
* @param flag Whether to default the serialization order of Case Class params to the defined order in the class.
487+
* This should be set to false if you want to enable <code>MapperFeature.SORT_PROPERTIES_ALPHABETICALLY</code>.
488+
* This is not needed in Jackson 3.
489+
* @since 2.20.1
490+
*/
491+
def setCaseClassDefaultSerializationOrderBasedOnDeclaredParamOrder(flag: Boolean): Unit =
492+
this.caseClassDefaultOrderBasedOnDeclaredParamOrder = flag
493+
}
472494

473495
private case class WrappedCreatorProperty(creatorProperty: CreatorProperty, refHolder: ClassHolder)
474496
extends CreatorProperty(creatorProperty, creatorProperty.getFullName) {

src/test/scala/com/fasterxml/jackson/module/scala/ser/CaseClassSerializerTest.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty.Access
44
import com.fasterxml.jackson.annotation._
55
import com.fasterxml.jackson.databind.{MapperFeature, ObjectMapper, PropertyNamingStrategies}
66
import com.fasterxml.jackson.module.scala.DefaultScalaModule
7+
import com.fasterxml.jackson.module.scala.introspect.ScalaAnnotationIntrospectorModule
78

89
import scala.beans.BeanProperty
910

@@ -230,12 +231,16 @@ class CaseClassSerializerTest extends SerializerTest {
230231
}
231232

232233
// https://github.com/FasterXML/jackson-module-scala/issues/772
233-
it should "sort properties of the case class" ignore {
234+
it should "sort properties of the case class" in {
234235
val mapper = newBuilder
235236
.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
236-
.disable(MapperFeature.SORT_CREATOR_PROPERTIES_FIRST)
237237
.build()
238-
serialize(ClassWithUnorderedFields(), mapper) shouldEqual """{"f0":0,"f1":1,"f2":2,"f3":3}"""
238+
ScalaAnnotationIntrospectorModule.setCaseClassDefaultSerializationOrderBasedOnDeclaredParamOrder(false)
239+
try {
240+
serialize(ClassWithUnorderedFields(), mapper) shouldEqual """{"f0":0,"f1":1,"f2":2,"f3":3}"""
241+
} finally {
242+
ScalaAnnotationIntrospectorModule.setCaseClassDefaultSerializationOrderBasedOnDeclaredParamOrder(true)
243+
}
239244
}
240245

241246
it should "respect JsonPropertyOrder" in {

0 commit comments

Comments
 (0)