Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3521,7 +3521,8 @@ public <T extends JsonNode> T valueToTree(Object fromValue)

// inlined 'writeValue' with minor changes:
// first: disable wrapping when writing
final SerializationConfig config = getSerializationConfig().without(SerializationFeature.WRAP_ROOT_VALUE);
// [databind#4047] ObjectMapper.valueToTree will ignore the configuration SerializationFeature.WRAP_ROOT_VALUE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment is slightly misleading: the issue is that it would ignore WRAP_ROOT_VALUE, but after this change it doesn't anymore. the comment makes it sound like now it's ignoring WRAP_ROOT_VALUE

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sorry; I just copied issue description which is misleading. If anyone can do a PR thatd be great (or I'll fix that when I get home).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

final SerializationConfig config = getSerializationConfig();
final DefaultSerializerProvider context = _serializerProvider(config);

// Then create TokenBuffer to use as JsonGenerator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.databind.node;

import com.fasterxml.jackson.annotation.JsonRootName;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
Expand Down Expand Up @@ -108,6 +109,13 @@ public void serializeWithType(JsonGenerator g,
}
}

// [databind#4047]
@JsonRootName("event")
static class Event {
public Long id;
public String name;
}

/*
/**********************************************************
/* Unit tests
Expand Down Expand Up @@ -363,4 +371,22 @@ public void testNodeConvert() throws Exception
result = MAPPER.treeToValue(node, MAPPER.constructType(ObjectNode.class));
assertSame(src, result);
}

// [databind#4047] : ObjectMapper.valueToTree will ignore the configuration SerializationFeature.WRAP_ROOT_VALUE
public void testValueToTree() throws Exception
{
// Arrange
Event value = new Event();
value.id = 1L;
value.name = "foo";

ObjectMapper wrapRootMapper = jsonMapperBuilder()
.enable(SerializationFeature.WRAP_ROOT_VALUE)
.build();

// Act & Assert
String expected = "{\"event\":{\"id\":1,\"name\":\"foo\"}}";
assertEquals(expected, wrapRootMapper.writeValueAsString(value));
assertEquals(expected, wrapRootMapper.valueToTree(value).toString());
}
}