Skip to content

Commit

Permalink
refactor(model): Serialize dependency graph edges in sorted order
Browse files Browse the repository at this point in the history
Improve human readability and make the serialized ordering independent
from the insertion order.

Signed-off-by: Frank Viernau <frank_viernau@epam.com>
  • Loading branch information
fviernau committed May 24, 2024
1 parent 4c2db37 commit 0dba9c9
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 198 deletions.
3 changes: 3 additions & 0 deletions model/src/main/kotlin/DependencyGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ package org.ossreviewtoolkit.model

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.annotation.JsonSerialize

import java.util.SortedSet

import org.ossreviewtoolkit.model.utils.DependencyGraphEdgeSortedSetConverter
import org.ossreviewtoolkit.model.utils.PackageLinkageValueFilter

/**
Expand Down Expand Up @@ -102,6 +104,7 @@ data class DependencyGraph(
* A set with the edges of this dependency graph. By traversing the edges, the dependencies of packages can be
* determined.
*/
@JsonSerialize(converter = DependencyGraphEdgeSortedSetConverter::class)
val edges: Set<DependencyGraphEdge>? = null
) {
companion object {
Expand Down
5 changes: 5 additions & 0 deletions model/src/main/kotlin/utils/SortedSetConverters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import java.util.SortedSet

import org.ossreviewtoolkit.model.ArtifactProvenance
import org.ossreviewtoolkit.model.CopyrightFinding
import org.ossreviewtoolkit.model.DependencyGraphEdge
import org.ossreviewtoolkit.model.FileList
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.LicenseFinding
Expand All @@ -44,6 +45,10 @@ class CopyrightFindingSortedSetConverter : StdConverter<Set<CopyrightFinding>, S
override fun convert(value: Set<CopyrightFinding>) = value.toSortedSet(CopyrightFinding.COMPARATOR)
}

class DependencyGraphEdgeSortedSetConverter : StdConverter<Set<DependencyGraphEdge>, Set<DependencyGraphEdge>>() {
override fun convert(value: Set<DependencyGraphEdge>) = value.toSortedSet(compareBy({ it.from }, { it.to }))
}

/** Do not convert to SortedSet in order to not require a comparator consistent with equals */
class FileListSortedSetConverter : StdConverter<Set<FileList>, Set<FileList>>() {
override fun convert(value: Set<FileList>) = value.sortedBy { it.provenance.getSortKey() }.toSet()
Expand Down
Loading

0 comments on commit 0dba9c9

Please sign in to comment.