Skip to content

Commit

Permalink
fix: Support empty PATCH requests
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Jun 10, 2021
1 parent a6371b0 commit ded263a
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 10 deletions.
109 changes: 101 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"rdf-terms": "^1.5.1",
"redis": "^3.0.2",
"redlock": "^4.2.0",
"sparqlalgebrajs": "^2.5.1",
"sparqlalgebrajs": "^3.0.0",
"sparqljs": "^3.1.2",
"streamify-array": "^1.0.1",
"url-join": "^4.0.1",
Expand Down
12 changes: 11 additions & 1 deletion src/ldp/permissions/SparqlPatchPermissionsExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class SparqlPatchPermissionsExtractor extends PermissionsExtractor {
}

private isSupported(op: Algebra.Operation): boolean {
if (op.type === Algebra.types.DELETE_INSERT) {
if (this.isDeleteInsert(op) || this.isNop(op)) {
return true;
}
if (op.type === Algebra.types.COMPOSITE_UPDATE) {
Expand All @@ -57,7 +57,14 @@ export class SparqlPatchPermissionsExtractor extends PermissionsExtractor {
return op.type === Algebra.types.DELETE_INSERT;
}

private isNop(op: Algebra.Operation): op is Algebra.Nop {
return op.type === Algebra.types.NOP;
}

private needsAppend(update: Algebra.Operation): boolean {
if (this.isNop(update)) {
return false;
}
if (this.isDeleteInsert(update)) {
return Boolean(update.insert && update.insert.length > 0);
}
Expand All @@ -66,6 +73,9 @@ export class SparqlPatchPermissionsExtractor extends PermissionsExtractor {
}

private needsWrite(update: Algebra.Operation): boolean {
if (this.isNop(update)) {
return false;
}
if (this.isDeleteInsert(update)) {
return Boolean(update.delete && update.delete.length > 0);
}
Expand Down
6 changes: 6 additions & 0 deletions src/storage/patch/SparqlUpdatePatchHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export class SparqlUpdatePatchHandler extends PatchHandler {
// Verify the patch
const { source, identifier, patch } = input;
const op = (patch as SparqlUpdatePatch).algebra;

// In case of a NOP we can skip everything
if (op.type === Algebra.types.NOP) {
return [];
}

this.validateUpdate(op);

return this.applyPatch(source, identifier, op);
Expand Down
13 changes: 13 additions & 0 deletions test/unit/ldp/permissions/SparqlPatchPermissionsExtractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ describe('A SparqlPatchPermissionsExtractor', (): void => {
await expect(result).rejects.toThrow('Can only determine permissions of a PATCH with DELETE/INSERT operations.');
});

it('requires nothing for NOP operations.', async(): Promise<void> => {
const operation = {
method: 'PATCH',
body: { algebra: factory.createNop() },
} as unknown as Operation;
await expect(extractor.handle(operation)).resolves.toEqual({
read: false,
append: false,
write: false,
control: false,
});
});

it('requires append for INSERT operations.', async(): Promise<void> => {
const operation = {
method: 'PATCH',
Expand Down
7 changes: 7 additions & 0 deletions test/unit/storage/patch/SparqlUpdatePatchHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ describe('A SparqlUpdatePatchHandler', (): void => {
await expect(handler.canHandle(input)).rejects.toThrow(NotImplementedHttpError);
});

it('handles NOP operations by not doing anything.', async(): Promise<void> => {
await handle('');
expect(source.getRepresentation).toHaveBeenCalledTimes(0);
expect(converter.handleSafe).toHaveBeenCalledTimes(0);
expect(source.setRepresentation).toHaveBeenCalledTimes(0);
});

it('handles INSERT DATA updates.', async(): Promise<void> => {
await handle(fullfilledDataInsert);
expect(await basicChecks(startQuads.concat(
Expand Down

0 comments on commit ded263a

Please sign in to comment.