From 59718c8a3adaa3d4746a3e4a8bd5e7e1023b731b Mon Sep 17 00:00:00 2001 From: Tglman Date: Mon, 15 Jul 2024 17:57:53 +0100 Subject: [PATCH] fix: make sure that toJson of list of ids get expanded, issue #10264 --- .../sql/functions/text/OSQLMethodToJSON.java | 21 +++++++--- .../OSelectStatementExecutionTest.java | 38 +++++++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/orientechnologies/orient/core/sql/functions/text/OSQLMethodToJSON.java b/core/src/main/java/com/orientechnologies/orient/core/sql/functions/text/OSQLMethodToJSON.java index 382443a270b..c639daa9a38 100644 --- a/core/src/main/java/com/orientechnologies/orient/core/sql/functions/text/OSQLMethodToJSON.java +++ b/core/src/main/java/com/orientechnologies/orient/core/sql/functions/text/OSQLMethodToJSON.java @@ -19,6 +19,7 @@ import com.orientechnologies.common.collection.OMultiValue; import com.orientechnologies.orient.core.command.OCommandContext; import com.orientechnologies.orient.core.db.record.OIdentifiable; +import com.orientechnologies.orient.core.id.ORecordId; import com.orientechnologies.orient.core.record.ORecord; import com.orientechnologies.orient.core.record.impl.ODocument; import com.orientechnologies.orient.core.sql.executor.OResult; @@ -56,19 +57,27 @@ public Object execute( } final String format = iParams.length > 0 ? ((String) iParams[0]).replace("\"", "") : null; - - if (iThis instanceof OResult) { - iThis = ((OResult) iThis).toElement(); + if (iThis instanceof ORecordId) { + ORecord rec = iContext.getDatabase().load((ORecordId) iThis); + if (rec != null) { + return rec.toJSON(format); + } } - if (iThis instanceof ORecord) { + if (iThis instanceof OResult) { + if (format != null) { + return ((OResult) iThis).toJSON(); + } else { + ((OResult) iThis).toElement().toJSON(format); + } + } else if (iThis instanceof ORecord) { final ORecord record = (ORecord) iThis; - return iParams.length == 1 ? record.toJSON(format) : record.toJSON(); + return record.toJSON(format); } else if (iThis instanceof Map) { final ODocument doc = new ODocument().fromMap((Map) iThis); - return iParams.length == 1 ? doc.toJSON(format) : doc.toJSON(); + return doc.toJSON(format); } else if (OMultiValue.isMultiValue(iThis)) { diff --git a/core/src/test/java/com/orientechnologies/orient/core/sql/executor/OSelectStatementExecutionTest.java b/core/src/test/java/com/orientechnologies/orient/core/sql/executor/OSelectStatementExecutionTest.java index 90d914cea9a..db823e8fb57 100755 --- a/core/src/test/java/com/orientechnologies/orient/core/sql/executor/OSelectStatementExecutionTest.java +++ b/core/src/test/java/com/orientechnologies/orient/core/sql/executor/OSelectStatementExecutionTest.java @@ -4588,6 +4588,44 @@ public void testMapToJson() { } } + @Test + public void testOutJson() { + db.command("create class SimpleV extends V ").close(); + db.command("create vertex SimpleV set name = 'foo'").close(); + db.command("create vertex SimpleV set name = 'foo1'").close(); + db.command( + "create edge e from (select from SimpleV where name='foo') to (select from SimpleV" + + " where name='foo1')") + .close(); + try (OResultSet rs = + db.query( + "select name, outs.tojson() as json from ( select name, out() as outs from SimpleV" + + " where name='foo') ")) { + Assert.assertTrue(rs.hasNext()); + OResult item = rs.next(); + Assert.assertFalse(((String) item.getProperty("json")).contains("null")); + } + } + + @Test + public void testOutJsonEmb() { + db.command("create class SimpleV extends V ").close(); + db.command("create vertex SimpleV set name = 'foo'").close(); + db.command("create vertex SimpleV set name = 'foo1'").close(); + db.command( + "create edge e from (select from SimpleV where name='foo') to (select from SimpleV" + + " where name='foo1')") + .close(); + try (OResultSet rs = + db.query( + "select name, outs as json from ( select name, out().toJson() as outs from SimpleV" + + " where name='foo') ")) { + Assert.assertTrue(rs.hasNext()); + OResult item = rs.next(); + Assert.assertFalse(((String) item.getProperty("json")).contains("null")); + } + } + @Test public void testOptimizedCountQuery() { String className = "testOptimizedCountQuery";