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

Change JsonNode.with(String) and withArray(String) to consider argument as JsonPointer if valid expression #3568

Closed
cowtowncoder opened this issue Aug 9, 2022 · 7 comments
Milestone

Comments

@cowtowncoder
Copy link
Member

With #1980 there are new methods/overloads for withArray() and with(), which take JsonPointer.
The old methods that take String should be retrofitted in 2.x so that:

  1. If argument is valid JsonPointer (that is, starts with leading '/` or is empty String ""), it should be used as such
  2. Only if argument is not a valid JsonPointer, should it check existing property.

For Jackson 3.0 we can just drop the check and expect valid JsonPointer String representation.

The reason for change is to make expected main use case work, while retaining reasonable backwards-compatibility.

@cowtowncoder cowtowncoder added to-evaluate Issue that has been received but not yet evaluated 2.14 and removed to-evaluate Issue that has been received but not yet evaluated labels Aug 9, 2022
@cowtowncoder cowtowncoder changed the title Change JsonNode.with(String) and withArray(String) to consider argument as JsonPointer (if valid) Change JsonNode.with(String) and withArray(String) to consider argument as JsonPointer if valid expression Aug 10, 2022
cowtowncoder added a commit that referenced this issue Aug 10, 2022
cowtowncoder added a commit that referenced this issue Aug 10, 2022
cowtowncoder added a commit that referenced this issue Aug 10, 2022
@dan2097
Copy link

dan2097 commented Aug 27, 2023

The reason for change is to make expected main use case work, while retaining reasonable backwards-compatibility.

For the corner case where a user is creating/fetching keys that start with a forward slash this change is unequivocally not backwards compatible. Given that with was deprecated as part of #3535 did its behaviour really need to change...

(I'm not personally effected by this change, was just wondering what the closest replacement for the deprecated with() method was as the Javadoc didn't provide a suggestion, and was surprised to see the JsonPointer parsing logic. As far as I can see there isn't an exact replacement if you want your input interpreted as a literal key. For my use case putObject was actually appropriate)

@alasdairhurst
Copy link

alasdairhurst commented Jul 17, 2024

The reason for change is to make expected main use case work, while retaining reasonable backwards-compatibility.

For the corner case where a user is creating/fetching keys that start with a forward slash this change is unequivocally not backwards compatible. Given that with was deprecated as part of #3535 did its behaviour really need to change...

(I'm not personally effected by this change, was just wondering what the closest replacement for the deprecated with() method was as the Javadoc didn't provide a suggestion, and was surprised to see the JsonPointer parsing logic. As far as I can see there isn't an exact replacement if you want your input interpreted as a literal key. For my use case putObject was actually appropriate)

We hit this exact issue when upgrading Jackson. The main issue is around looking up or performing iterative logic on keys dynamically rather than hardcoding the pointer or parameter argument. This leads to an unexpected difference in behavior when the key just happens to start with a slash. (I.e. when traversing JSON data that involves HTTP path segments, for example OpenAPI specs).

In our case, without a workaround exposed on the JsonNode class, we had to manually escape any dynamic property passed into with or withArray that started with a / and turn it into a valid json pointer for that property with escaped special chars for it then to be compiled and executed later which does impact performance in sensitive environments.

// previous
obj.with(key)

// current
public static String property(String property) {
    // Bypass JsonPointer encoding/decoding/compiling by returning the original prop where possible
    if (!property.startsWith("/")) {
	return property;
    }
    // Can't be used as a basic prop lookup so encode as JsonPointer 
    return JsonPointer.valueOf("").appendProperty(property).toString();
}

obj.with(property(key))

@cowtowncoder Could we ensure this one is added to the "Changes, compatibility" section of the 2.14 release notes indicating the breaking change please?

@cowtowncoder cowtowncoder added this to the 2.14.0 milestone Jul 17, 2024
@cowtowncoder
Copy link
Member Author

cowtowncoder commented Jul 17, 2024

@alasdairhurst Happy to add a note -- could use help in formulating what is said.
But can start by adding a link.

@cowtowncoder
Copy link
Member Author

Added link, brief description.

For anyone looking into solution, Jackson 2.16 added JsonNode.withObjectProperty(String) that will NOT try to auto-detect JsonPointer expression but use String as the property name, escape as necessary.
See #4095 for details.

@cowtowncoder
Copy link
Member Author

Thank you for suggestion, @alasdairhurst !

@alasdairhurst
Copy link

alasdairhurst commented Jul 17, 2024

Thanks @cowtowncoder. Something along the lines of the following. I'll leave it up to you to tweak/correct and place as you see fit.

Changes, behavior

Breaking changes

  • JsonNode.with and JsonNode.withArray now treat parameters with leading forward slashes / as JsonPointer expressions.
    • Formerly,,with('/fruit') would resolve the object at the property "/fruit". Now, the object at the property "fruit" would be resolved instead.
    • To resolve parameters with forward slashes as JsonPointer expressions going forward, ensure that the parameter name is fully escaped. For example: with("/~1fruit").
    • To maintain the previous behavior and keep using an explicit property name and NOT try to auto-detect JsonPointer expressions, JsonNode.withObjectProperty(String) or JsonNode.withArrayProperty(String) can be used in the place of JsonNode.with(String) and JsonNode.withArray(String) respectively. (Added in Jackson 2.16 #4095)
    • See #3568

@cowtowncoder
Copy link
Member Author

@alasdairhurst Thank you -- I adjusted https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.14 as suggested.

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

No branches or pull requests

3 participants