Search code examples
gremlin

Gremlin GroovyTranslator generates wrong result and fails at remote server


I am using Gremlin java and I found GroovyTranslator adds additional \ before $ sign, and this causes query failing to execute on remote server.

GraphTraversal traversal = graph.addV().property("amount", "$1");
System.out.println(GroovyTranslator.of("g").translate(traversal.asAdmin().getBytecode()));

Translated result:

g.addV().property("amount","\$1")
  1. If this is issue with GroovyTranslator, i can replace \$ with $, but I am not sure if more special characters will have this issue.

  2. This fails because of backslash, but what if some property value want to use backslash?
    From what I see, use backslash will always fail.
    I suppose following should work but it doesn't:

    curl -X POST -d '{"gremlin":"g.V().has(\"key\",\"\\$\")"}' ...
    

Solution

  • In Groovy the dollar sign has special meaning if you are using Groovy Strings (GStrings). It is used to indicate interpolation should occur as in :

    gremlin> a=3
    ==>3
    gremlin> "The number is $a"
    ==>The number is 3
    

    If the server you are connecting to uses Groovy as-is to parse the query then the backslash is needed. If the server does not use Groovy as-is then you will need to remove the backslash.

    There are a few other things to be aware of with GroovyTranslator. When it generates literal numbers it puts a cast such as (int) 3 into the query. You may need to also remove these depending on the back end graph database you are connecting to.