Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coercion shouldn't be necessary for enums specifying an empty string #4896

Open
1 task done
joaocanaverde-blue opened this issue Jan 10, 2025 · 0 comments
Open
1 task done
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@joaocanaverde-blue
Copy link

joaocanaverde-blue commented Jan 10, 2025

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

Trying to deserialise:

{ "example": "" }

Results in:

Cannot coerce empty String ("") to `YesOrNoOrEmpty` value (but could if coercion was enabled using `CoercionConfig`)

Despite an empty string ("") being listed as a value in the enum.

enum class YesOrNoOrEmpty(val value: kotlin.String) {
    @JsonProperty(value = "")
    EMPTY(""),

    @JsonProperty(value = "yes")
    YES("yes"),

    @JsonProperty(value = "no")
    NO("no");
}

I'm trying to deserialise historical values, so this is a value I have to support. Ideally I'd still like to deserialise to an enum rather than just into a string, and would like to avoid coercion if possible.

Given that I'm specifying the empty string explicitly with @JsonProperty(value = ""), I'm assuming encountering this exception is a bug rather than intended behaviour?

Version Information

2.18.2

Reproduction

Read:

{ "example": "" }

into an Example object like:

data class Example(
    val example: YesOrNo
)

enum class YesOrNo(val value: kotlin.String) {
    @JsonProperty(value = "")
    EMPTY(""),

    @JsonProperty(value = "yes")
    YES("yes"),

    @JsonProperty(value = "no")
    NO("no");
}

Expected behavior

Deserialisation works.

Additional context

A workaround does exist (as the exception message suggests):

import com.fasterxml.jackson.databind.cfg.CoercionAction
import com.fasterxml.jackson.databind.cfg.CoercionInputShape
import com.fasterxml.jackson.databind.type.LogicalType
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder

@Configuration
class JacksonConfig {
    @Bean
    fun jsonCustomizer(): Jackson2ObjectMapperBuilderCustomizer {
        return Jackson2ObjectMapperBuilderCustomizer { builder: Jackson2ObjectMapperBuilder ->
            builder.postConfigurer { objectMapper ->
                objectMapper.coercionConfigFor(LogicalType.Enum)
                    .setCoercion(CoercionInputShape.EmptyString, CoercionAction.TryConvert)
            }

        }
    }
}
@joaocanaverde-blue joaocanaverde-blue added the to-evaluate Issue that has been received but not yet evaluated label Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

1 participant