fix: Do not generate empty INSERT graph.

Fixes https://github.com/solid/community-server/issues/383
This commit is contained in:
Ruben Verborgh 2020-12-02 21:39:38 +01:00
parent c2b189184b
commit 0ecbffa885
2 changed files with 22 additions and 1 deletions

View File

@ -238,7 +238,9 @@ export class SparqlDataAccessor implements DataAccessor {
if (triples) { if (triples) {
// This needs to be first so it happens before the insert // This needs to be first so it happens before the insert
updates.unshift(this.sparqlUpdateDeleteAll(name)); updates.unshift(this.sparqlUpdateDeleteAll(name));
insert.push(this.sparqlUpdateGraph(name, triples)); if (triples.length > 0) {
insert.push(this.sparqlUpdateGraph(name, triples));
}
} }
return { return {

View File

@ -190,6 +190,25 @@ describe('A SparqlDataAccessor', (): void => {
])); ]));
}); });
it('overwrites the data and metadata when writing an empty resource.', async(): Promise<void> => {
metadata = new RepresentationMetadata('http://test.com/container/resource',
{ [RDF.type]: [ toNamedNode(LDP.Resource) ]});
const empty = guardedStreamFrom([]);
await expect(accessor.writeDocument({ path: 'http://test.com/container/resource' }, empty, metadata))
.resolves.toBeUndefined();
expect(fetchUpdate).toHaveBeenCalledTimes(1);
expect(fetchUpdate.mock.calls[0][0]).toBe(endpoint);
expect(simplifyQuery(fetchUpdate.mock.calls[0][1])).toBe(simplifyQuery([
'DELETE WHERE { GRAPH <http://test.com/container/resource> { ?s ?p ?o. } };',
'DELETE WHERE { GRAPH <meta:http://test.com/container/resource> { ?s ?p ?o. } };',
'INSERT DATA {',
' GRAPH <meta:http://test.com/container/resource> { <http://test.com/container/resource> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/ldp#Resource>. }',
' GRAPH <http://test.com/container/> { <http://test.com/container/> <http://www.w3.org/ns/ldp#contains> <http://test.com/container/resource>. }',
'}',
]));
});
it('removes all references when deleting a resource.', async(): Promise<void> => { it('removes all references when deleting a resource.', async(): Promise<void> => {
metadata = new RepresentationMetadata('http://test.com/container/', metadata = new RepresentationMetadata('http://test.com/container/',
{ [RDF.type]: [ toNamedNode(LDP.Resource), toNamedNode(LDP.Container) ]}); { [RDF.type]: [ toNamedNode(LDP.Resource), toNamedNode(LDP.Container) ]});