Skip to content

Commit cf0d451

Browse files
authored
Fix templated queries with 2+ variable occurrences (#168)
1 parent 66ec99b commit cf0d451

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12+
- Templated queries now also work for queries containing multiple occurrences of a template variable (#164).
1213
- Works when served from any base URL including a path (e.g. `https://www.example.com/your/preferred/path`) (#165).
1314

1415
## [1.4.0] - 2024-10-02
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
PREFIX schema: <http://schema.org/>
2+
3+
SELECT ?name ?sameAs_url WHERE {
4+
{
5+
?list schema:name ?listTitle;
6+
schema:name ?name;
7+
schema:genre $genre;
8+
schema:sameAs ?sameAs_url;
9+
}
10+
UNION
11+
{
12+
?list schema:name ?listTitle;
13+
schema:name ?name;
14+
schema:genre $genre;
15+
schema:sameAs ?sameAs_url;
16+
}
17+
}

main/src/config.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,26 @@
368368
]
369369
}
370370
},
371+
{
372+
"id": "9012",
373+
"queryGroupId": "c-tst",
374+
"queryLocation": "musicians_variables_double.rq",
375+
"name": "A templated query about musicians (double results)",
376+
"description": "This query tests whether a template variable is expanded more than once. If it works, this query will return all results twice.",
377+
"icon": "MusicNoteIcon",
378+
"variables": {
379+
"genre": [
380+
"\"Romantic\"",
381+
"\"Baroque\"",
382+
"\"Classical\""
383+
]
384+
},
385+
"comunicaContext": {
386+
"sources": [
387+
"http://localhost:8080/example/favourite-musicians"
388+
]
389+
}
390+
},
371391
{
372392
"id": "9020",
373393
"queryGroupId": "c-tst",

main/src/dataProvider/SparqlDataProvider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ async function buildQueryText(query) {
149149
function replaceVariables(rawText, variables) {
150150
for (const [variableName, variableValue] of Object.entries(variables)) {
151151
// do not surround with double quotes here; add double quotes in the input if needed!
152-
rawText = rawText.replace("$" + variableName, variableValue);
152+
rawText = rawText.replaceAll("$" + variableName, variableValue);
153153
}
154154

155155
return rawText;

test/cypress/e2e/templated-query.cy.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ describe("Templated query", () => {
3939
// cy.get('button').contains('Query').should("not.exist"); -> useless if we add a 'new query' button
4040
});
4141

42+
it("With 1 variables, double expansion", () => {
43+
cy.visit("/");
44+
cy.contains("For testing only").click();
45+
cy.contains("A templated query about musicians (double results)").click();
46+
47+
cy.get('form').within(() => {
48+
cy.get('#genre').click();
49+
});
50+
cy.get('li').contains('Romantic').click();
51+
52+
cy.get('button').contains('Query').click();
53+
cy.contains("Finished in:");
54+
cy.get('.column-name').find('span').should("have.length", 2);
55+
});
56+
4257
it("With 2 variables", () => {
4358
cy.visit("/");
4459
cy.contains("For testing only").click();

0 commit comments

Comments
 (0)