This is an automated email from the ASF dual-hosted git repository.

andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git


The following commit(s) were added to refs/heads/main by this push:
     new 3563f5e26e GH-2578 Fix Quad.isTriple() handling in JenaTitanium
3563f5e26e is described below

commit 3563f5e26ef6d6c45f1e77a650efe55e285ef75c
Author: Ostrzyciel <ostrzycielnozyc...@gmail.com>
AuthorDate: Wed Jul 17 17:08:06 2024 +0200

    GH-2578 Fix Quad.isTriple() handling in JenaTitanium
    
    Issue: https://github.com/apache/jena/issues/2578
    
    This commit adds a `Quad.isTriple()` check when writing JSON-LD files, to 
cover the case where we have a "triple in quad". I've also added a test to 
check if this works. The test fails on the current main branch version of Jena.
    
    Undo stupid IDE autofix
---
 .../org/apache/jena/riot/system/JenaTitanium.java  |  2 +-
 .../org/apache/jena/system/TestJenaTitanium.java   | 27 ++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git 
a/jena-arq/src/main/java/org/apache/jena/riot/system/JenaTitanium.java 
b/jena-arq/src/main/java/org/apache/jena/riot/system/JenaTitanium.java
index 74931c77da..e6c7abbe5a 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/system/JenaTitanium.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/system/JenaTitanium.java
@@ -51,7 +51,7 @@ public class JenaTitanium {
             RdfResource predicate = resource(provider, labelMapping, 
quad.getPredicate());
             RdfValue object = nodeToValue(provider, labelMapping, 
quad.getObject());
 
-            if ( quad.isDefaultGraph() ) {
+            if ( quad.isTriple() || quad.isDefaultGraph() ) {
                 RdfTriple t = provider.createTriple(subject, predicate, 
object);
                 rdfDataset.add(t);
             }
diff --git 
a/jena-arq/src/test/java/org/apache/jena/system/TestJenaTitanium.java 
b/jena-arq/src/test/java/org/apache/jena/system/TestJenaTitanium.java
index 3629b45756..19de9c176d 100644
--- a/jena-arq/src/test/java/org/apache/jena/system/TestJenaTitanium.java
+++ b/jena-arq/src/test/java/org/apache/jena/system/TestJenaTitanium.java
@@ -20,6 +20,7 @@ package org.apache.jena.system;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
@@ -31,11 +32,13 @@ import com.apicatalog.rdf.io.error.RdfWriterException;
 import com.apicatalog.rdf.io.nquad.NQuadsWriter;
 
 import org.apache.jena.atlas.lib.StrUtils;
+import org.apache.jena.graph.NodeFactory;
 import org.apache.jena.riot.RDFParser;
 import org.apache.jena.riot.system.JenaTitanium;
 import org.apache.jena.riot.system.RiotLib;
 import org.apache.jena.sparql.core.DatasetGraph;
 import org.apache.jena.sparql.core.DatasetGraphFactory;
+import org.apache.jena.sparql.core.Quad;
 import org.apache.jena.sparql.sse.SSE;
 import org.apache.jena.sparql.util.IsoMatcher;
 import org.junit.Test;
@@ -106,4 +109,28 @@ public class TestJenaTitanium {
         DatasetGraph dsg2 = JenaTitanium.convert(rdfDataset, 
RiotLib.dftProfile());
         assertTrue(IsoMatcher.isomorphic(dsg1, dsg2));
     }
+
+    @Test public void convertDatasetWithNullGraph() throws IOException, 
RdfWriterException {
+        // .createTxnMem() returns an implementation that does not allow 
tripleInQuad
+        DatasetGraph dsg1 = DatasetGraphFactory.create();
+
+        // Add a triple in quad -- the graph term is set to null.
+        // The S, P, O terms can be whatever else.
+        // See: https://github.com/apache/jena/issues/2578
+        dsg1.add(Quad.create(
+                Quad.tripleInQuad,
+                NodeFactory.createBlankNode(),
+                NodeFactory.createBlankNode(),
+                NodeFactory.createBlankNode()
+        ));
+
+        RdfDataset rdfDataset = JenaTitanium.convert(dsg1);
+
+        // Try converting it back – it should not output any nulls.
+        DatasetGraph dsg2 = JenaTitanium.convert(rdfDataset, 
RiotLib.dftProfile());
+        dsg2.find().forEachRemaining(q->{
+            assertNotNull(q.getGraph());
+            assertTrue(q.isDefaultGraph());
+        });
+    }
 }

Reply via email to