Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/org/spdx/jacksonstore/JacksonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ private int compareObject(JsonNode arg0, JsonNode arg1) {
}
List<String> fieldNames = new ArrayList<>();
arg0.fieldNames().forEachRemaining(fieldNames::add);
arg1.fieldNames().forEachRemaining(field -> {
if (!fieldNames.contains(field)) {
fieldNames.add(field);
}
});
Collections.sort(fieldNames);
int retval = 0;
for (String fieldName:fieldNames) {
Expand Down Expand Up @@ -335,7 +340,7 @@ public ObjectNode docToJsonNode(String documentUri) throws InvalidSPDXAnalysisEx
* Sorts the elements of an ArrayNode in place
* @param an ArrayNode to sort
*/
private void sortArrayNode(ArrayNode an) {
protected static void sortArrayNode(ArrayNode an) {
List<JsonNode> arrayElements = StreamSupport.stream(an.spliterator(), false).sorted(NODE_COMPARATOR).collect(Collectors.toList());
an.removeAll();
an.addAll(arrayElements);
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/org/spdx/jacksonstore/MultiFormatStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.node.ArrayNode;
import org.spdx.core.DefaultModelStore;
import org.spdx.core.InvalidSPDXAnalysisException;
import org.spdx.core.ModelRegistry;
Expand Down Expand Up @@ -93,6 +94,7 @@ public class MultiFormatStoreTest extends TestCase {
static final String XML_1REL_FILE_PATH = "testResources" + File.separator + "SPDXXML-SingleRel-v2.3.spdx.xml";
static final String JSON_SCHEMA_V2_3 = "testResources" + File.separator + "spdx-schema.json";
static final String UNSORTED_JSON = "testResources" + File.separator + "SPDXJSONUnsortedIds.json";
static final String UNSORTED_RELATIONSHIPS = "testResources" + File.separator + "unsortedrelationships.json";

/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
Expand Down Expand Up @@ -758,4 +760,38 @@ public void testSortOrder() throws InvalidSPDXAnalysisException, IOException, Sp
tempDirPath.toFile().delete();
}
}

public void testRegressionSort() throws IOException {
File jsonFile = new File(UNSORTED_RELATIONSHIPS);
ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
JsonNode relationships = null;
try (FileInputStream stream = new FileInputStream(jsonFile)) {
relationships = mapper.readTree(stream);
}
assertTrue(relationships.isArray());
ArrayNode relArray = (ArrayNode)relationships;
for (int i = 0; i < relArray.size(); i++) {
for (int j = i; j < relArray.size(); j++) {
JsonNode rel1 = relArray.get(i);
JsonNode rel2 = relArray.get(j);
int result = JacksonSerializer.NODE_COMPARATOR.compare(rel1, rel2);
int revers = JacksonSerializer.NODE_COMPARATOR.compare(rel2, rel1);
if (relArray.get(i).equals(relArray.get(j))) {
assertEquals(0, result);
}
if (relArray.get(j).equals(relArray.get(i))) {
assertEquals(0, result);
}
if (result < 0) {
assertTrue(revers > 0);
} else if (result > 0) {
assertTrue(revers < 0);
} else {
assertEquals(0, revers);
}
}
}
JacksonSerializer.sortArrayNode(relArray);
}

}
Loading