joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.24k stars 1.66k forks source link

Make equals and hashCode generation deterministic (order of expressions) #1578

Open myhau opened 11 months ago

myhau commented 11 months ago

Description

Order of lines of code in the generated equals and hashCode methods was non-deterministic. This means that the generated files with code have changed between generations (even if nothing has changed in the schema itself).

After the change, the order of these lines will be the same as the order of fields in the schema.

To do

Example for hashCode

For a simple schema that includes two fields: field1 and field2, the generator randomly produced either

@Override
    public int hashCode() {
        int result = 1;
        result = ((result* 31)+((this.field1 == null)? 0 :this.field1.hashCode()));
        result = ((result* 31)+((this.field2 == null)? 0 :this.field2.hashCode()));
        return result;
    }

or

@Override
    public int hashCode() {
        int result = 1;
        result = ((result* 31)+((this.field2== null)? 0 :this.field2.hashCode()));
        result = ((result* 31)+((this.field1 == null)? 0 :this.field1.hashCode()));
        return result;
    }