openapi-processor / openapi-processor-core

moved into openapi-processor-base
Apache License 2.0
7 stars 5 forks source link

[Feature] Generate hashcode & equals methods for model classes #94

Open Nicklas2751 opened 2 years ago

Nicklas2751 commented 2 years ago

The generated model classes has neither a hash code nor an equals method. This leads to that when I wan't to test my endpoint implementations I always have to use a recursive comparison.

So it would be nice to be able to get the hashcode and equals method generated.

hauner commented 2 years ago

Not sure I want to generate equals() & hasCode(). Depends on the number of cases it needs to handle. If the array is the only special case it looks manageable. :)

having Objects only:

public boolean equals (Object o) {
    if (this == o)
        return true;

    if (o == null || getClass () != o.getClass ())
        return false;

    Reference reference = (Reference) o;
    return Objects.equals (ref, reference.ref)
        && Objects.equals (value, reference.value);
}

public int hashCode () {
    return Objects.hash (ref, value);
}

having an array:

public boolean equals (Object o) {
    if (this == o)
        return true;

    if (o == null || getClass () != o.getClass ())
        return false;

    Book book = (Book) o;
    return Objects.equals (id, book.id)
        && Objects.equals (title, book.title)
        && Arrays.equals (authors, book.authors);
}

public int hashCode () {
    int result = Objects.hash (id, title);
    result = 31 * result + Arrays.hashCode (authors);
    return result;
}