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

JsonPath=$ causes ClassCastException #4937

Open
greg-at-moderne opened this issue Jan 23, 2025 · 2 comments
Open

JsonPath=$ causes ClassCastException #4937

greg-at-moderne opened this issue Jan 23, 2025 · 2 comments
Labels
bug Something isn't working parser-json

Comments

@greg-at-moderne
Copy link
Contributor

greg-at-moderne commented Jan 23, 2025

What version of OpenRewrite are you using?

Current main (4684213)

Minimal example

The following test in JsonPathMatcherTest:

    @Test
    void dollarToMatchTheRoot() {
        assertMatched("$", simple, simple);
    }

fails with:

line 1:1 mismatched input '<EOF>' expecting {'[', '.', '..'}

org.openrewrite.internal.RecipeRunException: java.lang.ClassCastException: class org.antlr.v4.runtime.tree.TerminalNodeImpl cannot be cast to class org.openrewrite.json.internal.grammar.JsonPathParser$ExpressionContext (org.antlr.v4.runtime.tree.TerminalNodeImpl and org.openrewrite.json.internal.grammar.JsonPathParser$ExpressionContext are in unnamed module of loader 'app')

	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:290)
[...]
Caused by: java.lang.ClassCastException: class org.antlr.v4.runtime.tree.TerminalNodeImpl cannot be cast to class org.openrewrite.json.internal.grammar.JsonPathParser$ExpressionContext (org.antlr.v4.runtime.tree.TerminalNodeImpl and org.openrewrite.json.internal.grammar.JsonPathParser$ExpressionContext are in unnamed module of loader 'app')
	at org.openrewrite.json.JsonPathMatcher.find0(JsonPathMatcher.java:76)
	at org.openrewrite.json.JsonPathMatcher.matches(JsonPathMatcher.java:86)
	at org.openrewrite.json.JsonPathMatcherTest$2.visitLiteral(JsonPathMatcherTest.java:914)
	at org.openrewrite.json.JsonPathMatcherTest$2.visitLiteral(JsonPathMatcherTest.java:864)
	at org.openrewrite.json.tree.Json$Literal.acceptJson(Json.java:253)
	at org.openrewrite.json.tree.Json.accept(Json.java:39)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
	... 28 more

Broader context

  • JsonPath is used as an argument to multiple recipes for languages like JSON or YAML
  • AFAICT $ is a valid JsonPath expression to denote the root of the document and it should be supported
  • e.g.
    spec -> spec.recipe(new AddKeyValue("$.", "key", "\"val\"", false)),
    uses $. (which BTW I think is invalid JsonPath), if you put $ there you get the same ClassCastException.
  • in some contexts the exception is wrapped/handled and the only thing you can see is a warning:
line 1:2 mismatched input '<EOF>' expecting {'[', '*', Identifier, StringLiteral}
@greg-at-moderne greg-at-moderne added the bug Something isn't working label Jan 23, 2025
@greg-at-moderne
Copy link
Contributor Author

Labelling as parser-json as the "closest relative" of JsonPath. The problem likely affects HCL and YAML too.

@timtebeek timtebeek moved this to Backlog in OpenRewrite Jan 24, 2025
@Jenson3210
Copy link
Contributor

I can confirm this is also showing in the MergeYaml recipe -> was able to bypass the error by using $.. Strangely enough, when I run the tests that execute the MergeYaml directly, there it is working. (there are even existing tests)
When i run the tests that run a recipe that internally calls MergeYaml, it is not working with this exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser-json
Projects
Status: Backlog
Development

No branches or pull requests

2 participants