Skip to content

json-ld: a value object under an @id or @vocab term stays a literal - #2297

Merged
bplatz merged 3 commits into
mainfrom
fix/jsonld-value-object-under-id-coercion
Nov 12, 2026
Merged

bplatz merged 3 commits into
mainfrom
fix/jsonld-value-object-under-id-coercion

Conversation

@aaj3f

@aaj3f aaj3f commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

I ran into this while inserting an extraction service's JSON-LD answer. Its @context coerces a term to "@type": "@id", and one of its values is written as a value object, e.g. "unit": {"@value": "TWD"}. The same answer as TriG says the literal "TWD", but our expander turns the JSON-LD into the relative IRI <TWD>. So the two serializations of one answer land as different data.

Per JSON-LD 1.1 API, the term's @id/@vocab coercion only ever applies to a string value: the Expansion Algorithm hands scalars to Value Expansion (step 4.3), and Value Expansion's steps 1–2 are the only place @id/@vocab turns something into an IRI. A map with @value instead goes through step 13.4.7 and comes out as a value object built from its own @type/@language (step 15). So {"@value": "TWD"} under an @id term should be the literal "TWD", not a node reference.

The cause is parse_value_object in fluree-graph-json-ld/src/expand.rs:426 (on main), which falls back to the term's type mapping when the object has no @type, and that includes TypeValue::Id. A few consequences of that, all fixed here:

  • {"@value": "TWD"} → <TWD> (relative IRI)
  • {"@value": "TWD", "@language": "en"} → also <TWD>, with the language tag silently dropped
  • {"@value": 5} → {"@id": ""} (expand.rs:445), i.e. an empty IRI

@vocab terms didn't have this problem for value objects, but I've pinned them in the tests too. An explicit {"@value": "ex:x", "@type": "@id"} still writes a reference, since we accept that form on purpose.

This lives in the shared expander, so it covers inserts, upserts and bulk import of JSON-LD (and so fluree insert -f of these documents). JSON-LD queries already read a value object under an @id term as a literal in where (the query parser has its own term handling), so with this change an insert and a query written with the same context finally agree. There's a test for exactly that.

One related change: reject_context_coercion_on_annotated_literal (fluree-db-transact/src/parse/edge_annotations.rs:151) refused an annotated value object under an @id/@vocab term because the base edge would become a ref while the reified f:reifiesObject stayed a literal. Now both are the same literal, so that refusal no longer guards anything. I flipped its test to assert the lowering goes through. A datatype or @json term still gets refused there, as before.

Not in this PR

The same fallback also gives a value object the term's datatype IRI (e.g. {"@value": "1"} under an xsd:integer term), which W3C expand test c024 says it shouldn't, and it applies the context's default @language to a value object with no @language, which Value Expansion doesn't do either. I left both alone on purpose: the JSON-LD query parser currently applies the term datatype to a where value object the same way, so fixing only the write side would make an insert and a query with the same context disagree. That one needs a decision about query semantics (whether {"@value": "?x"} under a typed term should keep constraining the datatype), so I'd rather not make it unilaterally inside a bug fix.

Relation to #2283 and #2212

On merge order, I'd suggest #2212, then #2283, then this one rebased on top (I'll take the conflict), but I'm happy to go first instead if that's easier, @bplatz, since the conflict is the same one-hunk resolution either way.

Tests

  • fluree-graph-json-ld: value_object_under_id_or_vocab_term_stays_a_literal (plain, @language, @type, numeric and explicit-@id value objects under both @id and @vocab, the single-object shape, and a bare string still coerced)
  • fluree-db-transact: value_object_under_id_or_vocab_term_inserts_a_literal (each value object under @id/@vocab writes the same insert templates as under an untyped term); accepts_value_form_annotation_under_id_typed_term
  • fluree-db-api (grp_transact): value_object_under_id_or_vocab_term_stores_a_literal (insert and upsert, checked with SPARQL isLiteral/LANG, plus a JSON-LD query with the same context)

All three new tests fail on main without the fix (isLiteral comes back false for the insert, for example). The repo doesn't run the W3C JSON-LD expand suite, so I checked the relevant manifest entries by hand: no expand test puts a value object under an @id/@vocab term directly, c024 and tn02 are the closest (value objects keep only their own @type/@language).

Gates: cargo fmt --all --check; clippy -D warnings --all-targets on fluree-graph-json-ld, fluree-db-transact, fluree-db-api; cargo test for fluree-graph-json-ld and fluree-db-transact (all targets) and fluree-db-api's grp_transact, grp_misc, grp_import, grp_query and harness_coverage.

Added in review

  • Stored data changes. An insert of {"@value": "TWD"} under an @id/@vocab term now stores the literal where it stored a reference. A ledger that already ingested such documents keeps the references it has.
  • The default-language guard reaches @id / @vocab terms. An annotated value object under such a term skipped the guard, so with a context @language the base edge took the language and its annotation did not. It is refused as under an untyped term (rejects_default_language_on_annotated_string_under_an_id_or_vocab_term).
  • A non-string @value typed @id is refused. {"@value": 5, "@type": "@id"} expanded to a reference to the empty IRI; it is now a 400 (a_value_object_typed_id_with_a_non_string_value_is_refused, and a case in the expander test).

🤖 Generated with Claude Code

https://claude.ai/code/session_016CjYmNj3BkNuqovk8Bsfcc

JSON-LD 1.1 coerces only a string to an IRI under a term typed "@id" or
"@vocab" (Expansion Algorithm step 4.3, Value Expansion step 1). A value
object takes its datatype and language only from its own entries. The
expander gave a value object under an "@id" term the term's coercion, so
{"@value": "TWD"} became the relative IRI <TWD>, a language-tagged value
object lost its tag the same way, and a numeric @value became the empty
IRI. Insert, upsert and bulk import all share this expander.

An explicit {"@value": ..., "@type": "@id"} still writes a reference.

The annotated-literal guard no longer refuses a value object under an
"@id" or "@vocab" term, since the base edge and the reified object are
now the same literal.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016CjYmNj3BkNuqovk8Bsfcc

@bplatz bplatz 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.

Core fix is right, and the new tests fail with the TypeValue::Id arm restored. Approving, but please look at the inline comments before merging — the first one is a real hole this PR opens.

Also:

  • No labels, so this lands under "Other Changes" — needs bug + area:transact.
  • Worth one line in the description that this changes what an insert stores; ledgers that already ingested such documents keep the refs.
  • No overlap with #2283 (it adds @vocab string coercion, @json, term @language, null terms, @index; it keeps the TypeValue::Id fallback and doesn't touch edge_annotations.rs). Only the one-hunk conflict you already described. Fine for this to go first.

TypeValue::Id => "@id".to_string(),
TypeValue::Vocab => "@vocab".to_string(),
TypeValue::Json => "@json".to_string(),
TypeValue::Id | TypeValue::Vocab => return Ok(()),

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.

return Ok(()) exits the whole function, so the default-language check below never runs for @id/@vocab terms. With "@language": "fr" in the context and the predicate typed @id, an annotated {"@value": "chat"} now lowers cleanly and the base edge expands to "chat"@fr; the same document under an untyped term is rejected with "relies on @context language coercion". Before this PR the type arm rejected it.

Fall through instead of returning (only build the error for Iri/Json), and add a test pairing an @id term with a default @language.

The function's doc comment above ("applies a predicate's term-coerced @type…") also wants the @id/@vocab exception.

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.

Addressed in 3d69033. The guard falls through for @id / @vocab terms, with a test pairing each with a default @language, and the doc comment names the exception.

Comment thread fluree-graph-json-ld/src/expand.rs Outdated
// Get type from entry if not explicit. A term's `@id` or `@vocab`
// coercion never applies here: JSON-LD 1.1 coerces only a string value
// to an IRI (Value Expansion), and a value object is a literal. Only an
// explicit `"@type": "@id"` on the object makes it a reference.

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.

Non-blocking: the explicit form with a non-string value, {"@value": 5, "@type": "@id"}, still produces {"@id": ""} a few lines down. The description lists the empty-IRI case as fixed, but only the term-coerced route is. Pre-existing; it should be an error. A follow-up is fine.

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.

Addressed in 3d69033 rather than left for a follow-up: a non-string @value typed @id is now refused (400 through the API).

…ct-under-id-coercion

# Conflicts:
#	fluree-graph-json-ld/src/expand.rs
… and a non-string @value typed @id is refused

An annotated value object under a term typed `@id` or `@vocab` skipped
the default-language guard, so with a context `@language` the base edge
took the language while its annotation did not. The guard now runs for
those terms as it does for an untyped one.

`{"@value": 5, "@type": "@id"}` expanded to a reference to the empty
IRI. Only a string names an IRI, so any other value is refused.
@bplatz bplatz added bug Something isn't working as expected area:transact Staging, commit, import/bulk-import, novelty, retraction semantics labels Nov 12, 2026
@bplatz

bplatz commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Review items are in 3d69033: the two inline threads above, the bug and area:transact labels, and an "Added in review" section in the description noting that this changes what an insert stores. Main is merged in; the one conflict in expand.rs took #2283's structure without the TypeValue::Id fallback.

@bplatz
bplatz merged commit 45f621c into main Nov 12, 2026
19 checks passed
@bplatz
bplatz deleted the fix/jsonld-value-object-under-id-coercion branch November 12, 2026 04:06
bplatz added a commit that referenced this pull request Nov 12, 2026
Brings in #2157, #2226, #2297, #2314 and #2331.

Conflicts:

- eval/helpers.rs (#2226): the filter verdict cache key takes main's store
  instance id and next's u32 graph id, and main's test builds its stores
  with next's `BinaryIndexStore::empty()`.
- grp_transact.rs lists both sides' test modules.
- #2157 changed `retract` and `drop_branch` on every nameservice backend,
  and the nested-ledger prefixes a sweep excludes. next replaced those
  operations with the fenced record store, so the nameservice, sweep and
  drop-test hunks take next's side here. The next commit carries the fix
  to next's operations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:transact Staging, commit, import/bulk-import, novelty, retraction semantics bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants