FasterXML / jackson-databind

General data-binding package for Jackson (2.x): works on streaming API (core) implementation(s)
Apache License 2.0
3.53k stars 1.38k forks source link

Enum naming strategy should allow different naming conventions used in source code #4675

Open hajdamak opened 3 months ago

hajdamak commented 3 months ago

Is your feature request related to a problem? Please describe.

Currently CamelCaseStrategy in EnumNamingStrategies assumes that enum entires in source code are named using UPPER_SNAKE_CASE convention, for example: enum Size { VERY_BIG }. However in Kotlin quite often UpperCamelCase is used, for example enum class Size { VeryBig }. CamelCaseStrategy in EnumNamingStrategies does not work with such a naming.

Describe the solution you'd like

Strategies in EnumNamingStrategies should work with enum entries written in code using different naming conventions like UpperCamelCase .

Usage example

No response

Additional context

No response

cowtowncoder commented 2 months ago

I am not 100% sure if this can be done reliably, but if so, sounds like a reasonable idea.

JooHyukKim commented 2 months ago

I believe naming conventions wrt Enum is somewhat controversial. Here is reference to discussion in Kotlin.

As far as I know it is conventional to use

So it might be safer off to provide extension points instead of declaring what Enum naming would be. But as @cowtowncoder mentioned, idk how it could be reliable, technically, since now we would have naming strategies two-ways, in-and-out.

yihtserns commented 2 months ago

Sounds like something that https://github.com/FasterXML/jackson-module-kotlin can/should(?) support out-of-the-box:

internal class KotlinAnnotationIntrospector(...) {
   ...
   override fun findEnumNamingStrategy(config: MapperConfig<*>, ac: AnnotatedClass): Any? {
      return PascalCaseOrCamelCaseStrategy()
   }
}
cowtowncoder commented 2 months ago

@yihtserns Perhaps, although if so, should only apply to Kotlin Enum types? And be configurable to allow disabling such logic.

yihtserns commented 2 months ago

...should only apply to Kotlin Enum types?

I think so far nobody from Java side has ever requested such thing, right? The reason this issue was created is because only Kotlin side has such "double standard". 🤐

EDIT: Oh you mean findEnumNamingStrategy should only return PascalCaseOrCamelCaseStrategy if the AnnotatedClass method arg is an enum class generated from Kotlin source code. UPDATE: E.g.:

internal class KotlinAnnotationIntrospector(...) {
   ...
   override fun findEnumNamingStrategy(config: MapperConfig<*>, ac: AnnotatedClass): Any? {
      if (ac.annotated.isKotlinClass()) {
         return PascalCaseOrCamelCaseStrategy()
      }
      return null
   }
}
cowtowncoder commented 2 months ago

Right, if there are reliable means to detect that information. Similar to how Kotlin special sauce avoided (I think?) for POJOs.