Skip to content

Commit 6abba07

Browse files
[Agent: Claude] fix(patch): N3-Patch emits full predicate IRIs on context-free docs (task-6 review #1)
- applyN3Patch detects whether the target document carries an @context and passes the verdict into insertTriple: no @context -> the predicate key is the full IRI (compactPredicate skipped). On the JavaScriptSolidServer#7 Turtle-family path (stored bytes -> expanded JSON-LD, no @context) the compacted keys ("rdf:type", "dc:title") re-parsed as literal scheme-IRIs <rdf:type> — silent corruption of every common-vocabulary insert. - Legacy JSON-LD-stored docs (which DO have an @context resolving those prefixes) keep the existing compaction behavior byte-identically. - deleteTriple needs NO symmetric fix: its match already tries the raw key first (key === predicate; expanded docs carry full-IRI keys), verified by repro and pinned by the new DELETE test. patch.where is parsed but never applied, so no compaction assumption there. - New tests pin both paths TRIPLE-LEVEL via the n3 Parser on an .nt-stored doc (N-Triples has no prefix mechanism, so a Turtle round-trip can't launder the corrupt CURIE back to the right IRI and mask the bug). Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent e2c20f4 commit 6abba07

2 files changed

Lines changed: 89 additions & 3 deletions

File tree

‎src/patch/n3-patch.js‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@ export function applyN3Patch(document, patch, baseUri) {
275275
// Clone the document
276276
let doc = JSON.parse(JSON.stringify(document));
277277

278+
// task-6 review #1: only a document WITH an @context can resolve prefixed
279+
// keys ("dc:title") back to vocabulary IRIs. On a context-free document
280+
// (the #7 Turtle-family path projects stored bytes to EXPANDED JSON-LD)
281+
// a compacted key re-parses as a literal scheme-IRI (<dc:title>) — silent
282+
// corruption — so inserts must emit the full predicate IRI as the key.
283+
// Legacy JSON-LD-stored docs (which do carry @context) keep compaction.
284+
const compact = doc !== null && typeof doc === 'object' && '@context' in doc;
285+
278286
// Handle @graph array or single object
279287
const isGraph = Array.isArray(doc['@graph']);
280288
let nodes = isGraph ? doc['@graph'] : [doc];
@@ -286,7 +294,7 @@ export function applyN3Patch(document, patch, baseUri) {
286294

287295
// Then apply inserts
288296
for (const triple of patch.inserts) {
289-
nodes = insertTriple(nodes, triple, baseUri);
297+
nodes = insertTriple(nodes, triple, baseUri, compact);
290298
}
291299

292300
// Reconstruct document
@@ -337,8 +345,10 @@ function deleteTriple(nodes, triple, baseUri) {
337345

338346
/**
339347
* Insert a triple into JSON-LD nodes
348+
* `compact` — whether the enclosing document can resolve prefixed keys
349+
* (i.e. carries an @context); without one the key must be the full IRI.
340350
*/
341-
function insertTriple(nodes, triple, baseUri) {
351+
function insertTriple(nodes, triple, baseUri, compact = true) {
342352
const { subject, predicate, object } = triple;
343353

344354
// Find or create the subject node
@@ -355,7 +365,7 @@ function insertTriple(nodes, triple, baseUri) {
355365
}
356366

357367
// Add the predicate-object
358-
const predicateKey = compactPredicate(predicate);
368+
const predicateKey = compact ? compactPredicate(predicate) : predicate;
359369
const objectValue = convertToJsonLd(object);
360370

361371
if (subjectNode[predicateKey]) {

‎test/lws-patch-conformance.test.js‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { test } from 'node:test';
55
import assert from 'node:assert/strict';
66
import http from 'node:http';
7+
import * as storage from '../src/storage/filesystem.js';
78
import { startTestServer, stopTestServer, request, createTestPod, getPodToken, getBaseUrl, assertStatus } from './helpers.js';
89

910
const N3_INSERT = `@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@@ -26,6 +27,81 @@ test('N3 Patch on a verbatim-stored .ttl applies and stays Turtle (#7)', async (
2627
assert.equal(back.headers.get('content-type').split(';')[0], 'text/turtle'); // stored format preserved
2728
});
2829

30+
test('N3 Patch INSERT emits full predicate IRIs on a context-free stored doc (review #1)', async (t) => {
31+
// #7 projects verbatim-stored bytes through EXPANDED JSON-LD (no @context)
32+
// before applyN3Patch runs. insertTriple used to run the predicate through
33+
// compactPredicate unconditionally, producing keys like "rdf:type" that
34+
// re-parse (no @context to resolve them) as literal scheme-IRIs
35+
// <rdf:type> instead of the real vocabulary IRI.
36+
//
37+
// Storage format is deliberately application/n-triples, NOT text/turtle:
38+
// Turtle-family write-back declares the SAME 7 default prefixes
39+
// (COMMON_PREFIXES) that compactPredicate hardcodes, so a Turtle
40+
// round-trip coincidentally "launders" the corrupt CURIE-shaped string
41+
// back to the correct IRI on re-parse and hides the bug. N-Triples has no
42+
// prefix mechanism — a corrupt predicate stays a literal wrong IRI, which
43+
// is what makes this the right stored form to pin the regression against.
44+
// Assert TRIPLE-LEVEL via the n3 Parser — a regex/substring check would
45+
// miss this class of bug (and would be fooled by the Turtle-masking above).
46+
//
47+
// PUT itself doesn't accept application/n-triples (not in SUPPORTED_INPUT),
48+
// so the seed file is written directly via storage.write — the same
49+
// seeding pattern test/lws-serve-nt-nq.test.js and
50+
// test/lws-conditional-406.test.js use for filesystem/git-sourced .nt/.nq.
51+
await startTestServer({ lws: true, conneg: true });
52+
t.after(stopTestServer);
53+
await createTestPod('patchiris');
54+
await storage.write('/patchiris/d.nt', Buffer.from('<#s> <http://ex/p> "v" .\n'));
55+
const INSERT_COMMON = `@prefix solid: <http://www.w3.org/ns/solid/terms#>.
56+
_:p a solid:InsertDeletePatch;
57+
solid:inserts {
58+
<#s> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://ex/Thing>.
59+
<#s> <http://purl.org/dc/terms/title> "Title".
60+
}.`;
61+
const r = await request('/patchiris/d.nt', { method: 'PATCH', auth: 'patchiris',
62+
headers: { 'Content-Type': 'text/n3' }, body: INSERT_COMMON });
63+
assert.ok([200, 204].includes(r.status), `expected 2xx, got ${r.status}`);
64+
65+
const back = await request('/patchiris/d.nt', { headers: { Accept: 'application/n-triples' }, auth: 'patchiris' });
66+
const nt = await back.text();
67+
const { Parser } = await import('n3');
68+
const quads = new Parser({ baseIRI: new URL('/patchiris/d.nt', getBaseUrl()).href }).parse(nt);
69+
const predicates = quads.map(q => q.predicate.value);
70+
assert.ok(predicates.includes('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
71+
`expected the full rdf:type IRI among stored predicates, got ${JSON.stringify(predicates)}`);
72+
assert.ok(predicates.includes('http://purl.org/dc/terms/title'),
73+
`expected the full dc:title IRI among stored predicates, got ${JSON.stringify(predicates)}`);
74+
});
75+
76+
test('N3 Patch DELETE removes a common-vocabulary triple on a context-free stored doc (review #1, symmetric)', async (t) => {
77+
// Pins the symmetric (delete-side) path: a genuine dc:title triple
78+
// (never touched by insertTriple's compaction) must still be matched
79+
// and removed by predicate on an expanded/context-free document.
80+
// application/n-triples again, so there is no prefix-declaration masking
81+
// to launder a match failure into an accidental pass. Seeded via
82+
// storage.write — see the note on the INSERT test above.
83+
await startTestServer({ lws: true, conneg: true });
84+
t.after(stopTestServer);
85+
await createTestPod('patchdelvoc');
86+
await storage.write('/patchdelvoc/d.nt', Buffer.from(
87+
'<#s> <http://ex/p> "v" .\n<#s> <http://purl.org/dc/terms/title> "Title" .\n'));
88+
const DELETE_COMMON = `@prefix solid: <http://www.w3.org/ns/solid/terms#>.
89+
_:p a solid:InsertDeletePatch;
90+
solid:deletes { <#s> <http://purl.org/dc/terms/title> "Title". }.`;
91+
const r = await request('/patchdelvoc/d.nt', { method: 'PATCH', auth: 'patchdelvoc',
92+
headers: { 'Content-Type': 'text/n3' }, body: DELETE_COMMON });
93+
assert.ok([200, 204].includes(r.status), `expected 2xx, got ${r.status}`);
94+
95+
const back = await request('/patchdelvoc/d.nt', { headers: { Accept: 'application/n-triples' }, auth: 'patchdelvoc' });
96+
const nt = await back.text();
97+
const { Parser } = await import('n3');
98+
const quads = new Parser({ baseIRI: new URL('/patchdelvoc/d.nt', getBaseUrl()).href }).parse(nt);
99+
const predicates = quads.map(q => q.predicate.value);
100+
assert.ok(!predicates.includes('http://purl.org/dc/terms/title'),
101+
`dc:title should have been deleted, got ${JSON.stringify(predicates)}`);
102+
assert.ok(predicates.includes('http://ex/p'), 'unrelated triple should survive the delete');
103+
});
104+
29105
test('JSON Merge Patch applies to a stored JSON-LD doc (P1, RFC 7386)', async (t) => {
30106
await startTestServer({ lws: true, conneg: true });
31107
t.after(stopTestServer);

0 commit comments

Comments
 (0)