-
-
Notifications
You must be signed in to change notification settings - Fork 708
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
NPE in StandardRepresentation for maps with null entries #3087
Comments
Thanks for reporting this @tkruse! |
I was looking for something easy to do, and this looks pretty easy. Can I take this one? |
Thanks @etellman, it should not be to complicated since it is only related to maps. Cheers |
I looked at this a bit, and am now questioning whether it is worth doing. I'm pretty confident that a null entry doesn't make sense and the only time it would come up is if there is a bug in the map implementation, which seems like a pretty obscure case. Here's what I looked The change to StandardRepresentation is straightforward:
To get "containsKey" to work, internal/Maps.java needs a change as well:
Without this, this test still throws a NPE:
But even with this change, this test fails (with the key "present" hard coded to be in the entry set):
That key is actually in the entry set, but entry sets containing null entries are now basically the same as empty entry sets, so this one fails because no Given that this change seems to be just trying to cover up a bug in the map implementation, I'm thinking that it would be better to leave things as they are now. The way it is now, you'll get a NPE and can go fix your custom map. What do you think? |
We only want to fix the NPE in the |
I probably didn't explain this clearly, but I don't think there should make any change for this. The code as written now seems fine to me. It's plausible that for a particular pair in the entry set, the key and/or value is null. I think these cases are already handled by AssertJ. It isn't plausible for the entry set to contain a null key/value pair, as there isn't anything reasonable that this would mean. If there's a null value in the entry set, the user's map implementation has a bug and they should fix it. When I created a trivial map implementation with this problem, I thought that instead of changing anything, we should just leave the code as it is now. If the user's map implementation returns entry sets with I definitely wouldn't change the behavior of An alternative would be to have Let me know what you think. If you still want to change |
If the map entry set contains a null value, print "null" instead of throwing a NPE. see: assertj#3087
Here's a pull request for handling null entries when formatting the map, since this seems likely to be the preferred solution: #3110. This doesn't try to make anything else handle null values in entries, so it's still possible that a map with this behavior will result in an exception being thrown before it's converted to a string. |
Describe the bug
When using a non-standard Java map where entries can be null, NPEs happen when assertJ tries to generate failure messages representing the map content.
Note that Map code allowing null map entries in
.entrySet()
are probably buggy, but I cannot tell if it really break a contract.Test case reproducing the bug
To reproduce:
To fix, StandardRepresentation should check the entry is not null.
It might be good to also check other occurences of entry.getKey() /entry.getValue() in the code.
I notice IntelliJ as an example handles this case in it's representation of maps.
The text was updated successfully, but these errors were encountered: