Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove 'documentDescribes' 'hasFiles' from output #56

Merged
merged 1 commit into from
Apr 15, 2023
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
68 changes: 8 additions & 60 deletions src/main/java/org/spdx/jacksonstore/JacksonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,6 @@ private void addJsonRelationships(String documentUri, TypedValue element, Object
ArrayNode relationships, Set<String> hasFileIds, Set<String> documentDescribesIds) throws InvalidSPDXAnalysisException {
Iterator<Object> valueList = store.listValues(documentUri, element.getId(),
SpdxConstants.PROP_RELATIONSHIP);
List<String> hasFileIdsToAdd = new ArrayList<>();
List<String> documentDescribesIdsToAdd = new ArrayList<>();
while (valueList.hasNext()) {
Object value = valueList.next();
if (!(value instanceof TypedValue)) {
Expand All @@ -480,23 +478,18 @@ private void addJsonRelationships(String documentUri, TypedValue element, Object
}
Optional<Object> relationshipComment = store.getValue(documentUri, tvValue.getId(), SpdxConstants.RDFS_PROP_COMMENT);
String relatedElementId;
String relatedElementType;
if (relatedSpdxElement.get() instanceof TypedValue) {
relatedElementId = ((TypedValue)relatedSpdxElement.get()).getId();
relatedElementType = ((TypedValue)relatedSpdxElement.get()).getType();
} else if (relatedSpdxElement.get() instanceof IndividualUriValue) {
String externalUri = ((IndividualUriValue)relatedSpdxElement.get()).getIndividualURI();
if (SpdxConstants.URI_VALUE_NONE.equals(externalUri)) {
relatedElementId = SpdxConstants.NONE_VALUE;
relatedElementType = SpdxConstants.CLASS_SPDX_NONE_ELEMENT;
} else if (SpdxConstants.URI_VALUE_NOASSERTION.equals(externalUri)) {
relatedElementId = SpdxConstants.NOASSERTION_VALUE;
relatedElementType = SpdxConstants.CLASS_SPDX_NOASSERTION_ELEMENT;
} else if (SpdxConstants.EXTERNAL_SPDX_ELEMENT_URI_PATTERN.matcher(externalUri).matches()) {
// external SPDX element
ExternalSpdxElement externalElement = ExternalSpdxElement.uriToExternalSpdxElement(externalUri, store, documentUri, null);
relatedElementId = externalElement.getExternalDocumentId() + ":" + externalElement.getExternalElementId();
relatedElementType = SpdxConstants.CLASS_EXTERNAL_SPDX_ELEMENT;
} else {
throw new SpdxInvalidTypeException("SPDX element must be of SpdxElement, SpdxNoneElement, SpdxNoAssertionElement or external SPDX element type. URI does not match pattern for external element: "+externalUri);
}
Expand All @@ -513,59 +506,14 @@ private void addJsonRelationships(String documentUri, TypedValue element, Object
throw new SpdxInvalidTypeException("Expected RelationshipType type for relationshipType property. Unexpected type "+relatedSpdxElement.get().getClass().toString());
}
String relationshipTypeStr = individualUriToString(documentUri, ((IndividualUriValue)relationshipType.get()).getIndividualURI());
if (RelationshipType.DESCRIBES.toString().equals(relationshipTypeStr) &&
SpdxConstants.CLASS_SPDX_DOCUMENT.equals(element.getType()) &&
!documentDescribesIds.contains(relatedElementId)) {
// This needs to be added as a documentDescribes property and not as a relationship
documentDescribesIdsToAdd.add(relatedElementId);
documentDescribesIds.add(relatedElementId);
} else if (RelationshipType.CONTAINS.toString().equals(relationshipTypeStr) &&
SpdxConstants.CLASS_SPDX_PACKAGE.equals(element.getType()) &&
SpdxConstants.CLASS_SPDX_FILE.equals(relatedElementType) &&
!hasFileIds.contains(relatedElementId)) {
// This needs to be added as a hasFiles property
//TODO: Check if hasFile is deprecated - if so, remove this section
hasFileIdsToAdd.add(relatedElementId);
hasFileIds.add(relatedElementId);
} else {
ObjectNode relationship = mapper.createObjectNode();
relationship.put(SpdxConstants.PROP_SPDX_ELEMENTID, element.getId());
relationship.put(SpdxConstants.PROP_RELATIONSHIP_TYPE, relationshipTypeStr);
relationship.put(SpdxConstants.PROP_RELATED_SPDX_ELEMENT, relatedElementId);
if (relationshipComment.isPresent()) {
relationship.put(SpdxConstants.RDFS_PROP_COMMENT, (String)relationshipComment.get());
}
relationships.add(relationship);
}
}
if (hasFileIdsToAdd.size() > 0) {
JsonNode jnFiles = elementNode.get(
MultiFormatStore.propertyNameToCollectionPropertyName(SpdxConstants.PROP_PACKAGE_FILE));
if (Objects.isNull(jnFiles)) {
jnFiles = mapper.createArrayNode();
elementNode.set(
MultiFormatStore.propertyNameToCollectionPropertyName(SpdxConstants.PROP_PACKAGE_FILE),
jnFiles);
}
if (!(jnFiles instanceof ArrayNode)) {
throw new InvalidSPDXAnalysisException("hasFile is not an array");
}
for (String hasFileId:hasFileIdsToAdd) {
((ArrayNode)jnFiles).add(hasFileId);
}
}
if (documentDescribesIdsToAdd.size() > 0) {
JsonNode jnDescribes = elementNode.get(SpdxConstants.PROP_DOCUMENT_DESCRIBES);
if (Objects.isNull(jnDescribes)) {
jnDescribes = mapper.createArrayNode();
elementNode.set(SpdxConstants.PROP_DOCUMENT_DESCRIBES, jnDescribes);
}
if (!(jnDescribes instanceof ArrayNode)) {
throw new InvalidSPDXAnalysisException("Describes is not an array");
}
for (String describesId:documentDescribesIdsToAdd) {
((ArrayNode)jnDescribes).add(describesId);
}
ObjectNode relationship = mapper.createObjectNode();
relationship.put(SpdxConstants.PROP_SPDX_ELEMENTID, element.getId());
relationship.put(SpdxConstants.PROP_RELATIONSHIP_TYPE, relationshipTypeStr);
relationship.put(SpdxConstants.PROP_RELATED_SPDX_ELEMENT, relatedElementId);
if (relationshipComment.isPresent()) {
relationship.put(SpdxConstants.RDFS_PROP_COMMENT, (String)relationshipComment.get());
}
relationships.add(relationship);
}
}

Expand Down
46 changes: 2 additions & 44 deletions src/test/java/org/spdx/jacksonstore/MultiFormatStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -500,28 +500,7 @@ public void testDocumentDescribes() throws InvalidSPDXAnalysisException, IOExcep
doc = inputMapper.readTree(inStream);
}
JsonNode describes = doc.get("documentDescribes");
Iterator<JsonNode> iter = describes.elements();
int count = 0;

while (iter.hasNext()) {
foundFileA = false;
foundFileB = false;
while (iter.hasNext()) {
count++;
String spdxId = iter.next().asText();
if (fileA.getId().equals(spdxId)) {
assertFalse(foundFileA);
foundFileA = true;
}
if (fileB.getId().equals(spdxId)) {
assertFalse(foundFileB);
foundFileB = true;
}
}
assertTrue(foundFileA);
assertTrue(foundFileB);
assertEquals(2, count);
}
assertTrue(Objects.isNull(describes));
} finally {
if (serFile.exists()) {
serFile.delete();
Expand Down Expand Up @@ -622,28 +601,7 @@ public void testhasFiles() throws InvalidSPDXAnalysisException, IOException {
JsonNode packages = doc.get("packages");
JsonNode jsonPkg = packages.elements().next();
JsonNode files = jsonPkg.get("hasFiles");
Iterator<JsonNode> iter = files.elements();
int count = 0;

while (iter.hasNext()) {
foundFileA = false;
foundFileB = false;
while (iter.hasNext()) {
count++;
String spdxId = iter.next().asText();
if (fileA.getId().equals(spdxId)) {
assertFalse(foundFileA);
foundFileA = true;
}
if (fileB.getId().equals(spdxId)) {
assertFalse(foundFileB);
foundFileB = true;
}
}
assertTrue(foundFileA);
assertTrue(foundFileB);
assertEquals(2, count);
}
assertTrue(Objects.isNull(files));
} finally {
if (serFile.exists()) {
serFile.delete();
Expand Down