Repository navigation
fix(json-ld): refuse an array or map @value with no @json or @vector type - #2408
Conversation
…type A value object whose @value is an array or a map stands for a literal only as a JSON literal or a vector. Without either type, from the object or from its term, the expander passed it through: a transaction then failed with "Unsupported @value type: Array [...]", and the adapter behind a bulk import and inline shapes dropped the value without an error. The expander now refuses such a value object, as JSON-LD 1.1 does an invalid value object, with a message that names the @type to add. Every JSON-LD reader goes through it, so all of them refuse the same documents. A @JSON term is unaffected (its whole value is the literal before the expander looks at @value), and a value object under a @vector term still takes the term's type.
aaj3f
left a comment
There was a problem hiding this comment.
@bplatz this is the right fix: one check in the expander so every reader refuses the same document, and your merge-into-next notes hold up (one caveat below).
My reading is that this moves bulk import from silently dropping an untyped array or map @value to refusing the whole import, so an import that worked on 4.2.7 can now fail. That sits beside #2227's lenient-import rule, but I think the line holds: an ill-typed literal is valid RDF we can keep as written, while this value object is invalid JSON-LD with no literal to keep. I'd like your confirmation that it's the intended decision.
Two small musts below: a map typed @vector still drops silently in a bulk import, and the compatibility entry needs the repair half we've been putting on main's data-changing JSON-LD fixes.
Full review below:
✅ Approving to unblock: please get the map-under-@vector drop and the repair sentence in before merging. Refusing in the expander is the right altitude, and both new tests fail with the check disabled (mutation-checked on a box); the two musts are a few lines each.
Before merging:
fluree-graph-json-ld/src/expand.rs:550: a map under@vectorpasses the expander, and a bulk import drops it silently.docs/reference/compatibility.md:471: add the repair half: an earlier import stored nothing for such a value, so write it again.
Notes:
- On merging into
next:git merge-treeagainstnextconflicts inexpand.rs(takenext's side, as you say) and in the integration tests (keep both), butcompatibility.mdand theexpand.rsunit tests auto-merge. So nothing stops a merger from missing your "drop the last assertion" step, after which that test fails onnext. Andmain's compatibility entry, kept verbatim there, says "the term it sits under" can type a value object@vector, whichnext's own entry from #2393 contradicts. OnnextI'd drop that clause, or the entry, since #2393's says it already. - This is one instance of a shape we keep fixing: one JSON-LD concept read by several readers, each with its own fallback, so one reader drops what another refuses (#2227 for ill-typed literals, #2393 on
next, #2414's reserved-predicate checks). Putting the check in the expander is the class fix for this concept. The adapter's own silent fallback is the one site left outside it, which is must 1. - The refusal names the fix but not the place: in an NDJSON import it doesn't say which line or node. #2414 threads a
pathintoparse_value_object, so whichever of the two lands second could put it in the message. - It merges cleanly onto current
main(with my #2227 and #2214) and alongside #2414 and #2425. Claude built all three merged on a box, and the new test passes there.
Adherence: Patterns ✔ · Performance ✔ · Deployment targets ✔ · Testing ✔ · Conventions ⚠️ · verification
- Patterns/abstractions: ✔ One check in the shared expander rather than one per reader. The adapter's silent fallback is the remaining per-reader path (must 1).
- Performance (speed first, memory second): ✔ Parser-only: one kind test per value object. No performance-degradation risk.
- Deployment targets: ✔ n/a: the expander runs the same in every host, and nothing here touches I/O, threads or time.
- Testing: ✔ Both tests run in CI (
grp_transact,fluree-graph-json-ldunit tests) and both fail with the check disabled. The map-under-@vectorcase has no test yet. - Conventions:
⚠️ Multi-line commits, CI green. The Behavior-changes entry lacks the repair half (must 2).
Verified at 56c71d37b: CI green on this SHA (test 16,199 passed incl. both new tests, clippy, fmt, testsuite-sparql, wasm32); on a box Claude ran eleven value-object shapes through a transaction and a bulk import at base and head, disabled the check (both new tests fail), ran merge-tree against next and current main, and built main with #2408, #2414 and #2425 merged.
Approving so you can merge once those two are in.
|
|
||
| /// Whether a value object typed `type_iri` may hold an array or a map: a JSON | ||
| /// literal, or a vector (an array only, which the vector parser checks). | ||
| fn holds_structured_value(type_iri: Option<&str>) -> bool { |
There was a problem hiding this comment.
Blocking: a map typed @vector still drops silently in a bulk import.
holds_structured_value admits @vector and f:embeddingVector for any structured @value, on the strength of the vector parser refusing a map (the comment above it). A transaction does refuse it ("Cannot coerce object to …embeddingVector"). But a bulk import never reaches a vector parser: the adapter's Value::Object(_) | Value::Array(_) arm keeps only an array under the vector type, and everything else falls to Ok(ProcessedValue::None) under "shouldn't happen".
Claude ran {"@id": "ex:s", "ex:v": {"@value": {"a": 1}, "@type": "@vector"}} (and the f:embeddingVector spelling) at this head: the transaction refuses it, and the bulk import succeeds with 0 flakes, the value gone. So that one document still splits the readers, the way the untyped ones did before this PR.
Either fix is small. Admitting the vector types for arrays only:
fn holds_structured_value(type_iri: Option<&str>, val: &JsonValue) -> bool {
match type_iri {
Some("@json" | fluree_vocab::rdf::JSON) => true,
Some("@vector" | fluree_vocab::fluree::EMBEDDING_VECTOR) => val.is_array(),
_ => false,
}
}Or (my preference, since it also covers whatever the expander lets through next) turning the adapter's fallback into an AdapterError::InvalidStructure naming the datatype, so no reader can drop a structured @value silently. next's copy of holds_structured_value from #2393 has the same gap, so whichever you pick wants to land on both lines. Commenting here because adapter.rs is not in this diff.
There was a problem hiding this comment.
Addressed in 49cb18d — both: the expander admits the vector types for an array only, and the adapter's fallback refuses a structured @value it has no literal form for instead of dropping it. The merge notes in the PR body now say to carry the array-only rule into next's copy.
|
|
||
| #### A JSON-LD array or map `@value` needs a `@json` or `@vector` type | ||
|
|
||
| A value object whose `@value` is an array or a map is refused unless it, or the term it sits under, types it `@json` or `@vector`: `{"@value": [0.5, 0.25]}` under an untyped term has no literal to stand for. A transaction refused it already, with a message that named no fix, and a bulk import and inline shapes dropped the value without an error. Each now refuses it with a message that names the `@type` to add. |
There was a problem hiding this comment.
Blocking (D1's floor): the entry needs its repair half.
The entry says what a new document gets. It doesn't say what an earlier release left behind: a bulk import or inline shape that held such a value stored nothing for it, and nothing in the ledger records the drop. The data-changing JSON-LD fixes we've shipped on main carry a Behavior-changes entry and a repair recipe, and here the recipe is a sentence. Something like:
| A value object whose `@value` is an array or a map is refused unless it, or the term it sits under, types it `@json` or `@vector`: `{"@value": [0.5, 0.25]}` under an untyped term has no literal to stand for. A transaction refused it already, with a message that named no fix, and a bulk import and inline shapes dropped the value without an error. Each now refuses it with a message that names the `@type` to add. | |
| A value object whose `@value` is an array or a map is refused unless it, or the term it sits under, types it `@json` or `@vector`: `{"@value": [0.5, 0.25]}` under an untyped term has no literal to stand for. A transaction refused it already, with a message that named no fix, and a bulk import and inline shapes dropped the value without an error. Each now refuses it with a message that names the `@type` to add. A ledger built by an earlier bulk import holds nothing for such a value, and nothing records that it was dropped: to restore one, write it again with the `@type` it needs. |
| _ => None, | ||
| }), | ||
| }; | ||
| if (val.is_array() || val.is_object()) && !holds_structured_value(type_iri.as_deref()) { |
There was a problem hiding this comment.
This is more of a nit than a must. Because the new check runs before the @id branch, {"@value": ["ex:a"], "@type": "@id"} now gets "needs "@type": "@json" (or "@vector" for an array)" where it got "a value object typed @id names an IRI, so its @value must be a string" (Claude compared the two on a box). The old message names the real fix. Skipping the structured check when type_iri is @id keeps it. It's minor and non-blocking, but if you agree it's right, I'd rather see it folded in now than lost in the backlog.
…-value-needs-type
… adapter The expander let a value object typed `@vector` (or `f:embeddingVector`) hold any array or map, on the strength of the vector parser refusing a map. A transaction reaches that parser; a bulk import does not. The adapter keeps only an array under the vector type, and its fallback for any other array or map `@value` returned no value, so the import stored nothing for the map and raised no error. The expander now admits a vector type for an array only, so a map typed as a vector fails every reader with the same message, naming the type to use. The adapter's fallback is an error too: a structured `@value` it has no literal form for, in expanded input that never passed the expander, is refused with the datatype it carried rather than dropped. Tests cover the map under `@vector`, under `f:embeddingVector` and under a `@vector` term in the expander; the adapter's refusal on expanded input; and both spellings through a transaction and a bulk import.
… dropped The entry said what a new document gets but not what an earlier release left behind: a bulk import or inline shape that held an untyped array or map `@value`, or a map typed `@vector`, stored nothing for it, and nothing in the ledger records the drop. It now says so, and that the value is restored by writing it again with the `@type` it needs. It also says `@vector` types an array only, as the expander now checks.
The structured-value check ran before the `@id` branch, so
`{"@value": ["ex:a"], "@type": "@id"}` was told it needs
`"@type": "@JSON"`, where it used to be told that a value object typed
`@id` names an IRI and its `@value` must be a string. The second names
the real fix. The check now leaves a value object typed `@id` to that
branch.
|
From the review summary:
Confirmed, it's intended: an ill-typed literal is valid RDF an import keeps as written (#2227), while this value object is invalid JSON-LD with no literal to keep. The PR body now says so.
Addressed in the PR body's "Merging into
Noted on #2414, so whichever lands second adds the path: #2414 (comment) |
Brings #2408 and #2366. #2408 and next's #2393 each add the structured-value check to the expander. Next's is kept, as #2408's merge notes say: a value object takes no type from its term there, so the check reads only the object's own `@type`. #2408's two later changes are carried into it: a vector type admits an array only (a map typed `@vector` was dropped by a bulk import), and a value object typed `@id` keeps its own message that its `@value` must be a string IRI. The adapter's refusal of a structured `@value` with no literal form merged as is. Both expander tests are kept; #2408's loses its last assertion, that a value object under a `@vector` term takes the term's type, which next refuses. Both integration tests are kept. #2408's compatibility entry drops "or the term it sits under", which would contradict next's "A JSON-LD value object is taken as written".
A value object whose
@valueis an array or a map can only be a literal as a JSON literal or a vector. When neither the object nor its term types it@jsonor@vector, the expander passed it through, and the readers disagreed:Unsupported @value type: Array [...], which names no fix.The expander now refuses it, as JSON-LD 1.1 does an invalid value object:
invalid @value value: a value object whose @value is an array or a map needs "@type": "@json" (or "@vector" for an array). Every JSON-LD reader goes through the expander, so they all refuse the same documents.A map typed
@vector(orf:embeddingVector) split the readers the same way: a transaction refused it, and a bulk import stored nothing. The expander now admits a vector type for an array only, and the bulk-import adapter refuses any structured@valueit has no literal form for, rather than dropping it. A value object typed@idkeeps its own message, that its@valuemust be a string IRI.This refuses a whole bulk import that an earlier release completed while dropping such values. That is intended: unlike an ill-typed literal, which is valid RDF an import keeps as written (#2227), this value object is invalid JSON-LD with no literal to keep.
Unchanged:
@jsonterm: its whole value becomes the literal before the expander looks at@value.@vectorterm: it takes the term's type.wherepattern: the query parser reads its values itself.Merging into
nextFound in review of #2393, which makes the same change on
next(8e5d2f6). The two will conflict whenmainis merged intonext. Steps the merge does not do by itself are marked manual:fluree-graph-json-ld/src/expand.rs: both addholds_structured_valueand the check inparse_value_object. Keepnext's. There a value object takes no type from its term, so the check reads only the object's own@type, and its message says so.main's check also reads the term's type, whichnextno longer gives a value object.next's copy, which has the same gaps.holds_structured_valueadmits@vectorandf:embeddingVectorfor an array only (it takes the value), and the check skips a value object typed@id, which keeps the@idbranch's own message.fluree-graph-json-ld/src/adapter.rs: the fallback that refuses a structured@valuewith no literal form auto-merges. Keep it.fluree-graph-json-ld/src/expand.rstests: keep both tests. They auto-merge, so this one is easy to miss. Manual: drop the last assertion ofstructured_value_object_needs_a_json_or_vector_type(a value object under a@vectorterm takes the term's type). Onnextthat document is refused. The other assertions match"@type": "@json"or"must be a string", which both lines' messages contain once the@idskip is carried over.fluree-db-api/tests/it_jsonld_value_object_coercion.rs: keep both integration tests.docs/reference/compatibility.md: keep this entry, which auto-merges. Manual: drop its "or the term it sits under" clause. Undernext's "A JSON-LD value object is taken as written" entry (fix(json-ld): a value object is taken as written #2393), a term no longer types a value object, and the clause would contradict it.Tests
it_jsonld_value_object_coercion::an_untyped_structured_value_object_is_refused: an array and a map@valueunder an untyped term, and a map typed@vectororf:embeddingVector, through a transaction and through a bulk import. With the expander check and the adapter refusal both removed, the import succeeds.expand::tests::structured_value_object_needs_a_json_or_vector_type: the refused shapes, including a map under@vector,f:embeddingVectoror a@vectorterm; the accepted ones (@json,@vectorandf:embeddingVectoron an array, and an array under a@vectorterm); and an array or map typed@id, which keeps the IRI message.adapter::tests::a_structured_value_with_no_literal_form_is_refused: expanded input that never passed the expander, with a map typed as a vector and an array typedxsd:integer, is refused rather than dropped.