Skip to content

Commit

Permalink
Fixed issue #3440
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Jan 28, 2015
1 parent 495de1c commit 6db63af
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 49 deletions.
Expand Up @@ -527,11 +527,19 @@ private static void fetchDocument(final ODocument iRootRecord, final Object iUse
final OIdentifiable fieldValue, final String fieldName, final int iCurrentLevel, final int iLevelFromRoot,
final int iFieldDepthLevel, final Map<ORID, Integer> parsedRecords, final String iFieldPathFromRoot,
final OFetchListener iListener, final OFetchContext iContext) throws IOException {
if (fieldValue instanceof ORID && !((ORID) fieldValue).isValid()) {
// RID NULL: TREAT AS "NULL" VALUE
iContext.onBeforeStandardField(fieldValue, fieldName, iRootRecord);
iListener.parseLinked(iRootRecord, fieldValue, iUserObject, fieldName, iContext);
iContext.onAfterStandardField(fieldValue, fieldName, iRootRecord);
return;
}

final Integer fieldDepthLevel = parsedRecords.get(fieldValue.getIdentity());
if (!fieldValue.getIdentity().isValid() || (fieldDepthLevel != null && fieldDepthLevel.intValue() == iLevelFromRoot)) {
removeParsedFromMap(parsedRecords, fieldValue);
final ODocument linked = (ODocument) fieldValue.getRecord();
if( linked == null )
if (linked == null)
return;

iContext.onBeforeDocument(iRootRecord, linked, fieldName, iUserObject);
Expand Down
Expand Up @@ -159,7 +159,8 @@ public void writeLinkedValue(final OIdentifiable iRecord, final String iFieldNam
}

public void writeLinkedAttribute(final OIdentifiable iRecord, final String iFieldName) throws IOException {
jsonWriter.writeAttribute(settings.indentLevel, true, iFieldName, OJSONWriter.encode(iRecord.getIdentity()));
final Object link = iRecord.getIdentity().isValid() ? OJSONWriter.encode(iRecord.getIdentity()) : null;
jsonWriter.writeAttribute(settings.indentLevel, true, iFieldName, link);
}

public boolean isInCollection(ODocument record) {
Expand Down
Expand Up @@ -97,6 +97,10 @@ public static boolean isTemporary(final long clusterPosition) {
return clusterPosition < CLUSTER_POS_INVALID;
}

public static boolean isA(final String iString) {
return iString.matches("#(-?[0-9]+):(-?[0-9]+)");
}

public void reset() {
clusterId = CLUSTER_ID_INVALID;
clusterPosition = CLUSTER_POS_INVALID;
Expand Down
Expand Up @@ -19,18 +19,18 @@
*/
package com.orientechnologies.orient.core.serialization.serializer.record.string;

import java.io.IOException;
import java.io.StringWriter;
import java.text.ParseException;
import java.util.*;

import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.common.parser.OStringParser;
import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.OUserObject2RecordHandler;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.db.record.*;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ORecordLazyList;
import com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue;
import com.orientechnologies.orient.core.db.record.ORecordLazySet;
import com.orientechnologies.orient.core.db.record.OTrackedList;
import com.orientechnologies.orient.core.db.record.OTrackedSet;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.exception.OSerializationException;
import com.orientechnologies.orient.core.fetch.OFetchHelper;
Expand All @@ -54,6 +54,16 @@
import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper;
import com.orientechnologies.orient.core.util.ODateHelper;

import java.io.IOException;
import java.io.StringWriter;
import java.text.ParseException;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@SuppressWarnings("serial")
public class ORecordSerializerJSON extends ORecordSerializerStringAbstract {

Expand Down Expand Up @@ -430,7 +440,7 @@ private Object getValue(final ODocument iRecord, String iFieldName, String iFiel

if (c == null && !iFieldValue.isEmpty()) {
// TRY TO AUTODETERMINE THE BEST TYPE
if (iFieldValue.charAt(0) == ORID.PREFIX && iFieldValue.contains(":"))
if (ORecordId.isA(iFieldValue))
iType = OType.LINK;
else if (OStringSerializerHelper.contains(iFieldValue, '.')) {
// DECIMAL FORMAT: DETERMINE IF DOUBLE OR FLOAT
Expand All @@ -454,19 +464,8 @@ else if (OStringSerializerHelper.contains(iFieldValue, '.')) {
} else if (iFieldValue.startsWith("{") && iFieldValue.endsWith("}"))
iType = OType.EMBEDDED;
else {
if (iFieldValueAsString.length() >= 4 && iFieldValueAsString.charAt(0) == ORID.PREFIX && iFieldValueAsString.contains(":")) {
// IS IT A LINK?
final List<String> parts = OStringSerializerHelper.split(iFieldValueAsString, 1, -1, ':');
if (parts.size() == 2)
try {
Short.parseShort(parts.get(0));
// YES, IT'S A LINK
if (parts.get(1).matches("\\d+")) {
iType = OType.LINK;
}
} catch (Exception ignored) {
}
}
if (ORecordId.isA(iFieldValueAsString))
iType = OType.LINK;

if (iFieldTypes != null) {
Character c = iFieldTypes.get(iFieldName);
Expand Down
@@ -1,38 +1,30 @@
/*
*
* * Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
* *
* * For more information: http://www.orientechnologies.com
*
*/
*
* * Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
* *
* * For more information: http://www.orientechnologies.com
*
*/
package com.orientechnologies.orient.core.sql;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.common.io.OIOUtils;
import com.orientechnologies.common.parser.OBaseParser;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.metadata.schema.OType;
Expand All @@ -50,6 +42,13 @@
import com.orientechnologies.orient.core.sql.filter.OSQLPredicate;
import com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
* SQL Helper class
*
Expand Down Expand Up @@ -122,7 +121,7 @@ else if (iValue.charAt(0) == OStringSerializerHelper.LIST_BEGIN
fieldValue = new OCommandSQL(iValue.substring(1, iValue.length() - 1));
((OCommandSQL) fieldValue).getContext().setParent(iContext);

} else if (iValue.charAt(0) == ORID.PREFIX)
} else if (ORecordId.isA(iValue))
// RID
fieldValue = new ORecordId(iValue.trim());
else {
Expand Down
Expand Up @@ -1145,4 +1145,17 @@ public void testJSONTxDoc() {
Assert.assertTrue(content.hasSameContentOf(o));
}
}

public void testInvalidLink() {
ODocument nullRefDoc = new ODocument();
nullRefDoc.fromJSON("{\"name\":\"Luca\", \"ref\":\"#-1:-1\"}");

// Assert.assertNull(nullRefDoc.rawField("ref"));

String json = nullRefDoc.toJSON();
int pos = json.indexOf("\"ref\":");

Assert.assertTrue(pos > -1);
Assert.assertEquals(json.charAt(pos + "\"ref\":".length()), 'n');
}
}

0 comments on commit 6db63af

Please sign in to comment.