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

analyzer: Stop using a SortedSet in ProjectAnalyzerResult #6244

Merged
merged 5 commits into from
Dec 19, 2022

Conversation

sschuberth
Copy link
Member

@sschuberth sschuberth commented Dec 16, 2022

The ProjectAnalyzerResult is only serialized as part of analyzer functional tests when comparing to expected results. So in memory, there is no need for the packages to be a SortedSet. Change the code to only sort packages on serialization via a Jackson converter to have a defined order when comparing to expected results.

Signed-off-by: Sebastian Schuberth sschuberth@gmail.com

Part of: #6235


import com.fasterxml.jackson.databind.util.StdConverter

class SortedSetConverter<T : Comparable<T>> : StdConverter<Set<T>, Set<T>>() {
Copy link
Member Author

@sschuberth sschuberth Dec 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, this is a generic implementation that works on Comparable<T>, which has the downside that the classes still need to implement Comparable, and the current implementations are confusing as they're not in line with equals().

So maybe we should prefer to have dedicated converters per class that we want to sort as these could hard-code the property to sort on then, like

class SortedPackageSetConverter : StdConverter<Set<Package>, Set<Package>>() {
    override fun convert(value: Set<Package>): Set<Package> =
        value.toSortedSet(compareBy { it.id })
}

That would allow us to remove the Comparable implementations in exchange for several more converted classes. But as there are only ~10 implementations that we need to care about as they influence serialization, that's maybe still a good idea.

Copy link
Member Author

@sschuberth sschuberth Dec 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe we should prefer to have dedicated converters per class [...] That would allow us to remove the Comparable implementations in exchange for several more converted classes.

For the record, that's what I did now.

@sschuberth
Copy link
Member Author

FYI @mnonnenmacher, a SortedSet keeps only the first entry that's equal according to compareTo, see https://pl.kotl.in/zzR5V7Npo.

Which means that this change may uncover previously unnoticed cases where packages with the same id but different other properties are being added to the result.

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
@sschuberth sschuberth marked this pull request as ready for review December 19, 2022 14:50
@sschuberth sschuberth requested a review from a team as a code owner December 19, 2022 14:50
fviernau
fviernau previously approved these changes Dec 19, 2022
As now all properties are taken into account, this aligns
`compareTo() == 0` with `equals() == true`.

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
There is no need to have these sorted in memory.

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
It is fine if the issues are not created in the order of sorted package
ids.

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
The `ProjectAnalyzerResult` is only serialized as part of analyzer
functional tests when comparing to expected results. So in memory, there
is no need for the `packages` to be a `SortedSet`. Change the code to
only sort `packages` on serialization via a Jackson converter to have a
defined order when comparing to expected results.

This lays the fundation for getting rid of `Comparable` implementations
in various classes. These implementations were only added for sorted
output during serialization, but they break the `Set` contract as
`compareTo() == 0` is not aligned with `equals() == true`, also see [1].

[1]: https://docs.oracle.com/javase/8/docs/api/java/util/SortedSet.html

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
@sschuberth sschuberth merged commit 029336c into main Dec 19, 2022
@sschuberth sschuberth deleted the par-no-sorted-set branch December 19, 2022 17:20
sschuberth added a commit that referenced this pull request Jan 2, 2023
Continuing the story from [1], only sort authors on serialization.

[1]: #6244

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
@sschuberth sschuberth added the release notes Changes that should be mentioned in release notes label Jan 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release notes Changes that should be mentioned in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants