Repository navigation
json-ld: a value object under an @id or @vocab term stays a literal - #2297
Conversation
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
left a comment
There was a problem hiding this comment.
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
@vocabstring coercion,@json, term@language, null terms,@index; it keeps theTypeValue::Idfallback and doesn't touchedge_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(()), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| // 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Review items are in 3d69033: the two inline threads above, the |
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.
I ran into this while inserting an extraction service's JSON-LD answer. Its
@contextcoerces 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/@vocabcoercion 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/@vocabturns something into an IRI. A map with@valueinstead 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@idterm should be the literal"TWD", not a node reference.The cause is
parse_value_objectinfluree-graph-json-ld/src/expand.rs:426(onmain), which falls back to the term's type mapping when the object has no@type, and that includesTypeValue::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@vocabterms 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 -fof these documents). JSON-LD queries already read a value object under an@idterm as a literal inwhere(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/@vocabterm because the base edge would become a ref while the reifiedf:reifiesObjectstayed 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@jsonterm 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 anxsd:integerterm), which W3C expand testc024says it shouldn't, and it applies the context's default@languageto 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 awherevalue 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
fix/jsonld-context-conformance) rewrites the same block ofparse_value_object. It fixes the@languagecase (an object's own@languagenow beats the term's type), but{"@value": "TWD"}with no@languagestill becomes<TWD>there, since its fallback keepsSome(TypeValue::Id) => Some("@id")(the new API test fails on fix(json-ld): honor @vocab and @json coercion, term @language, null terms and @index maps #2283's head too, withisLiteralcoming backfalse).git merge-treeshows exactly one conflict, in that block ofexpand.rs. The resolution is just to take fix(json-ld): honor @vocab and @json coercion, term @language, null terms and @index maps #2283's side of the hunk: the removal of theTypeValue::Idarm sits outside the conflict and merges on its own. I checked the resolved merge: thefluree-graph-json-ldandfluree-db-transacttests and the new API test all pass.fix/jsonld-native-literal-types) only touchesadapter.rsand merges cleanly with this.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-@idvalue objects under both@idand@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/@vocabwrites the same insert templates as under an untyped term);accepts_value_form_annotation_under_id_typed_termfluree-db-api(grp_transact):value_object_under_id_or_vocab_term_stores_a_literal(insert and upsert, checked with SPARQLisLiteral/LANG, plus a JSON-LD query with the same context)All three new tests fail on
mainwithout the fix (isLiteralcomes backfalsefor 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/@vocabterm directly,c024andtn02are the closest (value objects keep only their own@type/@language).Gates:
cargo fmt --all --check; clippy-D warnings --all-targetsonfluree-graph-json-ld,fluree-db-transact,fluree-db-api;cargo testforfluree-graph-json-ldandfluree-db-transact(all targets) andfluree-db-api'sgrp_transact,grp_misc,grp_import,grp_queryandharness_coverage.Added in review
{"@value": "TWD"}under an@id/@vocabterm now stores the literal where it stored a reference. A ledger that already ingested such documents keeps the references it has.@id/@vocabterms. An annotated value object under such a term skipped the guard, so with a context@languagethe 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).@valuetyped@idis 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