Skip to content

Commit

Permalink
util-jackson: Use PropertyNamingStrategies for static instances
Browse files Browse the repository at this point in the history
Problem

In release 2.12,  Jackson introduced `PropertyNamingStrategies` as a work-around
for [#2715](FasterXML/jackson-databind#2715) which deprecated using the like `PropertyNamingStrategy` static fields.

Solution

Move to using the equivalent static fields in `PropertyNamingStrategies`.

Result

Less deprection tech-debt.

Differential Revision: https://phabricator.twitter.biz/D838232
  • Loading branch information
cacoco authored and jenkins committed Feb 24, 2022
1 parent ef6e3fb commit 1a0d87c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object ScalaObjectMapper {

/** The default [[PropertyNamingStrategy]] for a [[ScalaObjectMapper]] */
private[twitter] val DefaultPropertyNamingStrategy: PropertyNamingStrategy =
PropertyNamingStrategy.SNAKE_CASE
PropertyNamingStrategies.SNAKE_CASE

/** The default [[JsonInclude.Include]] for serialization for a [[ScalaObjectMapper]] */
private[twitter] val DefaultSerializationInclude: JsonInclude.Include =
Expand Down Expand Up @@ -125,29 +125,29 @@ object ScalaObjectMapper {

/**
* Utility to create a new [[ScalaObjectMapper]] explicitly configured with
* [[PropertyNamingStrategy.LOWER_CAMEL_CASE]] as a `PropertyNamingStrategy` wrapping the
* [[PropertyNamingStrategies.LOWER_CAMEL_CASE]] as a `PropertyNamingStrategy` wrapping the
* given [[JacksonScalaObjectMapperType]].
*
* @note the `underlying` mapper is copied (not mutated) to produce the new [[ScalaObjectMapper]]
* with a [[PropertyNamingStrategy.LOWER_CAMEL_CASE]] PropertyNamingStrategy.
* with a [[PropertyNamingStrategies.LOWER_CAMEL_CASE]] PropertyNamingStrategy.
*/
def camelCaseObjectMapper(underlying: JacksonScalaObjectMapperType): ScalaObjectMapper = {
val objectMapperCopy = ObjectMapperCopier.copy(underlying)
objectMapperCopy.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE)
objectMapperCopy.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CAMEL_CASE)
new ScalaObjectMapper(objectMapperCopy)
}

/**
* Utility to create a new [[ScalaObjectMapper]] explicitly configured with
* [[PropertyNamingStrategy.SNAKE_CASE]] as a `PropertyNamingStrategy` wrapping the
* [[PropertyNamingStrategies.SNAKE_CASE]] as a `PropertyNamingStrategy` wrapping the
* given [[JacksonScalaObjectMapperType]].
*
* @note the `underlying` mapper is copied (not mutated) to produce the new [[ScalaObjectMapper]]
* with a [[PropertyNamingStrategy.SNAKE_CASE]] PropertyNamingStrategy.
* with a [[PropertyNamingStrategies.SNAKE_CASE]] PropertyNamingStrategy.
*/
def snakeCaseObjectMapper(underlying: JacksonScalaObjectMapperType): ScalaObjectMapper = {
val objectMapperCopy = ObjectMapperCopier.copy(underlying)
objectMapperCopy.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
objectMapperCopy.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
new ScalaObjectMapper(objectMapperCopy)
}

Expand All @@ -158,7 +158,7 @@ object ScalaObjectMapper {
* For example,
* {{{
* ScalaObjectMapper.builder
* .withPropertyNamingStrategy(new PropertyNamingStrategy.UpperCamelCaseStrategy)
* .withPropertyNamingStrategy(new PropertyNamingStrategies.UpperCamelCaseStrategy)
* .withNumbersAsStrings(true)
* .withAdditionalJacksonModules(...)
* .objectMapper
Expand All @@ -169,7 +169,7 @@ object ScalaObjectMapper {
* {{{
* val builder =
* ScalaObjectMapper.builder
* .withPropertyNamingStrategy(new PropertyNamingStrategy.UpperCamelCaseStrategy)
* .withPropertyNamingStrategy(new PropertyNamingStrategies.UpperCamelCaseStrategy)
* .withNumbersAsStrings(true)
* .withAdditionalJacksonModules(...)
*
Expand All @@ -187,7 +187,7 @@ object ScalaObjectMapper {
* For example,
* {{{
* ScalaObjectMapper.builder
* .withPropertyNamingStrategy(new PropertyNamingStrategy.UpperCamelCaseStrategy)
* .withPropertyNamingStrategy(new PropertyNamingStrategies.UpperCamelCaseStrategy)
* .withNumbersAsStrings(true)
* .withAdditionalJacksonModules(...)
* .objectMapper
Expand All @@ -198,7 +198,7 @@ object ScalaObjectMapper {
* {{{
* val builder =
* ScalaObjectMapper.builder
* .withPropertyNamingStrategy(new PropertyNamingStrategy.UpperCamelCaseStrategy)
* .withPropertyNamingStrategy(new PropertyNamingStrategies.UpperCamelCaseStrategy)
* .withNumbersAsStrings(true)
* .withAdditionalJacksonModules(...)
*
Expand Down Expand Up @@ -240,14 +240,14 @@ object ScalaObjectMapper {

/**
* Creates a new [[ScalaObjectMapper]] explicitly configured with
* [[PropertyNamingStrategy.LOWER_CAMEL_CASE]] as a `PropertyNamingStrategy`.
* [[PropertyNamingStrategies.LOWER_CAMEL_CASE]] as a `PropertyNamingStrategy`.
*/
final def camelCaseObjectMapper: ScalaObjectMapper =
ScalaObjectMapper.camelCaseObjectMapper(jacksonScalaObjectMapper)

/**
* Creates a new [[ScalaObjectMapper]] explicitly configured with
* [[PropertyNamingStrategy.SNAKE_CASE]] as a `PropertyNamingStrategy`.
* [[PropertyNamingStrategies.SNAKE_CASE]] as a `PropertyNamingStrategy`.
*/
final def snakeCaseObjectMapper: ScalaObjectMapper =
ScalaObjectMapper.snakeCaseObjectMapper(jacksonScalaObjectMapper)
Expand All @@ -256,7 +256,7 @@ object ScalaObjectMapper {

/**
* Configure a [[PropertyNamingStrategy]] for this [[Builder]].
* @note the default is [[PropertyNamingStrategy.SNAKE_CASE]]
* @note the default is [[PropertyNamingStrategies.SNAKE_CASE]]
* @see [[ScalaObjectMapper.DefaultPropertyNamingStrategy]]
*/
final def withPropertyNamingStrategy(propertyNamingStrategy: PropertyNamingStrategy): Builder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.JsonMappingException
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.MapperFeature
import com.fasterxml.jackson.databind.PropertyNamingStrategies
import com.fasterxml.jackson.databind.PropertyNamingStrategy
import com.fasterxml.jackson.databind.{ObjectMapper => JacksonObjectMapper}
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
Expand Down Expand Up @@ -384,7 +385,7 @@ class ScalaObjectMapperTest
val jacksonScalaObjectMapper: JacksonScalaObjectMapperType = new JacksonObjectMapper()
with JacksonScalaObjectMapper
jacksonScalaObjectMapper.registerModule(DefaultScalaModule)
jacksonScalaObjectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE)
jacksonScalaObjectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CAMEL_CASE)

// default for ScalaObjectMapper#Builder is snake_case which should not get applied here
// since this method only wraps and does not mutate the underlying mapper
Expand Down Expand Up @@ -454,7 +455,7 @@ class ScalaObjectMapperTest
// test with default scala module
val defaultScalaObjectMapper = new JacksonObjectMapper with JacksonScalaObjectMapper
defaultScalaObjectMapper.registerModule(DefaultScalaModule)
defaultScalaObjectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
defaultScalaObjectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
deserialize[BasicDate](defaultScalaObjectMapper, expectedStr) should equal(expectedInstance)

// case insensitive mapper feature does not pertain to scala enumerations (only Java enums)
Expand Down Expand Up @@ -566,7 +567,7 @@ class ScalaObjectMapperTest
// test with default scala module
val defaultScalaObjectMapper = new JacksonObjectMapper with JacksonScalaObjectMapper
defaultScalaObjectMapper.registerModule(DefaultScalaModule)
defaultScalaObjectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
defaultScalaObjectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
deserialize[Page[Person]](defaultScalaObjectMapper, input) should equal(result)

// test with ScalaObjectMapper
Expand Down Expand Up @@ -934,7 +935,7 @@ class ScalaObjectMapperTest
) {
val objMapper = new JacksonObjectMapper with JacksonScalaObjectMapper
objMapper.registerModule(DefaultScalaModule)
objMapper.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy)
objMapper.setPropertyNamingStrategy(new PropertyNamingStrategies.SnakeCaseStrategy)

val response = objMapper.writeValueAsString(NamingStrategyJsonProperty("abc"))
response should equal("""{"long_field_name":"abc"}""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@ import com.fasterxml.jackson.annotation._
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.`type`.TypeReference
import com.fasterxml.jackson.databind._
import com.fasterxml.jackson.databind.annotation.{JsonDeserialize, JsonNaming}
import com.fasterxml.jackson.databind.deser.std.NumberDeserializers.{
BigDecimalDeserializer,
NumberDeserializer
}
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonNaming
import com.fasterxml.jackson.databind.deser.std.NumberDeserializers.BigDecimalDeserializer
import com.fasterxml.jackson.databind.deser.std.NumberDeserializers.NumberDeserializer
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind.node.ValueNode
import com.fasterxml.jackson.module.scala.JsonScalaEnumeration
import com.twitter.util.{Time, WrappedValue}
import com.twitter.util.Time
import com.twitter.util.WrappedValue
import com.twitter.util.jackson.caseclass.SerdeLogging
import com.twitter.util.validation.MethodValidation
import com.twitter.util.validation.constraints.{OneOf, UUID}
import com.twitter.util.validation.constraints.OneOf
import com.twitter.util.validation.constraints.UUID
import com.twitter.util.validation.engine.MethodValidationResult
import com.twitter.{util => ctu}
import jakarta.validation.Payload
import jakarta.validation.constraints._
import java.time.format.DateTimeFormatter
import java.time.temporal.Temporal
import java.time.{LocalDate, LocalDateTime}
import java.time.LocalDate
import java.time.LocalDateTime
import scala.math.BigDecimal.RoundingMode

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
Expand Down Expand Up @@ -829,10 +831,10 @@ case class ItemSomeViews(

case class ItemNoDefaultForView(@JsonView(Array(classOf[Views.Public])) name: String)

@JsonNaming(classOf[PropertyNamingStrategy.KebabCaseStrategy])
@JsonNaming(classOf[PropertyNamingStrategies.KebabCaseStrategy])
case class CaseClassWithKebabCase(pleaseUseKebabCase: Boolean)

@JsonNaming(classOf[PropertyNamingStrategy.KebabCaseStrategy])
@JsonNaming(classOf[PropertyNamingStrategies.KebabCaseStrategy])
trait KebabCaseMixin

case class CaseClassShouldUseKebabCaseFromMixin(willThisGetTheRightCasing: Boolean)
Expand Down

0 comments on commit 1a0d87c

Please sign in to comment.