Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectMapper.valueToTree() will ignore the configuration SerializationFeature.WRAP_ROOT_VALUE #4047

Closed
1 task done
jessepys opened this issue Jul 18, 2023 · 12 comments · Fixed by #4050
Closed
1 task done
Labels
2.16 Issues planned for 2.16

Comments

@jessepys
Copy link

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

When we upgrade the jackson-databind version, then we found the ObjectMapper.valueToTree will return the different result with the previous version. Actually, we configured the SerializationFeature.WRAP_ROOT_VALUE.

The class is like this:

@JsonRootName("event")
public class Event {

}

The previous ObjectMapper.valueToTree result:
image

After upgraded the version result:
image

This should caused by the commit.
2e986df
Can we re-enbale SerializationFeature.WRAP_ROOT_VALUE in ObjectMapper.valueToTree method to keep consistent with method writeValueAsString?

Version Information

2.14.2 (The version after 2.13 should have this issue)

Reproduction

<-- Any of the following

  1. Configure the object mapper objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
  2. Call objectMapper.valueToTree(event) method
  3. You can the SerializationFeature.WRAP_ROOT_VALUE does not take effect after version 2.13.x
    -->
 public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.findAndRegisterModules();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.registerModule(new JSR310Module());
        return objectMapper;
    }

@JsonRootName("event")
public class Event {
    private Long id;
    private String name;
}
//call valueToTree method
objectMapper.valueToTree(event)

Expected behavior

SerializationFeature.WRAP_ROOT_VALUE configuration should be global and take effect in method valueToTree to keep the same with writeValueAsString

Additional context

No response

@jessepys jessepys added the to-evaluate Issue that has been received but not yet evaluated label Jul 18, 2023
@yawkat
Copy link
Member

yawkat commented Jul 18, 2023

it looks to me like the snippet was incorrectly copied from the convert implementation.

@cowtowncoder
Copy link
Member

We'd probably need a more slimmed-down reproduction here.

@cowtowncoder cowtowncoder added need-test-case To work on issue, a reproduction (ideally unit test) needed and removed to-evaluate Issue that has been received but not yet evaluated labels Jul 18, 2023
@yawkat
Copy link
Member

yawkat commented Jul 19, 2023

@cowtowncoder reproducer:

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);

        Event value = new Event();
        value.id = 1L;
        value.name = "foo";
        System.out.println(objectMapper.writeValueAsString(value)); // {"event":{"id":1,"name":"foo"}}
        System.out.println(objectMapper.valueToTree(value)); // {"id":1,"name":"foo"}
    }

    @JsonRootName("event")
    public static class Event {
        public Long id;
        public String name;
    }

The bug was introduced in 2e986df because valueToTree now explicitly drops WRAP_ROOT_VALUE. imo this should be reverted

@cowtowncoder
Copy link
Member

Hmmh. Does sounds problematic: wish I had added notes on why I felt this should be disabled. I think there's fundamental friction between WRAP_ROOT_VALUE usage, JsonNode, so that's likely why. But I guess easy enough to see which unit tests fail if leaving that feature on in a PR.

@JooHyukKim
Copy link
Member

The bug was introduced in .... ... WRAP_ROOT_VALUE. imo this should be reverted

I will see if we can revert the commit. But until then, anyone can jump on the #4049 refer to.

@JooHyukKim JooHyukKim mentioned this issue Jul 20, 2023
@JooHyukKim
Copy link
Member

Hmmh. Does sounds problematic: wish I had added notes on why I felt this should be disabled. I think there's fundamental friction between WRAP_ROOT_VALUE usage, JsonNode.... // rest-omitted

By refering to #2989, seems like it.

@JooHyukKim
Copy link
Member

JooHyukKim commented Jul 20, 2023

But I guess easy enough to see which unit tests fail if leaving that feature on in a PR.

#4050 Doesn't break anything 🤔. Was the commit used somewhere? So currently there are two PR's heading to different approach.

#4049 #4050
Approach Does not revert commit 2e986df and finds another solution. Reverts commit 2e986df.
Considerations 1. Need to find a solution 1. Is the commit the optimal way to handle support/resolve #2989?
2. Will there be side effects? like other modules already depend on it.

@yawkat
Copy link
Member

yawkat commented Jul 21, 2023

another alternative is to only revert the change to valueToTree.

@JooHyukKim
Copy link
Member

another alternative is to only revert the change to valueToTree.

True 👍🏻, as long as it does not introduce another inconsistent behavior. Reading through #2989 tho, it seems like the modification was targeted a broader range (out of databind) to cover formats other than JSON.

@cowtowncoder
Copy link
Member

Quick note: I don't think reversal of that sizable commit is the way to go. But I need time to look into alternative approaches.

@JooHyukKim
Copy link
Member

JooHyukKim commented Jul 22, 2023

Quick note: I don't think reversal of that sizable commit is the way to go. But I need time to look into alternative approaches.

Agreed. Closed #4050.

Btw, It seems like there might be another issue as pointed in this comment in PR #4049.

@JooHyukKim
Copy link
Member

another alternative is to only revert the change to valueToTree.

Trying to apply change to valueToTree by removing the explicit .without(SerializationFeature.WRAP_ROOT_VALUE).
Seems to work with current test cases #4050. Any possible test case that can break?

@cowtowncoder cowtowncoder added 2.16 Issues planned for 2.16 and removed need-test-case To work on issue, a reproduction (ideally unit test) needed labels Jul 26, 2023
@cowtowncoder cowtowncoder changed the title ObjectMapper.valueToTree will ignore the configuration SerializationFeature.WRAP_ROOT_VALUE ObjectMapper.valueToTree() will ignore the configuration SerializationFeature.WRAP_ROOT_VALUE Jul 26, 2023
cowtowncoder pushed a commit that referenced this issue Jul 26, 2023
cowtowncoder added a commit that referenced this issue Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.16 Issues planned for 2.16
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants