Video in progress
Why can adding a new value to an enum in an event or response DTO break old consumers even though it looks backward compatible?
By default Jackson throws InvalidFormatException when it sees an unknown enum value. When the producer starts sending status: "ON_HOLD", the old consumer can't parse the message: in Kafka that is a poison message, in HTTP a 500. Consumers must be ready for unknown values, and they must be upgraded first.
📖 Answer
Many people think "adding a field is safe, and a new enum value is just an addition too". But a field and a value are different: a Boot-configured Jackson ignores an unknown field, but not an unknown enum value.
enum OrderStatus { NEW, PAID, SHIPPED }
// the new producer version sent ON_HOLD
// old consumer: InvalidFormatException: Cannot deserialize value of type OrderStatus from String "ON_HOLD"
Protection on the consumer side:
enum OrderStatus {
NEW, PAID, SHIPPED,
@JsonEnumDefaultValue UNKNOWN
}
spring:
jackson:
deserialization:
read-unknown-enum-values-using-default-value: true
Now an unknown value arrives as UNKNOWN. But this is half a solution: what does the code do when it sees UNKNOWN? If a switch has default -> throw, the problem has just moved elsewhere. Every consumer has to decide on safe behavior for an "unknown state": skip it, log it, or store it to process later.
Another option is READ_UNKNOWN_ENUM_VALUES_AS_NULL, but the null later shows up as a NullPointerException.
Deploy order: first all consumers move to a version that understands the new value (or is ready for UNKNOWN), then the producer starts sending the new value. In Kafka this matters even more: the message stays in the topic and the old consumer fails on it every time it reads it.
Avro is similar: when a new symbol is added to an enum and the enum in the consumer's schema has no default, reading fails.
Sign up to mark studied questions and save your progress.