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

Fixes GH-2013 by traversing parents until sourceString is found #2014

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ public boolean isSilent() {
public String getPatternString() {

if (patternString == null) {
ASTOperationContainer parentContainer = (ASTOperationContainer) getParentContainer(this);

String sourceString = parentContainer.getSourceString();

// snip away line until begin token line position
String sourceString = getSourceString();
String substring = sourceString;
for (int i = 1; i < getBeginTokenLinePos(); i++) {
substring = substring.substring(substring.indexOf('\n') + 1);
Expand All @@ -84,6 +81,27 @@ public String getPatternString() {
return patternString;
}

private String getSourceString() {
Node theParent = getParentContainer(this);
String sourceString = null;
if (theParent instanceof ASTOperationContainer) {
sourceString = ((ASTOperationContainer) theParent).getSourceString();
} else if (theParent instanceof ASTUpdateSequence) {
sourceString = ((ASTUpdateSequence) theParent).getSourceString();
}

while (sourceString == null && theParent != null) {
theParent = theParent.jjtGetParent();
if (theParent == null) {
break;
}
if (theParent instanceof ASTUpdateSequence) {
sourceString = ((ASTUpdateSequence) theParent).getSourceString();
}
}
return sourceString;
Copy link
Contributor

Choose a reason for hiding this comment

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

I was wondering if maybe there could be a null check here, so that any exception is thrown when looking for the source string instead of when using the resulting string. Maybe return Objects.requireNonNull(sourceString);

Unless of course there is a case where returning null from getSourceString() is acceptable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thought of that for a while, but decided that if we did not found the sourceString at this point, something else was really messed up, so throwing a custom exception early, would not help much here since we do not have a resonable alternative to continue processing update further - a NPE will still be thrown in the code that uses it ...

}

private Node getParentContainer(Node node) {
if (node instanceof ASTOperationContainer || node == null) {
return node;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright (c) 2020 Eclipse RDF4J contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*******************************************************************************/
package org.eclipse.rdf4j.query.parser.sparql;

import org.eclipse.rdf4j.query.algebra.Service;
import org.eclipse.rdf4j.query.parser.ParsedUpdate;
import org.junit.Test;

public class TestServiceUpdateExprBuilder {

/**
* The test reproduces a {@link NullPointerException} that is thrown when parsing an update with several update
* expressions and one of these contain {@link Service} operator The NPE is thrown because the sourceString is set
* to the outermost operation container
*/
@Test
public void testServiceWithMultipleUpdateExpr() {
SPARQLParser parser = new SPARQLParser();
String updateStr = "PREFIX family: <http://examples.ontotext.com/family#>\n" +
"DROP ALL ;\n" +
"INSERT {\n" +
" family:Alice family:knows family:Bob .\n" +
"}\n" +
"WHERE {\n" +
" SERVICE <repository:1> {\n" +
" family:Alice family:knows family:Bob .\n" +
" }\n" +
"}";
// should not throw NPE, but prior to 3.1.3 it does
parser.parseUpdate(updateStr, null);
}
}