diff --git a/CHANGELOG.md b/CHANGELOG.md index cae418cb..73b19fc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Templated queries now also work for queries containing multiple occurrences of a template variable (#164). - Works when served from any base URL including a path (e.g. `https://www.example.com/your/preferred/path`) (#165). ## [1.4.0] - 2024-10-02 diff --git a/main/public/queries/musicians_variables_double.rq b/main/public/queries/musicians_variables_double.rq new file mode 100644 index 00000000..33499520 --- /dev/null +++ b/main/public/queries/musicians_variables_double.rq @@ -0,0 +1,17 @@ +PREFIX schema: + +SELECT ?name ?sameAs_url WHERE { + { + ?list schema:name ?listTitle; + schema:name ?name; + schema:genre $genre; + schema:sameAs ?sameAs_url; + } + UNION + { + ?list schema:name ?listTitle; + schema:name ?name; + schema:genre $genre; + schema:sameAs ?sameAs_url; + } +} \ No newline at end of file diff --git a/main/src/config.json b/main/src/config.json index d6bd1e92..55e90ba5 100644 --- a/main/src/config.json +++ b/main/src/config.json @@ -368,6 +368,26 @@ ] } }, + { + "id": "9012", + "queryGroupId": "c-tst", + "queryLocation": "musicians_variables_double.rq", + "name": "A templated query about musicians (double results)", + "description": "This query tests whether a template variable is expanded more than once. If it works, this query will return all results twice.", + "icon": "MusicNoteIcon", + "variables": { + "genre": [ + "\"Romantic\"", + "\"Baroque\"", + "\"Classical\"" + ] + }, + "comunicaContext": { + "sources": [ + "http://localhost:8080/example/favourite-musicians" + ] + } + }, { "id": "9020", "queryGroupId": "c-tst", diff --git a/main/src/dataProvider/SparqlDataProvider.js b/main/src/dataProvider/SparqlDataProvider.js index 58d0101f..79b9d18a 100644 --- a/main/src/dataProvider/SparqlDataProvider.js +++ b/main/src/dataProvider/SparqlDataProvider.js @@ -149,7 +149,7 @@ async function buildQueryText(query) { function replaceVariables(rawText, variables) { for (const [variableName, variableValue] of Object.entries(variables)) { // do not surround with double quotes here; add double quotes in the input if needed! - rawText = rawText.replace("$" + variableName, variableValue); + rawText = rawText.replaceAll("$" + variableName, variableValue); } return rawText; diff --git a/test/cypress/e2e/templated-query.cy.js b/test/cypress/e2e/templated-query.cy.js index 0c36fd9c..86817276 100644 --- a/test/cypress/e2e/templated-query.cy.js +++ b/test/cypress/e2e/templated-query.cy.js @@ -39,6 +39,21 @@ describe("Templated query", () => { // cy.get('button').contains('Query').should("not.exist"); -> useless if we add a 'new query' button }); + it("With 1 variables, double expansion", () => { + cy.visit("/"); + cy.contains("For testing only").click(); + cy.contains("A templated query about musicians (double results)").click(); + + cy.get('form').within(() => { + cy.get('#genre').click(); + }); + cy.get('li').contains('Romantic').click(); + + cy.get('button').contains('Query').click(); + cy.contains("Finished in:"); + cy.get('.column-name').find('span').should("have.length", 2); + }); + it("With 2 variables", () => { cy.visit("/"); cy.contains("For testing only").click();