You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by "andrii0lomakin (via GitHub)" <gi...@apache.org> on 2025/11/06 10:50:09 UTC

[PR] Fix JavaTranslator's `has` handling for null arguments [tinkerpop]

andrii0lomakin opened a new pull request, #3274:
URL: https://github.com/apache/tinkerpop/pull/3274

   - Updated `has()` special case logic to address potential issues when first or third arguments are `null`. Example : `g.E().has("knows","weight",null)` this query can lead to NPE on server execution as JavaTranslator can choose GraphTravers#has(String, Sring, P) method instead of GraphTravers#has(String, Sring, Object)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix JavaTranslator's `has` handling for null arguments [tinkerpop]

Posted by "xiazcy (via GitHub)" <gi...@apache.org>.
xiazcy closed pull request #3274: Fix JavaTranslator's `has` handling for null arguments
URL: https://github.com/apache/tinkerpop/pull/3274


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix JavaTranslator's `has` handling for null arguments [tinkerpop]

Posted by "andrii0lomakin (via GitHub)" <gi...@apache.org>.
andrii0lomakin commented on PR #3274:
URL: https://github.com/apache/tinkerpop/pull/3274#issuecomment-3511837413

   Hi.
   I will reimplement this fix.
   
   The correct fix is to sort the list of methods during the building method cache by isAssigned from the relationship. That will fix all current and future problems.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix JavaTranslator's `has` handling for null arguments [tinkerpop]

Posted by "andreachild (via GitHub)" <gi...@apache.org>.
andreachild commented on PR #3274:
URL: https://github.com/apache/tinkerpop/pull/3274#issuecomment-3503836042

   It would be good to add a unit test for this. Here is a suggested one since there are no existing (direct) unit tests for the `JavaTranslator`. Note that if you execute the test below without the code fix it will randomly fail/pass depending on the order of methods loaded into the `JavaTranslator.GLOBAL_METHOD_CACHE`.
   
   ```
   package org.apache.tinkerpop.gremlin.jsr223;
   
   import org.apache.tinkerpop.gremlin.process.traversal.Bytecode;
   import org.apache.tinkerpop.gremlin.process.traversal.P;
   import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
   import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
   import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;
   import org.junit.Test;
   
   import static org.junit.Assert.assertEquals;
   
   public class JavaTranslatorTest {
       private GraphTraversalSource g = EmptyGraph.instance().traversal();
       private JavaTranslator<GraphTraversalSource, Traversal.Admin<?, ?>> translator = JavaTranslator.of(EmptyGraph.instance().traversal());
   
       @Test
       public void shouldTranslateHasWithObjectArgValue() {
           final Bytecode bytecode = new Bytecode();
           bytecode.addStep("E");
           bytecode.addStep("has", "knows", "weight", 1.0);
           final Traversal.Admin<?, ?> translation = translator.translate(bytecode);
           assertEquals(g.E().has("knows", "weight", 1.0).asAdmin(), translation);
       }
   
       @Test
       public void shouldTranslateHasWithPredicateArgValue() {
           final Bytecode bytecode = new Bytecode();
           bytecode.addStep("E");
           bytecode.addStep("has", "knows", "weight", P.eq(1.0));
           final Traversal.Admin<?, ?> translation = translator.translate(bytecode);
           assertEquals(g.E().has("knows", "weight", P.eq(1.0)).asAdmin(), translation);
       }
   
       @Test
       public void shouldTranslateHasWithNullThirdArgValue() {
           final Bytecode bytecode = new Bytecode();
           bytecode.addStep("E");
           bytecode.addStep("has", "knows", "weight", null);
           final Traversal.Admin<?, ?> translation = translator.translate(bytecode);
           assertEquals(g.E().has("knows", "weight", (String) null).asAdmin(), translation);
       }
   
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix JavaTranslator's `has` handling for null arguments [tinkerpop]

Posted by "andreachild (via GitHub)" <gi...@apache.org>.
andreachild commented on code in PR #3274:
URL: https://github.com/apache/tinkerpop/pull/3274#discussion_r2504743438


##########
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/JavaTranslator.java:
##########
@@ -22,10 +22,7 @@
 import org.apache.commons.configuration2.BaseConfiguration;
 import org.apache.commons.configuration2.Configuration;
 import org.apache.commons.configuration2.MapConfiguration;
-import org.apache.tinkerpop.gremlin.process.traversal.Bytecode;
-import org.apache.tinkerpop.gremlin.process.traversal.Translator;
-import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
-import org.apache.tinkerpop.gremlin.process.traversal.TraversalSource;
+import org.apache.tinkerpop.gremlin.process.traversal.*;

Review Comment:
   It looks like your IDE has auto style formatting which differs from the conventions used in the tinkerpop project and thus the wildcard import has been added and various whitespace changes as well. According to https://tinkerpop.apache.org/docs/current/dev/developer/#_code_style we try to keep the style consistent with the existing conventions.
   
   > Contributors should examine the current code base to determine what the code style patterns are and should match their style to what is already present. Of specific note however, TinkerPop does not use "import wildcards" - IDEs should be adjusted accordingly to not auto-wildcard the imports.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix JavaTranslator's `has` handling for null arguments [tinkerpop]

Posted by "andreachild (via GitHub)" <gi...@apache.org>.
andreachild commented on code in PR #3274:
URL: https://github.com/apache/tinkerpop/pull/3274#discussion_r2505063672


##########
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/JavaTranslator.java:
##########
@@ -301,20 +298,30 @@ private Object invokeMethod(final Object delegate, final Class<?> returnType, fi
                             }
                         }
 
-                        // special case has() where the first arg is null - sometimes this can end up with the T being
-                        // null and in 3.5.x that generates an exception which raises badly in the translator. it is
-                        // safer to force this to the String form by letting this "found" version pass. In java this
-                        // form of GraphTraversal can't be produced because of validations for has(T, ...) but in
-                        // other language it might be allowed which means that has((T) null, ...) from something like
+                        // special cases of has() where the first arg is null or third arg is null - sometimes this can end up with the T being
+                        // null or in the second case calling has that accepts predicate instead of string as parameter in the last argument.
+                        // That generates an exception which raises badly in the translator. it is
+                        // safer to force this to the String form by letting this "found" version pass. In Java
+                        // form of GraphTraversal generated in the first case  can't be produced because of validations
+                        // for has(T, ...) but in other language it might be allowed which means that has((T) null, ...) from something like
                         // python will end up as has((String) null, ...) which filters rather than generates an
                         // exception. calling the T version even in a non-JVM language seems odd and more likely the
                         // caller was shooting for the String one, but ugh who knows. this issue showcases again how
                         // badly bytecode should either change to use gremlin-language and go away or bytecode should
                         // get a safer way to be translated to a traversal with more explicit calls that don't rely
                         // on reflection.
-                        if (methodName.equals(GraphTraversal.Symbols.has) && newArguments.length > 0 && null == newArguments[0] &&
-                            method.getParameterTypes()[0].isAssignableFrom(org.apache.tinkerpop.gremlin.structure.T.class)) {
-                            found = false;
+                        var parametersTypes = method.getParameterTypes();
+                        if (methodName.equals(GraphTraversal.Symbols.has) && newArguments.length > 0) {
+                            //first case has((T)null, ...) instead of has((String)null, ...)
+                            if (null == newArguments[0] &&
+                                    parametersTypes[0].isAssignableFrom(org.apache.tinkerpop.gremlin.structure.T.class)) {
+                                found = false;
+                            } else if (newArguments.length == 3 && newArguments[2] == null && parametersTypes[0] == String.class &&
+                                    parametersTypes[1] == String.class &&
+                                    parametersTypes[2] == P.class) {
+                                //the second case has(String, String, (P)(null)) instead of has(String,String, (Object)null)
+                                found = false;
+                            }

Review Comment:
   I'm surprised the has overloads with 2 method args have not also surfaced also a problem (it could just be by luck depending on the order of the methods loaded in the global cache). You could change the logic to address both the 2 and 3 arg method overloads:
   
   ```suggestion
                               } else if (newArguments[newArguments.length - 1] == null && parametersTypes[newArguments.length - 1] == P.class) {
                                   //the second case has(String, String, (P)(null)) instead of has(String,String, (Object)null)
                                   //or has(String, (P)(null)) instead of has(String, (Object)null)
                                   found = false;
                               }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix JavaTranslator's `has` handling for null arguments [tinkerpop]

Posted by "andreachild (via GitHub)" <gi...@apache.org>.
andreachild commented on PR #3274:
URL: https://github.com/apache/tinkerpop/pull/3274#issuecomment-3524496113

   Hi @andrii0lomakin I opened https://github.com/apache/tinkerpop/pull/3278 because I wanted a fix for the issue you found in 3.8.0 for which the release vote is happening today which unfortunately doesn't give you enough time to refactor the change as you mentioned. Please do continue with this PR and it can be added to 3.8.1 or 4.0. Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix JavaTranslator's `has` handling for null arguments [tinkerpop]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #3274:
URL: https://github.com/apache/tinkerpop/pull/3274#issuecomment-3496596175

   ## [Codecov](https://app.codecov.io/gh/apache/tinkerpop/pull/3274?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :white_check_mark: All modified and coverable lines are covered by tests.
   :warning: Please [upload](https://docs.codecov.com/docs/codecov-uploader) report for BASE (`3.8-dev@3cfb322`). [Learn more](https://docs.codecov.io/docs/error-reference?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#section-missing-base-commit) about missing BASE report.
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@            Coverage Diff             @@
   ##             3.8-dev    #3274   +/-   ##
   ==========================================
     Coverage           ?   79.07%           
   ==========================================
     Files              ?       28           
     Lines              ?     5940           
     Branches           ?        0           
   ==========================================
     Hits               ?     4697           
     Misses             ?     1022           
     Partials           ?      221           
   ```
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/tinkerpop/pull/3274?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix JavaTranslator's `has` handling for null arguments [tinkerpop]

Posted by "xiazcy (via GitHub)" <gi...@apache.org>.
xiazcy commented on PR #3274:
URL: https://github.com/apache/tinkerpop/pull/3274#issuecomment-6332205062

   Hi @andrii0lomakin, thank you for taking the time to contribute. Given this PR hasn't seen activity in a while, I'm closing it as part of a backlog cleanup. Please feel free to reopen or raise a fresh PR against the current branch and we'll take a look.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tinkerpop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org