Skip to content

Commit

Permalink
fix: make sure that toJson of list of ids get expanded, issue #10264
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Jul 15, 2024
1 parent 4ec7466 commit 057b0c3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object>) iThis);
return iParams.length == 1 ? doc.toJSON(format) : doc.toJSON();
return doc.toJSON(format);

} else if (OMultiValue.isMultiValue(iThis)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4583,6 +4583,40 @@ 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";
Expand Down

0 comments on commit 057b0c3

Please sign in to comment.