From f678140183fafcef8bb0d88f2b84b1d55e35a302 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 3 May 2023 20:34:22 -0700 Subject: [PATCH] Fix #3914 --- release-notes/VERSION-2.x | 1 + .../impl/AsDeductionTypeSerializer.java | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 97802c4a5b..62b485a4f7 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -11,6 +11,7 @@ Project: jackson-databind #3913: Issue with deserialization when there are unexpected properties (due to null `StreamReadConstraints`) (reported by @sbertault) +#3914: Fix TypeId serialization for `JsonTypeInfo.Id.DEDUCTION`, native type ids 2.15.0 (23-Apr-2023) diff --git a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsDeductionTypeSerializer.java b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsDeductionTypeSerializer.java index f23b574aca..7776201235 100644 --- a/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsDeductionTypeSerializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsDeductionTypeSerializer.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo.As; import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.core.type.WritableTypeId; import com.fasterxml.jackson.databind.BeanProperty; @@ -39,9 +40,19 @@ public WritableTypeId writeTypePrefix(JsonGenerator g, // write surrounding Object or Array start/end markers. But // we are not to generate type id to write (compared to base class) - if (idMetadata.valueShape.isStructStart() - // also: do not try to write native type id - && !g.canWriteTypeId()) { + if (idMetadata.valueShape.isStructStart()) { + // 03-May-2023, tatu: [databind#3914]: should not write Native Type Id; + // but may need to write the value start marker + if (g.canWriteTypeId()) { + idMetadata.wrapperWritten = false; + if (idMetadata.valueShape == JsonToken.START_OBJECT) { + g.writeStartObject(idMetadata.forValue); + } else if (idMetadata.valueShape == JsonToken.START_ARRAY) { + g.writeStartArray(idMetadata.forValue); + } + return idMetadata; + } + // But for non-wrapper types can just use the default handling return g.writeTypePrefix(idMetadata); } return null;