koryphe icon indicating copy to clipboard operation
koryphe copied to clipboard

Replaced HashSet with LinkedHashSet for deterministic ordering in IsIn

Open mumbler6 opened this issue 1 year ago • 4 comments

The indeterministic ordering of a HashSet in the IsIn class causes the ordering of objects when serialized (to a JSON string) to be inconsistent sometimes. The following test case fails if this happens:

https://github.com/gchq/koryphe/blob/36c63c3e24145e04405d7620524b23cf9b5b5849/core/src/test/java/uk/gov/gchq/koryphe/impl/predicate/IsInTest.java#L53

This PR proposes to replace the HashSet in the IsIn class with a LinkedHashSet, maintaining insertion order so that the test case is correct. It didn't make sense to me to simply change the test case, since the test case is testing serialization of the IsIn class and is comparing two JSON strings, which cannot be sorted or really manipulated.

This change was confirmed by running the NonDex tool, which explores and reports errors in different behaviors of under-determined Java APIs.

To reproduce this problem, you can run the test with NonDex using these commands:

mvn install -pl core -am -DskipTests

mvn -pl core edu.illinois:nondex-maven-plugin:2.1.7:nondex -Dtest=uk.gov.gchq.koryphe.impl.predicate.IsInTest#shouldJsonSerialiseAndDeserialise

Here are screenshots of the output produced by NonDex before and after the fix: image image

Please let me know if you want to discuss these changes.

mumbler6 avatar Oct 03 '24 01:10 mumbler6

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Oct 03 '24 01:10 CLAassistant

Ah, I understand. When I made this PR, I thought it made more sense to make changes to the underlying implementation but it's clear that is not necessary. You should be able to make edits to the branch, as I've should've checked that setting in this image.

image

mumbler6 avatar Jan 04 '25 01:01 mumbler6