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

Fix comparison of title in equals() and hashCode() of ProblemDetail #30294

Closed
wants to merge 1 commit into from

Conversation

quaff
Copy link
Contributor

@quaff quaff commented Apr 6, 2023

Lazy computed title property should be taken into account

Lazy computed title property should be taken into account
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 6, 2023
@sbrannen sbrannen added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement labels Apr 6, 2023
@sbrannen sbrannen changed the title Improve ProblemDetail equals and hashCode Improve equals() and hashCode() in ProblemDetail Apr 6, 2023
Copy link
Contributor

@simonbasle simonbasle left a comment

Choose a reason for hiding this comment

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

thanks for the PR @quaff but what's the rationale behind this?

using the getTitle() over this.title isn't that much of an improvement because if the field is not set, the getter derives the result from the this.status field (namely, the reason phrase of the enum which is constant). since this.status already participates in both equals() and hashcode(), this change doesn't add new information to these methods.

it even arguably can make them less performant, because now instead of a simple null equality + int comparison, it adds resolution of an enum from the status code + getting the enum's reason phrase to the mix.

so overall I don't see the benefit of this change.

@simonbasle simonbasle added status: waiting-for-triage An issue we've not yet triaged or decided on and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 6, 2023
@quaff
Copy link
Contributor Author

quaff commented Apr 7, 2023

thanks for the PR @quaff but what's the rationale behind this?

using the getTitle() over this.title isn't that much of an improvement because if the field is not set, the getter derives the result from the this.status field (namely, the reason phrase of the enum which is constant). since this.status already participates in both equals() and hashcode(), this change doesn't add new information to these methods.

it even arguably can make them less performant, because now instead of a simple null equality + int comparison, it adds resolution of an enum from the status code + getting the enum's reason phrase to the mix.

so overall I don't see the benefit of this change.

You can review the test I added, currently the ProblemDetail is not equal after serialize then deserialize.
The actual use case is:

		ResponseEntity<ProblemDetail> resp = this.testRestTemplate.exchange(
				RequestEntity.method(HttpMethod.GET, path).header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).build(),
				ProblemDetail.class);
		ProblemDetail expected = ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT, "Conflicts arise");
		expected.setTitle(HttpStatus.CONFLICT.getReasonPhrase());
		expected.setInstance(URI.create(path));
		assertThat(resp.getBody()).isEqualTo(expected);

Title must be explicit set before comparison, but it is implicit at server side, that's not idiomatic.

@quaff quaff requested a review from simonbasle April 7, 2023 01:42
@rstoyanchev rstoyanchev self-assigned this Apr 11, 2023
@rstoyanchev rstoyanchev added this to the 6.0.8 milestone Apr 11, 2023
@rstoyanchev rstoyanchev added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on type: enhancement A general enhancement labels Apr 11, 2023
Copy link
Contributor

@rstoyanchev rstoyanchev left a comment

Choose a reason for hiding this comment

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

Yes that makes sense to use getTitle() to get a consistent value.

@rstoyanchev rstoyanchev changed the title Improve equals() and hashCode() in ProblemDetail Fix comparison of title in equals() and hashCode() of ProblemDetail Apr 11, 2023
rstoyanchev pushed a commit that referenced this pull request Apr 12, 2023
Lazy computed title property should be taken into account

See gh-30294
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants