Skip to content

Commit 44dea1f

Browse files
committed
Minor clean up for #222 fix
1 parent aae4082 commit 44dea1f

File tree

4 files changed

+43
-156
lines changed

4 files changed

+43
-156
lines changed

src/main/java/com/fasterxml/jackson/databind/ser/BeanPropertyWriter.java

+6-37
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public class BeanPropertyWriter extends PropertyWriter
160160
* is used for determining exact mechanism to use (compared to
161161
* actual runtime type used for serializing actual state).
162162
*/
163-
protected final TypeSerializer _typeSerializer;
163+
protected TypeSerializer _typeSerializer;
164164

165165
/**
166166
* In case serializer is not known statically (i.e. <code>_serializer</code>
@@ -335,33 +335,6 @@ protected BeanPropertyWriter(BeanPropertyWriter base, SerializedString name) {
335335
_metadata = base._metadata;
336336
}
337337

338-
/**
339-
* @since 2.6
340-
*/
341-
protected BeanPropertyWriter(BeanPropertyWriter base, TypeSerializer typeSer)
342-
{
343-
_name = base._name;
344-
_wrapperName = base._wrapperName;
345-
346-
_member = base._member;
347-
_contextAnnotations = base._contextAnnotations;
348-
_declaredType = base._declaredType;
349-
_accessorMethod = base._accessorMethod;
350-
_field = base._field;
351-
_serializer = base._serializer;
352-
_nullSerializer = base._nullSerializer;
353-
_internalSettings = base._internalSettings; // safe, as per usage
354-
_cfgSerializationType = base._cfgSerializationType;
355-
_dynamicSerializers = base._dynamicSerializers;
356-
_suppressNulls = base._suppressNulls;
357-
_suppressableValue = base._suppressableValue;
358-
_includeInViews = base._includeInViews;
359-
_nonTrivialBaseType = base._nonTrivialBaseType;
360-
_metadata = base._metadata;
361-
362-
_typeSerializer = typeSer;
363-
}
364-
365338
public BeanPropertyWriter rename(NameTransformer transformer) {
366339
String newName = transformer.transform(_name.getValue());
367340
if (newName.equals(_name.toString())) {
@@ -371,17 +344,13 @@ public BeanPropertyWriter rename(NameTransformer transformer) {
371344
}
372345

373346
/**
374-
* Mutant factory to construct and return a new {@link BeanPropertyWriter} with
375-
* specified type serializer, unless this instance already has that
376-
* type serializer configured.
377-
*
347+
* Method called to set, reset or clear the configured type serializer
348+
* for property.
349+
*
378350
* @since 2.6
379351
*/
380-
public BeanPropertyWriter withTypeSerializer(TypeSerializer typeSer) {
381-
if (typeSer == _typeSerializer) {
382-
return this;
383-
}
384-
return new BeanPropertyWriter(this, typeSer);
352+
public void assignTypeSerializer(TypeSerializer typeSer) {
353+
_typeSerializer = typeSer;
385354
}
386355

387356
/**

src/main/java/com/fasterxml/jackson/databind/ser/BeanSerializerFactory.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -721,9 +721,10 @@ protected List<BeanPropertyWriter> removeOverlappingTypeIds(SerializerProvider p
721721
}
722722
String n = td.getPropertyName();
723723
PropertyName typePropName = PropertyName.construct(n);
724+
724725
for (BeanPropertyWriter w2 : props) {
725726
if ((w2 != bpw) && w2.wouldConflictWithName(typePropName)) {
726-
props.set(i, bpw.withTypeSerializer(null));
727+
bpw.assignTypeSerializer(null);
727728
break;
728729
}
729730
}

src/test/java/com/fasterxml/jackson/databind/jsontype/TestExternalId.java

+35-8
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,12 @@
55
import com.fasterxml.jackson.annotation.*;
66
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
77
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
8-
98
import com.fasterxml.jackson.databind.BaseMapTest;
109
import com.fasterxml.jackson.databind.ObjectMapper;
1110

1211
// Tests for [JACKSON-453]
1312
public class TestExternalId extends BaseMapTest
1413
{
15-
/*
16-
/**********************************************************
17-
/* Helper types
18-
/**********************************************************
19-
*/
20-
2114
static class ExternalBean
2215
{
2316
@JsonTypeInfo(use=Id.NAME, include=As.EXTERNAL_PROPERTY, property="extType")
@@ -217,6 +210,31 @@ public AsValueThingy() { }
217210
}
218211
}
219212

213+
// [databind#222]
214+
static class Issue222Bean
215+
{
216+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
217+
property = "type",
218+
include = JsonTypeInfo.As.EXTERNAL_PROPERTY)
219+
public Issue222BeanB value;
220+
221+
public String type = "foo";
222+
223+
public Issue222Bean() { }
224+
public Issue222Bean(int v) {
225+
value = new Issue222BeanB(v);
226+
}
227+
}
228+
229+
@JsonTypeName("222b") // shouldn't actually matter
230+
static class Issue222BeanB
231+
{
232+
public int x;
233+
234+
public Issue222BeanB() { }
235+
public Issue222BeanB(int value) { x = value; }
236+
}
237+
220238
/*
221239
/**********************************************************
222240
/* Unit tests, serialization
@@ -399,7 +417,7 @@ public void testWithNaturalScalar118() throws Exception
399417
assertEquals("foobar", result.value);
400418
}
401419

402-
// For [Issue#119]... and bit of [#167] as well
420+
// For [databind#119]... and bit of [#167] as well
403421
public void testWithAsValue() throws Exception
404422
{
405423
ExternalTypeWithNonPOJO input = new ExternalTypeWithNonPOJO(new AsValueThingy(12345L));
@@ -422,4 +440,13 @@ public void testWithAsValue() throws Exception
422440
assertEquals(Date.class, result.value.getClass());
423441
assertEquals(12345L, ((Date) result.value).getTime());
424442
}
443+
444+
// for [databind#222]
445+
public void testExternalTypeWithProp222() throws Exception
446+
{
447+
final ObjectMapper mapper = new ObjectMapper();
448+
Issue222Bean input = new Issue222Bean(13);
449+
String json = mapper.writeValueAsString(input);
450+
assertEquals("{\"value\":{\"x\":13},\"type\":\"foo\"}", json);
451+
}
425452
}

src/test/java/com/fasterxml/jackson/failing/TestExternalTypeId222.java

-110
This file was deleted.

0 commit comments

Comments
 (0)