Skip to content

fix(json-ld): a value object is taken as written - #2393

Merged
bplatz merged 6 commits into
nextfrom
fix/jsonld-value-object-as-written
Nov 15, 2026
Merged

bplatz merged 6 commits into
nextfrom
fix/jsonld-value-object-as-written

Conversation

@bplatz

@bplatz bplatz commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A JSON-LD value object took the context's default @language and the datatype of the term it sits under. JSON-LD 1.1 gives both only to a bare value (Value Expansion); a value object is taken as written. So {"@value": "obj"}, the standard way to keep one string out of the default language, was stored as "obj"@en, and {"@value": "5"} under a term typed xsd:integer was stored as the integer 5.

Fix

  • Expander (parse_value_object in fluree-graph-json-ld/src/expand.rs): a value object takes only its own @type and @language. Every JSON-LD reader goes through it: transactions, bulk import, rdf::parse, inline shapes.
  • JSON-LD where (fluree-db-query/src/parse/node_map.rs): the query parser gave value objects their term's datatype too (it never applied the default language). Fixing only the write side would have broken the round trip: {"@value": "5"} written as a string, then looked up as an integer. A constant @value now takes no datatype from its term. A variable @value ({"@value": "?v", "@t": "?t"}) keeps it, as a bare "?v" does.
  • Edge annotations (fluree-db-transact/src/parse/edge_annotations.rs): reject_context_coercion_on_annotated_literal refused an annotated value object under a typed term or a default language, because the base edge took the coercion and the reifier bundle did not. Both now read the value as written, so those documents are accepted. The refusal stays for a @json term, which still makes the whole value one JSON literal, as the spec says.

Behavior changes

In docs/reference/compatibility.md, under "A JSON-LD value object is taken as written (breaking)", with a repair update under "Data written by an earlier release that reads differently":

  • A document that writes value objects under a default language or a typed term now writes plain strings, or numbers typed by their value.
  • Data already stored keeps the language or datatype it was given. Re-inserting the same document adds a second value, an upsert replaces it, and a delete written with the same value object no longer removes it.
  • {"@value": [...]} under a @vector term needs its own "@type": "@vector". No test, fixture or doc uses the untyped form; a bare array under the term is unchanged. Every JSON-LD reader refuses it, as it does any array or map @value without a @json or @vector type: a bulk import and rdf::parse used to drop one without an error.

While writing the note: a JSON-LD where string with no language matches a stored string in any language, so {"@value": "obj"} still finds an old "obj"@en (SPARQL's "obj" matches only xsd:string). This PR doesn't change that, and the note says so.

Tests

  • it_jsonld_value_object_coercion::value_object_takes_neither_the_default_language_nor_its_terms_datatype: the issue's two documents end to end. SPARQL reads back each value's datatype and language, and a JSON-LD where written with the document's context finds each value.
  • it_edge_annotations::edgekey_roundtrip_value_object_under_term_coercion: an annotated value object under a typed term and under a default language. The reifier names the base edge, and the edge holds a plain string.
  • it_jsonld_value_object_coercion::an_array_value_object_with_no_type_is_refused: a transaction and rdf::parse both refuse an untyped array @value under a @vector term.
  • Unit tests in the expander, the query parser and the annotation lowering. The annotation tests that pinned the refusals now check acceptance; the node-local context tests use a @json term, which still reaches the guard.

Each new test fails with its half of the fix reverted.

Fixes #2356
Follow-up: #2407

The array and map @value refusal (8e5d2f6) is also on main in #2408, worded for main, where a value object still takes its term's type. When main is merged into next, keep this PR's version; #2408's body lists the hunks.

A value object took the context's default @language and the datatype of
its term, which JSON-LD 1.1 gives only to a bare value. {"@value": "obj"}
could not keep a string out of the default language, and {"@value": "5"}
under a term typed xsd:integer was stored as the integer.

A JSON-LD where reads a constant value object the same way, so a pattern
written like the insert matches what it stored. A variable @value keeps
its term's datatype, as a bare variable does.

Annotated value objects under a typed term or a default language were
refused because the base edge took those and the reifier bundle did not.
Both now take the value as written, so the refusal is kept only for a
@JSON term, which still makes the whole value one JSON literal.
@bplatz bplatz added bug Something isn't working as expected breaking-change Backwards-incompatible change; drives the Breaking Changes release-notes section labels Nov 14, 2026
@bplatz
bplatz requested a review from a team November 14, 2026 05:11

@aaj3f aaj3f left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bplatz nice fix, and I like that the where side and the annotation guard moved with the expander, so a pattern written like the insert still finds what it wrote.

My reading is that a typed term now types only bare values, so {"@value": "abc"} under an xsd:integer term becomes a string where it was refused. I think that's right by JSON-LD 1.1, but I'd like your confirmation it's intended. And this lands in 5.0 rather than main, unlike #2283, which I read as right since there's no general repair for old data; I'd like your confirmation of that too.

Separately, this is the sixth JSON-LD value rule where the where parser had to be changed by hand to match the write side (eac9be949, 5c98bd1d2, 6e4d0061b, 14695d293, 40c14ba73, this PR). One shared value-to-term function would make the next one a one-place change, though I'm not saying this PR needs to do that. One docs must below.

Full review below:


✅ Approving to unblock: please get the repair-recipe fix at compatibility.md:531 in before merging. This is a careful fix, @bplatz: the expander, the where parser and the annotation guard change together, and the upgrade entry is unusually thorough (every bullet in it reproduced on a box). The one must is a docs precision fix: the typed-term repair update would also rewrite integers this PR doesn't change.

Before merging:

  1. docs/reference/compatibility.md:531: scope the typed-term repair to string @values, and give number @values their own sentence.

Notes:

  • SPARQL parity gets better here. On the box ledger, every constant value object now returns the rows the SPARQL literal written the same way returns ({"@value": "5"} and "5", {"@value": 5} and 5, {"@value": "2024-01-01"} and "2024-01-01"), where at the merge-base the two string ones under typed terms didn't. What's left is the gap the body names: a JSON-LD string with no language matches any language, and SPARQL's "obj" matches xsd:string only.
  • Merge order: this merges clean with each of #2396, #2397 and #2400, in either order, and adds nothing to #2227's eventual merge into next (that conflict is next vs main). Claude ran #2396's W3C suite with this merged in: no register entry flips and the suite stays green.
Adherence: Patterns ✔ · Performance ✔ · Deployment targets ✔ · Testing ✔ · Conventions ⚠️ · verification
  • Patterns/abstractions: ✔ fixes the rule in the one expander every JSON-LD reader shares, with a single value_object_dtc helper for both where value-object sites; the shared-IR model holds and results now match SPARQL for constant value objects.
  • Performance (speed first, memory second): ✔ parser-only, once per pattern, no per-row work; a constant value object under a typed term just loses a dtc, and no pattern changes lane (the dtc-gated fast paths need a variable object, and variables keep theirs). No performance-degradation risk.
  • Deployment targets: ✔ n/a: pure parsing with no I/O, threads, clocks or caches; the same code runs on every host, and the wasm32/wasm-smoke jobs passed on the head.
  • Testing: ✔ unit tests in the expander, the query parser and the annotation lowering, plus two integration tests wired into grp_transact and grp_misc; all ran and passed in CI on the head. Claude's mutations of each half turn the PR's own tests red.
  • Conventions: ⚠️ multi-line commit, clippy and fmt green, a thorough breaking-change entry with a repair section; the repair recipe needs the must above.

Verified at a67c54901c: CI run 43560288056 green (test: 17509 run, 17509 passed, 49 skipped; every new test passes by name). On a box, Claude ran one probe at the merge-base and the head (26 write shapes, 14 JSON-LD and 7 SPARQL where shapes, re-insert/upsert/delete over old data) and four mutations of the fix (reverting the expander half, the where half or the annotation guard each turns the PR's own unit and integration tests red; dropping the variable carve-out turns the parser's unit test red). git merge-tree clean against #2396, #2397, #2400, next and main.

Approving so you can merge once the recipe is scoped, but maybe worth folding in the three smaller notes too.

Comment thread docs/reference/compatibility.md Outdated
- **Values written through a `@vocab`, `@json` or `@index` term** (see [JSON-LD term definitions coerce values as JSON-LD 1.1 defines](#json-ld-term-definitions-coerce-values-as-json-ld-11-defines)). They keep the shape the earlier release stored, and the same context now reads and writes the new one, so a `where` pattern, a delete or an upsert written through the term no longer reaches them. A string stored through a `@vocab` term becomes the IRI with one update, here for terms relative to `http://example.org/`: `DELETE { ?s ex:status ?old } INSERT { ?s ex:status ?new } WHERE { ?s ex:status ?old FILTER(isLiteral(?old)) BIND(IRI(CONCAT("http://example.org/", STR(?old))) AS ?new) }`. Values stored through a `@json` or an `@index` term have no general rewrite (a split array does not record which values it held together): delete them by the property's full IRI and write the documents again.
- **Facts an update stored under a fallback namespace code.** A `where` constant in a namespace the ledger had not seen yet was stored under a fallback code, so once the namespace got its own code, a lookup by the IRI missed those facts. Every IRI an update writes is now stored under its canonical code. Facts already stored the fallback way stay split from the IRI's other facts; there is no migration.
- **Whole doubles written by a client that keeps the `.0`** (see [A JSON-LD number is typed by its value](#a-json-ld-number-is-typed-by-its-value-breaking)). The same client now writes integers for those values, so the property holds both. To make the stored values integers as well, convert them once, here for `ex:price`: `DELETE { ?s ex:price ?old } INSERT { ?s ex:price ?new } WHERE { ?s ex:price ?old FILTER(DATATYPE(?old) = xsd:double && ?old = FLOOR(?old) && ABS(?old) < 1e21) BIND(xsd:integer(?old) AS ?new) }`. A whole double becomes the integer (merging with an equal integer already stored), and `2.5` stays a double. To keep the property's values doubles instead, type them `xsd:double` in the client's context.
- **Values written as value objects under a default `@language` or a typed term** (see [A JSON-LD value object is taken as written](#a-json-ld-value-object-is-taken-as-written-breaking)). They hold the language or datatype the earlier release gave them, which the same document no longer writes. A stored value does not record whether it came from a value object or a bare value, so there is no general rewrite: convert a property only where every value it holds in that language or datatype was written as a value object. Here for `ex:t`, written under `"@language": "en"`: `DELETE { ?s ex:t ?old } INSERT { ?s ex:t ?new } WHERE { ?s ex:t ?old FILTER(LANG(?old) = "en") BIND(STR(?old) AS ?new) }`. For `ex:n`, written under a term typed `xsd:integer`, the same update on `ex:n` with `FILTER(DATATYPE(?old) = xsd:integer)` in place of the `LANG` test. To keep the stored values instead, give the value objects their language or datatype.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking (docs): the typed-term repair update would also rewrite integers this PR doesn't change.

The ex:n recipe keeps every value with DATATYPE(?old) = xsd:integer and rewrites it to STR(?old). But a value object with a JSON number, {"@value": 5}, is the integer 5 both before and after this PR, so those values already match what the document writes. The caveat ("convert a property only where every value it holds in that language or datatype was written as a value object") doesn't tell the two apart.

Claude ran this on a box: under "n": {"@id": "ex:n", "@type": "xsd:integer"}, {"@value": 5} reads back as xsd:integer at the merge-base and at the head. So a property written entirely with {"@value": 5} meets the caveat as written, and the update turns its correct integers into the string "5", which the same document then no longer matches on a delete or in a where, and a re-insert adds the integer beside it. The number case also changed in the other direction and isn't covered here: {"@value": 5} under xsd:decimal was a decimal and is now an integer, and {"@value": 1.5} is now an xsd:double.

Scoping the recipe to string @values and giving numbers their own sentence would cover both, something like:

Suggested change
- **Values written as value objects under a default `@language` or a typed term** (see [A JSON-LD value object is taken as written](#a-json-ld-value-object-is-taken-as-written-breaking)). They hold the language or datatype the earlier release gave them, which the same document no longer writes. A stored value does not record whether it came from a value object or a bare value, so there is no general rewrite: convert a property only where every value it holds in that language or datatype was written as a value object. Here for `ex:t`, written under `"@language": "en"`: `DELETE { ?s ex:t ?old } INSERT { ?s ex:t ?new } WHERE { ?s ex:t ?old FILTER(LANG(?old) = "en") BIND(STR(?old) AS ?new) }`. For `ex:n`, written under a term typed `xsd:integer`, the same update on `ex:n` with `FILTER(DATATYPE(?old) = xsd:integer)` in place of the `LANG` test. To keep the stored values instead, give the value objects their language or datatype.
- **Values written as value objects under a default `@language` or a typed term** (see [A JSON-LD value object is taken as written](#a-json-ld-value-object-is-taken-as-written-breaking)). They hold the language or datatype the earlier release gave them, which the same document no longer writes. A stored value does not record whether it came from a value object or a bare value, so there is no general rewrite: convert a property only where every value it holds in that language or datatype was written as a value object with a string `@value`. Here for `ex:t`, written under `"@language": "en"`: `DELETE { ?s ex:t ?old } INSERT { ?s ex:t ?new } WHERE { ?s ex:t ?old FILTER(LANG(?old) = "en") BIND(STR(?old) AS ?new) }`. For `ex:n`, written as `{"@value": "5"}` under a term typed `xsd:integer`, the same update on `ex:n` with `FILTER(DATATYPE(?old) = xsd:integer)` in place of the `LANG` test. A number or boolean `@value` is typed by its value now, not by its term (`{"@value": 5}` under a term typed `xsd:decimal` was a decimal and is now an integer), and an integer `@value` under an `xsd:integer` term is unchanged. To keep the stored values instead, give the value objects their language or datatype.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 5df1562

Comment thread docs/reference/compatibility.md Outdated

This applies to every JSON-LD reader: a transaction, a bulk import, [`rdf::parse`](rdf-documents.md) and inline shapes. A JSON-LD `where` reads a constant value object the same way, so a pattern written like the insert matches what it stored. A variable written as a value object (`{"@value": "?v", "@t": "?t"}`) keeps its term's datatype, as `"?v"` does.

Two documents that were refused are accepted, and one that was accepted is refused:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional (docs). This is more of a docs-completeness note than a suggestion. The list says two refused documents are now accepted, but the box run shows a third kind: a value object whose @value doesn't read as its term's datatype. {"@value": "abc"} under an xsd:integer term was refused (Cannot parse 'abc' as integer) and is now the string "abc"; {"@value": true} under the same term is now a boolean; and {"@value": 5} under an xsd:string term (Cannot coerce number 5 to …#string) is now the integer.

That's right by the spec, but it means a typed term no longer catches a bad value written as a value object, which some users may be leaning on today. It also slips past #2227's ill-typed-literal refusal once both land, since the literal is no longer typed at all. Maybe one more bullet:

Suggested change
Two documents that were refused are accepted, and one that was accepted is refused:
Three kinds of document that were refused are accepted, and one that was accepted is refused:
- A value object whose `@value` does not read as its term's datatype is stored as written: `{"@value": "abc"}` under a term typed `xsd:integer` is the string `"abc"`. It was refused, so the typed term no longer checks a value object.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 42b5369

Comment thread docs/reference/compatibility.md Outdated
- Under a term typed `xsd:integer`, `{"@value": "5"}` is the string `"5"`. It was the integer `5`. A number takes its datatype from its value: `{"@value": 5}` under a term typed `xsd:decimal` is the integer `5`.
- A bare value is unchanged: `"plain"` takes the default language, and `"5"` under the term is the integer.

This applies to every JSON-LD reader: a transaction, a bulk import, [`rdf::parse`](rdf-documents.md) and inline shapes. A JSON-LD `where` reads a constant value object the same way, so a pattern written like the insert matches what it stored. A variable written as a value object (`{"@value": "?v", "@t": "?t"}`) keeps its term's datatype, as `"?v"` does.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional (docs). The variable sentence is true, but it may be worth saying what follows from it. On a ledger holding both the integer 5 and the string "5" on ex:n, Claude found that {"n": "?v"} and {"n": {"@value": "?v"}} return only the integer, at the merge-base and at the head (same for an xsd:date term). So after this PR, a document that writes {"@value": "5"} and a query that reads "n": "?v" through the same context don't see each other, which I think will surprise people more than the constant case does.

I agree with keeping the variable's meaning, since it leaves {"@value": "?v", "@t": "?t"} history queries as they were. Just a sentence, something like:

Suggested change
This applies to every JSON-LD reader: a transaction, a bulk import, [`rdf::parse`](rdf-documents.md) and inline shapes. A JSON-LD `where` reads a constant value object the same way, so a pattern written like the insert matches what it stored. A variable written as a value object (`{"@value": "?v", "@t": "?t"}`) keeps its term's datatype, as `"?v"` does.
This applies to every JSON-LD reader: a transaction, a bulk import, [`rdf::parse`](rdf-documents.md) and inline shapes. A JSON-LD `where` reads a constant value object the same way, so a pattern written like the insert matches what it stored. A variable written as a value object (`{"@value": "?v", "@t": "?t"}`) keeps its term's datatype, as `"?v"` does, so it matches only values of that datatype: under a term typed `xsd:integer` it does not return the string that `{"@value": "5"}` writes.

Minor and non-blocking, but I'd rather see it folded in now than lost in the backlog.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in f826e3c

Comment thread docs/reference/compatibility.md Outdated
Two documents that were refused are accepted, and one that was accepted is refused:

- An annotated value object (`@annotation`) under a typed term or a default language annotates the value as written. It was refused unless it named its own `@type` or `@language`.
- `{"@value": [...]}` under a term typed `@vector` needs its own `"@type": "@vector"`, and is refused without it. A bare array under the term is unchanged.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional (error message). The refusal holds, but the message a user gets doesn't point at the fix. Claude ran {"@value": [0.5, 0.25]} under a term typed @vector on the head, and the insert fails with Parse error: Unsupported @value type: Array [Number(0.5), Number(0.25)] (the same for the annotated form). That comes from the untyped arm of parse_literal_value_with_meta (fluree-db-transact/src/parse/jsonld.rs:2436), which this PR now routes these documents to.

Since the doc promises a refusal that tells you what to add, maybe give arrays their own arm there:

Value::Array(_) => Err(TransactError::Parse(
    "a value object whose @value is an array needs its own \"@type\": \"@vector\" \
     (or \"@json\"); a value object takes no datatype from its term"
        .to_string(),
)),

It's minor, but if you agree it's right, I'd rather see it folded in now than lost in the backlog. (Commenting here because fluree-db-transact/src/parse/jsonld.rs is not in this diff.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 8e5d2f6

The refusal moved into the expander rather than a new arm in the transaction parser. A bulk import, rdf::parse and inline shapes don't reach that parser: their adapter dropped an untyped array @value without an error, so under a @vector term this PR had turned a stored vector into a silent drop for them. The expander now refuses any array or map @value without a @json or @vector type, using your message, for every reader.

…type

A value object takes no datatype from its term, so `{"@value": [0.5,
0.25]}` under a term typed `@vector` reaches the readers untyped. The
expander passed it through: a transaction then failed with "Unsupported
@value type: Array [...]", and the adapter behind a bulk import,
`rdf::parse` and inline shapes dropped the value without an error. The
same held for any array or map `@value` with no `@json` or `@vector`
type.

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 `where` pattern does not
run the expander on its values.
The repair update for values a value object wrote under a typed term
rewrote every value of the term's datatype to its string. An integer
@value under an xsd:integer term is an integer before and after the
change, so the update would also have turned those correct values into
strings, which the same document then no longer matches.

The recipe now applies only where the property's values were written as
string @values, and says STR gives the stored value's canonical form. A
number or boolean @value gets its own sentence: it is typed by its value,
so it is unchanged under a matching term, and a whole number under an
xsd:decimal term is now an integer and a fractional one a double.
A variable under a typed term keeps the term's datatype, written bare or
as a value object, so it matches only values of that datatype. Now that
a value object takes no datatype from its term, a query read through the
same context as a document that writes {"@value": "5"} under an
xsd:integer term does not return the string it stored. Say so where the
entry describes how a where clause reads value objects.
A value object takes no datatype from its term, so one whose @value does
not read as the term's datatype is stored as written: {"@value": "abc"}
under a term typed xsd:integer is the string "abc", where it was refused.
List it with the other documents whose acceptance changed, say that a
value object's own @type and a bare value are still checked, and point to
sh:datatype for holding a property to one datatype.
@bplatz

bplatz commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

please get the repair-recipe fix at compatibility.md:531 in before merging

Addressed in 5df1562

{"@value": "abc"} under an xsd:integer term becomes a string where it was refused … I'd like your confirmation it's intended.

Intended: JSON-LD 1.1 gives a term's @type only to bare values. A value object's own @type is still checked, and so is a bare value under the term. Documented in 42b5369, with sh:datatype as the way to hold a property to one datatype.

this lands in 5.0 rather than main … I'd like your confirmation of that too.

Confirmed: it changes what an existing document writes, and data already written has no general repair, so it belongs in the major.

@bplatz

bplatz commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

this is the sixth JSON-LD value rule where the where parser had to be changed by hand to match the write side … One shared value-to-term function would make the next one a one-place change

Follow-up: #2407

@bplatz
bplatz merged commit e448092 into next Nov 15, 2026
20 checks passed
@bplatz
bplatz deleted the fix/jsonld-value-object-as-written branch November 15, 2026 12:29
bplatz added a commit that referenced this pull request Nov 15, 2026
#2393 refuses a value object whose @value is an array or a map and that
is not typed @JSON or @vector. That is what ter29 and ter51 expect
(`{"@value": ["foo"]}`, directly and through a @value alias), in both
the expand and toRdf suites, so their four register entries go.
bplatz added a commit that referenced this pull request Nov 18, 2026
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".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change Backwards-incompatible change; drives the Breaking Changes release-notes section bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants