-
Notifications
You must be signed in to change notification settings - Fork 37.7k
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
The class MediaType has a natural ordering that is inconsistent with equals, which is generally recommended or should otherwise at least be indicated in the javadoc [SPR-6788] #11454
Comments
Arjen Poutsma commented Added formatting. |
Arjen Poutsma commented Thanks for spotting this. MediaType no longer implements Comparable now. Instead, I've added a sortBySpecificity() method, which sorts a list of media types by specificity. |
Arjen Poutsma commented The javadoc of sortBySpecificity() reads: Given two media types:
For example:
|
Tomas Johansson commented Well, thank you yourself for dealing with the issue within one day, but I must say I was surprised to see that you immediately removed the implementation of the interface, considering the existing client code (among spring framework users) which now will get compiling errors when they have done this:
which now will have to be replaced with this:
Since Spring is a very much published framework ( see "Public versus Published Interfaces" : http://martinfowler.com/ieeeSoftware/published.pdf ) I think that the javadoc and release notes for 3.0.1 should mention that the interface should be considered as deprecated and in the future will become removed, rather than removing it at once and breaking the client code out there. Another thing, when I looked in the implementation in the new method 'sortBySpecificity' I noted that it does not really do much itself, except from two things:
Regarding the last potential reason for exposing this method into the MediaType class, is it really an appropriate thing to do, as opposed of instead making the Comparator implementation available (either by the MediaType class, or by a new MediaTypeComparatorFactory class) ? The problem with providing one specific method for that particular sorting is that it can force client programmers to write more clumsy code doing the sorting in different ways, if they want to either use the included Comparator or write their own Comparator.
On the other hand, if you (i.e. the Spring framework) would expose the Comparator (and I would also suggest to remove the new method sortBySpecificity), then the above method 'sortMediaTypes' could be implemented by the client code in a more transparent way like below:
/ Tomas Johansson |
Arjen Poutsma commented Thomas, There are a couple of good reasons why I removed the Comparable implementation outright, and decided to offer a sortBySpecificity method instead. Firstly, as you pointed out early in this issue, the original implementation of Comparable was inconsistent with equals and hashCode. The javadoc of Comparable states that this is strongly recommended. So, having a comparator made more sense, also because - as you rightfully comment - sorting by specificity is only of the ways to sort media types. However, if this comparator would have been made public, it allowed usage in sorted sets and maps, which would have caused strange results. The javadoc of Comparator says:
(Note that I myself made the mistake of sorting comparable MediaTypes in a TreeMap, in the ContentNegotiatingViewResolver. This could have caused some interesting new JIRAs along the way. This bug was fixed as part of the resolution of this issue.) I figured that if I could make the mistake of using it in TreeMaps, everybody could, and I wanted to negate that. That's why I decided to offer the sortBySpecificity() method, and not make the comparator public. So, in effect, the specificity comparator is only to be used for sorting lists (and not for TreeMaps and TreeSets). Some other remarks:
I hope that clears things up a bit, and I hope you understand my reasoning behind these changes. Cheers, Arjen |
Arjen Poutsma commented After thinking about your comment a bit more, and talking this through with Juergen, we've reinstated the Comparable implementation of MediaType. It now orders MediaTypes alphabetically, making it consistent with equals. |
Tomas Johansson commented Well, that sounded good but it actually is not consistent.
/ Tomas |
Arjen Poutsma commented The "text/html; q=0.7; charset=iso-8859-1" vs "text/html; charset=iso-8859-1; q=0.7" inconsistency should now be fixed. |
Tomas Johansson commented No, unfortunately not :-( / Tomas |
Tomas Johansson opened SPR-6788 and commented
For example, if you use these two strings:
"text/html; q=0.7; charset=iso-8859-1"
"text/html; q=0.7"
then the compareTo method will return zero but equals will return false.
I suggest that a consistent implementation should either be fixed, or otherwise (if necessary for some reason ?) at least it should be mentioned in the javadoc that the natural ordering that is inconsistent with equals.
Example of JUnit test that I think should pass:
/ Tomas Johansson
Affects: 3.0 GA
The text was updated successfully, but these errors were encountered: