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

Use JsonPath.Net for JSONPath selectors (Lombiq Technologies: OCORE-148) #15524

Merged
merged 27 commits into from
Mar 21, 2024

Conversation

sarahelsaig
Copy link
Contributor

@sarahelsaig sarahelsaig commented Mar 16, 2024

Fixes #15505

@Piedone
Copy link
Member

Piedone commented Mar 18, 2024

@MikeAlhayek could you please check?

Copy link
Member

@MikeAlhayek MikeAlhayek left a comment

Choose a reason for hiding this comment

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

I would request a review from @sebastienros for this one.

/// specification at https://www.rfc-editor.org/rfc/rfc9535.html or the JsonPath.Net documentation at
/// https://docs.json-everything.net/path/basics/.
/// </remarks>
public static JsonNode? SelectNode(this JsonNode? jsonNode, string? path, PathParsingOptions? options = null)
Copy link
Member

Choose a reason for hiding this comment

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

Why all these ? ? If nullability is enabled then it's wrong IMO, if not then it's not necessary, right?

Copy link
Member

Choose a reason for hiding this comment

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

I realized it was there before, it should still be fixed IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nullability was already enabled in this file here. I agree, jsonNode˙and path shouldn't be nullable (but options should, ofc). Fixed.

}
path = path?.Trim();
if (string.IsNullOrEmpty(path)) return jsonNode;
if (path[0] != '$') path = "$." + path;
Copy link
Member

Choose a reason for hiding this comment

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

is not using $. not supported in JSONPath.Net?
Ideally we would not patch what the caller specifies, but if we are using something that is currently incompatible, and JSONPath.NEt doesn't support it then fine. Add a comment in that case.

Copy link
Contributor Author

@sarahelsaig sarahelsaig Mar 18, 2024

Choose a reason for hiding this comment

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

It's not supported. (afaik paths without the leading $ is non-spec in JSONPath) Without that condition JsonPath.TryParse returns false, and JsonPath.Parse(path) throws:

Json.Path.PathParseException: Path must start with '$'
   at Json.Path.PathParser.Parse(ReadOnlySpan`1 source, Int32& index, PathParsingOptions options, Boolean requireGlobal)
   at Json.Path.JsonPath.Parse(String source, PathParsingOptions options)

Based on the exception message, this patch should be universally safe.

I believe in Newtonsoft does this automatically under the hood. You can see from this example, that the path property behaves as $.property and not as $..property.

image

I agree that it's not ideal, but without this patch existing functionality in OC would break.
Added a comment.

@Piedone Piedone changed the title Use JsonPath.Net for JSONPath selectors Use JsonPath.Net for JSONPath selectors (Lombiq Technologies: OCORE-148) Mar 19, 2024
Copy link
Contributor

This pull request has merge conflicts. Please resolve those before requesting a review.

# Conflicts:
#	src/docs/community/contributors/images/contributors-map.svg
# Conflicts:
#	src/OrchardCore.Build/Dependencies.props
# Conflicts:
#	src/docs/community/contributors/images/contributors-map.svg
# Conflicts:
#	src/docs/community/contributors/images/contributors-map.svg
Copy link
Contributor

This pull request has merge conflicts. Please resolve those before requesting a review.

# Conflicts:
#	src/OrchardCore.Modules/OrchardCore.Menu/Controllers/AdminController.cs
Copy link
Contributor

This pull request has merge conflicts. Please resolve those before requesting a review.

@MikeAlhayek
Copy link
Member

@sarahelsaig can you please fix the conflict here so we can merge it?

# Conflicts:
#	test/OrchardCore.Tests/Data/ContentItemTests.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent JObject.SelectNode() behavior and feature loss
4 participants