micronaut-projects / micronaut-serialization

Build Time Serialization APIs for Micronaut
Apache License 2.0
26 stars 19 forks source link

@JsonPropery ignored when using global naming strategy #883

Closed Myshkouski closed 2 months ago

Myshkouski commented 2 months ago

Expected Behavior

Property names from @JsonProperty or @JsonbProperty should take precedence over property name, translated by PropertyNamingStrategy.

Let's take a look at the following case:

  1. DTO class:

    // doc-examples/example-kotlin/src/test/kotlin/example/BookTest.kt
    @Serdeable
    data class Book (
    val title: String,
    @JsonProperty("qty") val quantity: Int
    )
  2. Define app configuration:

    # doc-examples/example-kotlin/src/test/resources/application.properties
    micronaut.serde.property-naming-strategy=SNAKE_CASE
  3. Run test:

// doc-examples/example-kotlin/src/test/kotlin/example/BookTest.kt:12
val result = objectMapper.writeValueAsString(Book("The Stand", 50))

result should be {"title":"The Stand","qty":50} with explicit property name qty, set by @JsonProperty.

Actual Behaviour

Actual result is {"title":"The Stand","quantity":50}. Annotation property names are ignored.

Steps To Reproduce

  1. Copy this fork:
    git clone https://github.com/Myshkouski/micronaut-serialization
  2. Checkout this revision from fork:
    git checkout 425696c939401fc74b1ccf89df120793f37e85fb
  3. Run tests from io.micronaut.serde.jackson.GlobalPropertyStrategySpec:
    ./gradlew :micronaut-serde-jackson:test --tests "io.micronaut.serde.jackson.GlobalPropertyStrategySpec"
  4. 'test global property strategy SNAKE_CASE with JsonProperty annotated fields' will fail:

    Test Run :micronaut-serde-jackson:test > Partition 1 in session 1 on localhost-executor-1 > GlobalPropertyStrategySpec test global property strategy SNAKE_CASE with JsonProperty annotated fields FAILED
    
    Condition not satisfied:
    
    val == '{"explicit_foo_bar_prop_name":"hello","abc_xyz":123}'
    |   |
    |   false
    |   19 differences (63% similarity)
    |   {"(---------)foo_bar(----------)":"hello","abc_xyz":123}
    |   {"(explicit_)foo_bar(_prop_name)":"hello","abc_xyz":123}
    {"foo_bar":"hello","abc_xyz":123}
      at io.micronaut.serde.jackson.GlobalPropertyStrategySpec.test global property strategy SNAKE_CASE with JsonProperty annotated fields(GlobalPropertyStrategySpec.groovy:59)

Environment Information

No response

Example Application

https://github.com/Myshkouski/micronaut-serialization/tree/425696c939401fc74b1ccf89df120793f37e85fb

Version

4.5.0