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

NullPointerException in equals(), hashCode() and toString() with @Nullable Vector. #34

Open
cozycode opened this issue May 31, 2021 · 3 comments

Comments

@cozycode
Copy link

cozycode commented May 31, 2021

I am creating an Immutable POJO object which needs to support an @Nullable Vector and the toString(), equals() and hashCode() methods are throwing NPEs because they assume the vector will never be null.

Most of the time this isn't an issue because I don't let the Vectors be nullable, but I now have a situation where it is not avoidable. I have worked around it by overriding those three methods, but this is something that I feel should be fixed in the "org.immutables.vavr:vavr-encodings" artifact (i.e. this project).

Thanks for all you do.

@cozycode
Copy link
Author

cozycode commented Jun 1, 2021

I tried to see if I could fix it and send up a PR, but I don't see where you've done anything custom to support toString(), equals() or hashCode() so I didn't know how to proceed and am less certain if the issue is from this jar. Maybe you need to add something specific to handle those methods? I don't know.

@elucash
Copy link
Member

elucash commented Jun 2, 2021

Thank you for reporting this. I definitely want to somehow address the issue, but it might not be easy due to certain limitation we have (regarding annotation matching and handling). It should be possible to customize toString, hashCode, equals using encoding (for that, encoding should contain String toString(), and/or int hashCode() and/or boolean equals(T t) definitions with no annotations, where those would use field(s) and return appropriate values for the type being encoded).

Just as workaround, if you use Vavr it might be more natural to use Option<Vector<T>> x(). Definitely you will not have addX() etc, but Vavr seems to have quite concise construction APIs, it would be quire expressive to have something like .x(Option.some(Vector.of("A", "B", "C"))) or .x(Option.some(Vector.empty().append("A").append("B")...)

On the second thought, it might be even possible (I honestly don't remember if it will work for sure, but...) to create separate encoding for type combination like Option<Vector<T>>

@Encoding
class OptionalVectorEncoding<T> {
   @Encoding.Impl
   private Option<Vector<T>> field = Option.none();
   ... 
   // and then builder which would define `addXx` method by smth like
   // case none -> some(Vector.of(param))
   // case some(v) -> some(v.append(param))`
}

That example is probably for creating encoding for your own needs, I don't know if those belong to this library of basic vavr encodings.

@elucash
Copy link
Member

elucash commented Jun 2, 2021

Here's example I found of similar, but probably more involved implementation of the encoding for combined types (apparently those are actually working) https://github.com/Discord4J/discord-json/blob/master/encoding/src/main/java/discord4j/discordjson/possible/PossibleListEncoding.java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants