Repository navigation
fix(transact): an empty write document has nothing to add - #2154
Conversation
An insert or upsert of `[]` or `{"@graph": []}` was refused as "an
object with only @id", which it doesn't hold. It is now refused as having
no triples, the message a graph insert already gives. An object with only
`@id` keeps its own message.
A sync treats `[]` as the empty document `{"@graph": []}` already is:
refused without allowEmpty, and with it, the graph is cleared. Graph
insert, sync and the sync guard share one test of an empty document.
aaj3f
left a comment
There was a problem hiding this comment.
@bplatz the shared is_empty_document is the right move, and I like that the message now says what's actually wrong with the payload. One headline note, and it's the must below, because I think it's a design matter more than a slip: the consolidation stops one copy short. The HTTP /sync route keeps its own empty-payload gate (fluree-db-server/src/routes/transact.rs:1464-1467) that only knows "@graph": [], and the consensus path behind it stages through the builder, which takes an empty JSON-LD document as meant. Before this PR [] never got that far because it failed to parse; now it parses as an empty sync and clears the graph with no allowEmpty. It's the same shape as a few we've seen lately (a second copy of a check that a change to the first copy doesn't reach, e.g. #2022, #2126, #2133), so I'd rather see that gate call the shared check than have its condition patched.
Claude-assisted review below:
✅ Approving to unblock you — with 1 thing that needs to be addressed before this merges.
Must address:
- A
[]sync over HTTP clears the graph withoutallowEmpty(fluree-db-server/src/routes/transact.rs:1464-1467; details inline atfluree-db-transact/src/parse/jsonld.rs:344). I reproduced it at this head:POST /v1/fluree/sync/<ledger>with body[]and noallowEmptyreturns200, writes a commit, and leaves the graph empty, for a named graph and for the default graph. With this PR's four source files put back tomain, the same request is a400and the graph is untouched. Fix path: have that gate call the shared check, and add a[]row tosync_route_contract's guard loop so the route is pinned.
Otherwise this reads well to me. [] now behaves the same on admin's sync guard, graph insert and the Python surface, an object with only @id keeps its own message (it_transact.rs:89-97), and the HTTP status doesn't move: both messages are a TransactError::Parse, which is still a 400. I also grepped our downstream consumers for the old "only @id" text and nothing matches on it, so the message change is safe there.
A couple of smaller things, both inline:
- 🟡 the "only @id" message still fires for documents that hold no
@idat all ([{}], a{"@context": …}with nothing else,{"@graph": [{}]}), and on the update surface"insert": []is refused with that same text while"delete": []is a no-op (jsonld.rs:439).
Adherence to repo commitments:
- Patterns/abstractions:
⚠️ one sharedis_empty_documentfor graph insert, sync andadmin's guard is the right shape; the server's/syncgate and the CLI's pre-check (fluree-db-cli/src/commands/graph_sync.rs:183-189) are two more copies of the same condition outside it. - Performance (speed first, memory second): ✔ a check on the document root at parse time; no engine or hot-path change. No performance-degradation risk.
- Deployment targets:
⚠️ the parse change is host-neutral; the leak is on thefluree-db-serverhost, whose route gates JSON-LD sync itself. Embedded callers ofsync_named_graph(and the Python package) go throughadmin's guard, which this PR updates. The new tests coveradmin's entry only. - Testing:
⚠️ []cases for insert/upsert, graph insert and sync onadmin's entry plus the Python tests, all of which ran in CI; nothing exercises the HTTP/syncroute with[], which is where it leaks. - Conventions: ✔ multi-line commit bodies;
sync.mdandserver-integration.mdupdated, though they now promise a refusal the HTTP route doesn't give until the must is in.
Verified locally at branch HEAD (e9618c06b): it_transact::an_empty_insert_or_upsert_has_nothing_to_add, it_sync_graph::empty_payload_requires_allow_empty, it_graph_scoped_writes::graph_insert_refusals and default_graph_sync_keeps_blank_nodes_and_guards_empty pass by name (and ran in CI's test job, with the Python tests in the python jobs); turning is_empty_document's [] arm off sends an_empty_insert_or_upsert_has_nothing_to_add, graph_insert_refusals and empty_payload_requires_allow_empty red; the HTTP [] sync reproduced as above, against the same probe on main's four source files and against the one-line gate fix; git merge-tree against #2142 and #2144 is clean.
Approving now so you can merge without waiting on another pass from me — just be sure the /sync gate is in before you do.
| /// JSON-LD named graph, which a graph write refuses.) | ||
| pub fn is_empty_document(json: &Value) -> bool { | ||
| match json { | ||
| Value::Array(nodes) => nodes.is_empty(), |
There was a problem hiding this comment.
🔴 Must address before merge: a [] sync over HTTP now clears the graph without allowEmpty.
The HTTP /sync route has its own empty-payload gate, fluree-db-server/src/routes/transact.rs:1464-1467, and it only recognizes "@graph": [] (its comment says it exists because "the consensus submission below calls the builder directly"). The consensus appliers stage a JsonLdGraphSync through sync_graph_payload(…, GraphPayload::JsonLd(body), false) (fluree-db-consensus/src/local.rs:156, fluree-db-consensus/src/raft/commit_worker.rs:647), and the builder doesn't guard an empty JSON-LD payload: parse_graph_payload only checks allow_empty for RDF text. Before this PR, [] failed in parse_data with the "only @id" error, so nothing was cleared. With this arm, parse_graph_insert turns it into an empty sync and the sync wave retracts the whole graph.
I reproduced it at this head with a scratch test next to sync_route_contract in fluree-db-server/tests/integration.rs: seed a graph through POST /v1/fluree/sync/<ledger>?graph=…, then POST the same route with body [] and no allowEmpty. It returns 200 with a new t, and a dry-run resync of the seed then reports asserted=3, i.e. the graph is empty. The default graph does the same. With jsonld.rs, parse/mod.rs, fluree-db-transact/src/lib.rs and tx_builder.rs put back to main, the same requests get a 400 and the graph is untouched. So it contradicts what docs/transactions/sync.md:59 and the PR's table now say ("refused without allowEmpty").
The fix I'd suggest is the consolidation the PR already describes, one copy further: have that gate call the shared check. I tried the smallest version of it (make GraphPayload::is_explicitly_empty_jsonld pub and use fluree_db_api::GraphPayload::JsonLd(&body_json).is_explicitly_empty_jsonld() at transact.rs:1464), and the scratch test goes back to a 400 with the allowEmpty message for both graphs, while sync_route_contract, sync_route_targets_the_default_graph and sync_route_accepts_rdf_bodies still pass. A [] row in sync_route_contract's guard loop (fluree-db-server/tests/integration.rs:4424-4446) would pin it, since the PR's new sync tests only exercise admin's entry. The CLI's pre-check (fluree-db-cli/src/commands/graph_sync.rs:183-189) is a fourth copy with the same @graph-only condition, so until the server gate changes, fluree sync --remote with a [] file reaches that route too (traced, not run); once the server refuses it, the CLI copy only decides which message you see, but routing it through the same check would leave one definition.
Commenting here because fluree-db-server/src/routes/transact.rs is not in this diff.
| "the payload has no triples; there is nothing to add".to_string(), | ||
| )); | ||
| } | ||
| let (verb, noun) = match txn_type { |
There was a problem hiding this comment.
🟡 Optional: the "only @id" message still fires where there's no @id, and "insert": [] in an update still gets it.
This is more of a question than a suggestion. With the new early return, the fallback message below is reached by any document whose templates come out empty and that isn't an empty envelope or array, so [{}], a {"@context": …} with nothing else, and {"@graph": [{}]} all still say "an object with only @id is not a valid insert" though none of them has an @id (I ran each at this head). The update surface has the same text at jsonld.rs:753, so {"where": …, "delete": …, "insert": []} is refused as an object with only @id, while "delete": [] right above it (jsonld.rs:718-722) is an explicit no-op.
Minimally the message could follow what's actually missing (say "only @id" only when a node has an @id and nothing else, and "nothing to add" otherwise). Whether "insert": [] should be a no-op like "delete": [] is a behavior call, so I'd leave that to you. It's minor and non-blocking, but since this PR is already about that message, if you agree it's right I'd rather see it folded in now than lost in the backlog.
The server's /sync route and the CLI kept their own empty-payload checks, which only knew `"@graph": []`. Now that `[]` parses as an empty sync, the route passed it on to the consensus path, which doesn't check allowEmpty for JSON-LD, and the graph was cleared. Both now use GraphPayload::is_explicitly_empty_jsonld, the check admin's sync entry point already uses.
…": [] An insert or upsert that yielded no triples was refused as "an object with only @id" unless it was `[]` or an empty `@graph`. So `[{}]`, `{"@graph": [{}]}` and a lone `@context` got that message with no `@id` in them. The message now depends on whether the expanded document names a node. In an update, `"insert": []` was refused while `"delete": []` was a no-op. It is now a no-op too. An insert template with no predicate or @type is still refused.
Brings #2142, #2144, #2154, #2155, #2156, #2158, #2159, #2165, #2166 and #2167 into next. Where main's RDF lists (#2159) meet next's RDF 1.2 links: - The list view walks an EdgeAnnotation's body; next has no AnnotationTarget. - Export marks annotated edges skipping list rows, and writes an rdf:reifies row as next's @reifies block through main's JsonLdNode, untranslated rows included. - The JSON-LD and import readers keep next's graph argument and triple-term spool records beside main's gap-free list positions and list chunking. - One collection writer: main's shared module, which both JSON-LD and Turtle use, replaces next's Turtle-only one, and now also keeps the labels of blank nodes inside triple terms, of reifiers, and of blank graph names. A reified cell keeps its triples while the well-formed tail after it folds, as JSON-LD's list conversion does; next's writer test now says so. - Hiding list members also turns off next's wildcard lane, which reads leaflets directly, and next's independent-scan count builds its right scan with the same hiding. Elsewhere: - List-predicate discovery opens a leaf ranged unless the store cannot read ranges, which next fetches whole. - GC reports main's walk stop reason without next's removed artifact cache directory. - Tests from main await next's async genesis_ledger, reload with without_storage_lock, and build stores without a cache directory.
Two small fixes from feedback on the Python package. Both apply to every surface, not only Python.
Empty write documents
An insert or upsert of an empty JSON-LD document (
[],{"@graph": []}) was refused with "an object with only @id is not a valid insert". The document has no such object. The error now reads "the payload has no triples; there is nothing to add", which a graph insert of an empty payload already gives. The same goes for any document that names no node, such as[{}]or a lone@context. An object with only@idkeeps its own message.An empty write stays an error: it is almost always a client bug. An update or upsert that stages nothing (a
WHEREthat matched no rows, or values already present) is still a no-op, not an error. In an update,"insert": []is now a no-op, as"delete": []already was.Sync now treats
[]the way it already treats{"@graph": []}:[]allowEmptyallowEmpty; with it, the graph is cleared[][],{"@graph": []},[{}]"insert": []Every sync guard now uses one check,
fluree_db_transact::is_empty_document: graph insert, sync,admin's sync entry point, the server's/syncroute and the CLI. Before,admin's guard tested any object with an empty@graph, while the parser required an envelope. The server route and the CLI recognized only"@graph": [].create(source=)docstringIt said bulk loading needs a file-backed connection; it works on
:memory:too. A test now covers both connection types.