apache / apache/jena

When serializing Dataset to JSONLD the @context is populated only from default graph

Open
#2,031 1 comment 0 reactions 0 assignees View on GitHub
enhancement help wanted
Dominant language
Java
Stars
1.4k
Forks
712
Avg merge
15h 41m
Merged PRs (30d)
53

Description

### Version

4.4.0

### What happened?

Hi, right now I'm using Jena version `4.4.0` and experiencing a bug in serializing `Dataset` with `RDFFormat.JSONLD` where `@context` is populated only with properties from default graph.
The test.trig file from which `Dataset` is initialized contains this:
```
_:object_1 a _:objectClass2 ;
_:compacted.

{
_:object_2 a _:objectClass2 ;
_:un .
}
```
To serialize the `Dataset` I use the following code:
```
public class Test {
public void test() throws IOException {
Dataset dataset = createDataset();

ByteArrayOutputStream out = new ByteArrayOutputStream();
RDFDataMgr.write(out, dataset, RDFFormat.JSONLD);

System.out.println(out);
}

private Dataset createDataset() throws IOException {
Dataset dataset = DatasetFactory.create();
ClassLoader classloader = Thread.currentThread().getContextClassLoader();

try (InputStream is = classloader.getResourceAsStream("test.trig");) {
assert is != null;
RDFDataMgr.read(dataset, is, Lang.TRIG);
}
return dataset;
}
}
```

This results in the following JSONLD where `compacted.property` from default graph is in the `@context` and `uncompacted.property` is not :
```
{
"@graph" : [ {
"@id" : "_:b0",
"@type" : "_:b2",
"compacted.property" : "_:b1"
}, {
"@graph" : [ {
"@id" : "_:b3",
"@type" : "_:b2",
"http://big.prefix.com#uncompacted.property" : {
"@id" : "_:b4"
}
} ],
"@id" : "urn:test-graph"
} ],
"@context" : {
"compacted.property" : {
"@id" : "http://big.prefix.com#compacted.property",
"@type" : "@id"
}
}
}
```

I took a look around the code and it seems like the problem is in the method `JsonLDWriter.getJsonldContext`, particularly this line:
```
// if no ctx passed via jenaContext, create one in order to have localnames as keys for properties
ctx = createJsonldContext(dataset.getDefaultGraph(), prefixMap, addAllPrefixesToContextFlag(jenaContext)) ;
```
The context is being created only for default graph. This method seems to also check for user provided context a little bit higher up but I for the life of me couldn't figure out how to provide it :(

I upgraded to the latest version (4.9.0) but it seems like the `@context` is not being initialized at all, so all the properties are in full form:
```
{
"@graph": [
{
"@id": "_:b0",
"http://big.prefix.com#compacted.property": {
"@id": "_:b1"
},
"@type": "_:b2"
},
{
"@id": "urn:test-graph",
"@graph": [
{
"@id": "_:b3",
"http://big.prefix.com#uncompacted.property": {
"@id": "_:b4"
},
"@type": "_:b2"
}
]
}
]
}
```
I took a look around and it seems that in version `4.4.0` the `RDFFormat.JSONLD` is set to `RDFFormat.JSONLD_COMPACT_PRETTY` variant but in the version `4.4.9` it is set to `RDFFormat.JSONLD_PRETTY` and the `RDFFormat.JSONLD_COMPACT_PRETTY` is marked as deprecated in favor of JSONLD11. But JSONLD11 seems to not offer compact pretty variant, if I'm blind please point me to the right direction.

Btw, in version `4.9.0` if serializing with deprecated `RDFFormat.JSONLD_COMPACT_PRETTY` the bug still reproduces the same as in `4.4.0`, context is formed only for the default graph.

A bunch of logic in in my application depends on the properties of JSONLD being compacted so I would appreciate any help with this :D
1. Is there any way to make `@context` auto-populated in version `4.9.0`?
2. Or perhaps a way I could provide `@context` myself

I could probably figure out a way to fix the `JsonLDWriter.getJsonldContext` method and do a pull request. But I don't know if compacted JSONLD is deprecated and it is pointless

I attached a reproducer project archive:
[jena-reproducer.tar.gz](https://github.com/apache/jena/files/12841699/jena-reproducer.tar.gz)

### Are you interested in making a pull request?

Yes

Contributor guide

Open the contributing guide

Research direction

Start by running the attached reproducer with Apache Jena 4.9.0 and compare RDFFormat.JSONLD with RDFFormat.JSONLD_COMPACT_PRETTY. Then inspect JsonLDWriter.getJsonldContext and the RDFFormat variants mentioned in the report. Done means documenting or correcting how @context handles properties in both the default and named graphs, including whether a user-provided context is supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.