Skip to content

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

@spring-projects-issues

Description

@spring-projects-issues

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:

	@Test
	public void verifyConsistentImplementationOf_equals_And_compareTo() {
		verifyConsistentImplementationOf_equals_And_compareTo(
			MediaType.parseMediaType("text/html; q=0.7; charset=iso-8859-1"),
			MediaType.parseMediaType("text/html; q=0.7")
		);
		verifyConsistentImplementationOf_equals_And_compareTo(
				[add more test cases ...]
		);
		[add more test cases ...]
	}

	private void verifyConsistentImplementationOf_equals_And_compareTo(MediaType mediaType1, MediaType mediaType2) {
		assertEquals(mediaType1.equals(mediaType2), mediaType1.compareTo(mediaType2) == 0);
	}

/ Tomas Johansson


Affects: 3.0 GA

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions