From 775aaa79cd92f63e7ed31244d50c9c2e1666b700 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 24 Dec 2020 12:43:56 +0100 Subject: [PATCH] fix: Use base IRI when parsing SPARQL update queries Accept relative references in SPARQL updates --- src/ldp/http/SparqlUpdateBodyParser.ts | 2 +- test/integration/LpdHandlerOperations.test.ts | 6 +++--- .../ldp/http/SparqlUpdateBodyParser.test.ts | 20 +++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/ldp/http/SparqlUpdateBodyParser.ts b/src/ldp/http/SparqlUpdateBodyParser.ts index 45b2501a3..31941ac0b 100644 --- a/src/ldp/http/SparqlUpdateBodyParser.ts +++ b/src/ldp/http/SparqlUpdateBodyParser.ts @@ -31,7 +31,7 @@ export class SparqlUpdateBodyParser extends BodyParser { let algebra: Algebra.Operation; try { const sparql = await readableToString(toAlgebraStream); - algebra = translate(sparql, { quads: true }); + algebra = translate(sparql, { quads: true, baseIRI: metadata.identifier.value }); } catch (error: unknown) { this.logger.warn('Could not translate SPARQL query to SPARQL algebra', { error }); if (error instanceof Error) { diff --git a/test/integration/LpdHandlerOperations.test.ts b/test/integration/LpdHandlerOperations.test.ts index c086e44bc..57f5bd59e 100644 --- a/test/integration/LpdHandlerOperations.test.ts +++ b/test/integration/LpdHandlerOperations.test.ts @@ -75,7 +75,7 @@ describe('An integrated AuthenticatedLdpHandler', (): void => { 'POST', { 'content-type': 'text/turtle', 'transfer-encoding': 'chunked' }, [ ' .', - ' .' ], + ' .' ], ); expect(response.statusCode).toBe(201); expect(response._getData()).toHaveLength(0); @@ -89,8 +89,8 @@ describe('An integrated AuthenticatedLdpHandler', (): void => { requestUrl, 'PATCH', { 'content-type': 'application/sparql-update', 'transfer-encoding': 'chunked' }, - [ 'DELETE { }', - 'INSERT { }', + [ 'DELETE { }', + 'INSERT { }', 'WHERE {}', ], ); diff --git a/test/unit/ldp/http/SparqlUpdateBodyParser.test.ts b/test/unit/ldp/http/SparqlUpdateBodyParser.test.ts index 36cac4713..35b8b63bc 100644 --- a/test/unit/ldp/http/SparqlUpdateBodyParser.test.ts +++ b/test/unit/ldp/http/SparqlUpdateBodyParser.test.ts @@ -61,4 +61,24 @@ describe('A SparqlUpdateBodyParser', (): void => { [ 'DELETE DATA { }' ], ); }); + + it('accepts relative references.', async(): Promise => { + input.request = streamifyArray( + [ 'INSERT DATA { <#it> }' ], + ) as HttpRequest; + input.metadata.identifier = namedNode('http://test.com/my-document.ttl'); + const result = await bodyParser.handle(input); + expect(result.algebra.type).toBe(Algebra.types.DELETE_INSERT); + expect((result.algebra as Algebra.DeleteInsert).insert).toBeRdfIsomorphic([ quad( + namedNode('http://test.com/my-document.ttl#it'), + namedNode('http://test.com/p'), + namedNode('http://test.com/o'), + ) ]); + expect(result.binary).toBe(true); + expect(result.metadata).toBe(input.metadata); + + expect(await arrayifyStream(result.data)).toEqual( + [ 'INSERT DATA { <#it> }' ], + ); + }); });