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

JsonNode.deepCopy() ClassCastException`s #1829

Closed
D3v01dZA opened this issue Nov 16, 2017 · 3 comments
Closed

JsonNode.deepCopy() ClassCastException`s #1829

D3v01dZA opened this issue Nov 16, 2017 · 3 comments
Labels
3.0 Issue planned for initial 3.0 release
Milestone

Comments

@D3v01dZA
Copy link

The current signature of JsonNode#deepCopy looks like this:

public abstract <T extends JsonNode> T deepCopy();

I wondered about the decision to do this as you can quite easily create a ClassCastException using this as follows:

TextNode text = TextNode.valueOf("SomeText");
ObjectNode object = text.deepCopy();

This code is obviously incorrect just by looking at it however this will always compile and throw an exception at runtime. I know this is considered an API change but a signature that prevents this might be:

JsonNode.java
public abstract JsonNode deepCopy();

And then be overridden on each subclass with the correct type

ValueNode.java
public abstract ValueNode deepCopy();

After looking at this again, the signatures of ObjectNode and ArrayNode are correct but you can still trick it by upcasting to JsonNode and then using deepCopy()

@cowtowncoder
Copy link
Member

Hmmh. Yes, it would be possible to use return type co-variance here. This could be done for Jackson 3.x (as it can not be done for 2.x; not something that can go in a patch, and 2.9 is the last 2.x minor version).

@cowtowncoder cowtowncoder added the 3.x Issues to be only tackled for Jackson 3.x, not 2.x label Nov 16, 2017
@cowtowncoder
Copy link
Member

lol on "2.9 is the last". We are at 2.13 now... :)

Still, if done, this needs to go in 3.0.

@cowtowncoder cowtowncoder added 3.0 Issue planned for initial 3.0 release and removed 3.x Issues to be only tackled for Jackson 3.x, not 2.x labels Dec 1, 2024
@cowtowncoder cowtowncoder changed the title JsonNode#deepCopy ClassCast Exceptions JsonNode.deepCopy() ClassCastException`s Dec 5, 2024
@cowtowncoder cowtowncoder added this to the 3.0.0 milestone Dec 5, 2024
@cowtowncoder
Copy link
Member

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.0 Issue planned for initial 3.0 release
Projects
None yet
Development

No branches or pull requests

2 participants