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

QueryStringUtil.get...QueryString(String,BindingSet) methods don't replace variables at the end of the query string #4880

Open
tayloj opened this issue Jan 24, 2024 · 1 comment
Labels
🐞 bug issue is a bug

Comments

@tayloj
Copy link

tayloj commented Jan 24, 2024

Current Behavior

This code:

   String prepare(final String unprepared) {
      final MapBindingSet mbs = new MapBindingSet();
      mbs.addBinding("iri", Values.iri("https://www.youtube.com/watch?v=dQw4w9WgXcQ"));
      return QueryStringUtil.getGraphQueryString(unprepared, mbs);
   }

yields these results:

      System.out.println(prepare("DESCRIBE ?iri")); // => DESCRIBE ?iri
      System.out.println(prepare("DESCRIBE ?iri ")); // => DESCRIBE <https://www.youtube.com/watch?v=dQw4w9WgXcQ> 

This appears to be because the pattern to replace is

String pattern = "[\\?\\$]" + name + "(?=\\W)";

which demands some non-word character after the variable.

Expected Behavior

      System.out.println(prepare("DESCRIBE ?iri")); // => DESCRIBE <https://www.youtube.com/watch?v=dQw4w9WgXcQ> 
      System.out.println(prepare("DESCRIBE ?iri ")); // => DESCRIBE <https://www.youtube.com/watch?v=dQw4w9WgXcQ> 

Steps To Reproduce

Self-contained test:

import org.eclipse.rdf4j.model.util.Values;
import org.eclipse.rdf4j.query.impl.MapBindingSet;
import org.eclipse.rdf4j.repository.sparql.query.QueryStringUtil;
import org.junit.Test;

public class TestQueryStrings {

   @Test
   public void testPrepare() {
      // Works when variable is followed by space
      assertEquals("DESCRIBE <https://www.youtube.com/watch?v=dQw4w9WgXcQ> ",
            prepare("DESCRIBE ?iri "));

      // Fails when variable is at end of input
      assertEquals("DESCRIBE <https://www.youtube.com/watch?v=dQw4w9WgXcQ>",
            prepare("DESCRIBE ?iri"));
   }

   String prepare(final String unprepared) {
      final MapBindingSet mbs = new MapBindingSet();
      mbs.addBinding("iri", Values.iri("https://www.youtube.com/watch?v=dQw4w9WgXcQ"));
      return QueryStringUtil.getGraphQueryString(unprepared, mbs);
   }

   private static <T> void assertEquals(final T expected, final T actual) {
      if (!expected.equals(actual)) {
         throw new AssertionError("Expected '" + expected + "' but got '" + actual + "'");
      }
   }
}

Version

4.3.6

Are you interested in contributing a solution yourself?

Perhaps?

Anything else?

No response

@tayloj tayloj added the 🐞 bug issue is a bug label Jan 24, 2024
@frensjan
Copy link
Contributor

\b would be better here indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug issue is a bug
Projects
None yet
Development

No branches or pull requests

2 participants