rest icon indicating copy to clipboard operation
rest copied to clipboard

Response.Status enum doesn't implement equals in a commutative way.

Open kingsfleet opened this issue 4 years ago • 1 comments

It would expect:

builtResponse.getStatusInfo().equals(Response.Status.UNAUTHORIZED)

to be equals to

Response.Status.UNAUTHORIZED.equals(builtResponse.getStatusInfo())

But the enumeration doesn't implement equals for StatusType, just relying on object identity. So if builtResponse.getStatusInfo() returns an instance of Statuses.StatusImpl then it will return false even if all the relevant values are the same.

kingsfleet avatar Mar 09 '21 15:03 kingsfleet

I think the problem here is that objects like Response.Status.UNAUTHORIZED are Status enums and the values returned from response.getStatusInfo() are StatusType interfaces.

Java won't allow an enum to override the equals method, so the toEnum() method was added to the StatusType interface in 2.1. So, while it's not the most intuitive, I think this ought to work:

builtResponse.getStatusInfo().toEnum().equals(Response.Status.UNAUTHORIZED)

is equal to:

Response.Status.UNAUTHORIZED.equals(builtResponse.getStatusInfo().toEnum())

which should also be equals to:

Response.Status.UNAUTHORIZED == builtResponse.getStatusInfo()

Hope this helps!

andymc12 avatar Mar 09 '21 23:03 andymc12