Skip to content

Commit 8c820ef

Browse files
authored
Fix #4047 (#4050)
1 parent deaf38f commit 8c820ef

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3521,7 +3521,8 @@ public <T extends JsonNode> T valueToTree(Object fromValue)
35213521

35223522
// inlined 'writeValue' with minor changes:
35233523
// first: disable wrapping when writing
3524-
final SerializationConfig config = getSerializationConfig().without(SerializationFeature.WRAP_ROOT_VALUE);
3524+
// [databind#4047] ObjectMapper.valueToTree will ignore the configuration SerializationFeature.WRAP_ROOT_VALUE
3525+
final SerializationConfig config = getSerializationConfig();
35253526
final DefaultSerializerProvider context = _serializerProvider(config);
35263527

35273528
// Then create TokenBuffer to use as JsonGenerator

src/test/java/com/fasterxml/jackson/databind/node/TestConversions.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.databind.node;
22

3+
import com.fasterxml.jackson.annotation.JsonRootName;
34
import java.io.IOException;
45
import java.math.BigDecimal;
56
import java.util.*;
@@ -108,6 +109,13 @@ public void serializeWithType(JsonGenerator g,
108109
}
109110
}
110111

112+
// [databind#4047]
113+
@JsonRootName("event")
114+
static class Event {
115+
public Long id;
116+
public String name;
117+
}
118+
111119
/*
112120
/**********************************************************
113121
/* Unit tests
@@ -363,4 +371,22 @@ public void testNodeConvert() throws Exception
363371
result = MAPPER.treeToValue(node, MAPPER.constructType(ObjectNode.class));
364372
assertSame(src, result);
365373
}
374+
375+
// [databind#4047] : ObjectMapper.valueToTree will ignore the configuration SerializationFeature.WRAP_ROOT_VALUE
376+
public void testValueToTree() throws Exception
377+
{
378+
// Arrange
379+
Event value = new Event();
380+
value.id = 1L;
381+
value.name = "foo";
382+
383+
ObjectMapper wrapRootMapper = jsonMapperBuilder()
384+
.enable(SerializationFeature.WRAP_ROOT_VALUE)
385+
.build();
386+
387+
// Act & Assert
388+
String expected = "{\"event\":{\"id\":1,\"name\":\"foo\"}}";
389+
assertEquals(expected, wrapRootMapper.writeValueAsString(value));
390+
assertEquals(expected, wrapRootMapper.valueToTree(value).toString());
391+
}
366392
}

0 commit comments

Comments
 (0)