[JAVA] equals and hashCode for models with byte[] and binary strings#7341
[JAVA] equals and hashCode for models with byte[] and binary strings#7341wing328 merged 3 commits intoswagger-api:2.4.0from bmordue:fix-3731
Conversation
| }{{#hasVars}} | ||
| {{classname}} {{classVarName}} = ({{classname}}) o; | ||
| return {{#vars}}Objects.equals(this.{{name}}, {{classVarName}}.{{name}}){{#hasMore}} && | ||
| return {{#vars}}{{#isByteArray}}Arrays{{/isByteArray}}{{#isBinary}}Arrays{{/isBinary}}{{^isByteArray}}{{^isBinary}}Objects{{/isBinary}}{{/isByteArray}}.equals(this.{{name}}, {{classVarName}}.{{name}}){{#hasMore}} && |
There was a problem hiding this comment.
@bmordue One suggestion: What about using {{#isBinary}}java.util.Arrays{{/isBinary}} to avoid unused import when no property is defined as byte array or binary?
There was a problem hiding this comment.
Maybe I've misunderstood, but I thought isBinary and isByteArray apply to a single CodegenProperty and would only be relevant inside models.model.vars in the template?
There was a problem hiding this comment.
My point is in https://github.com/swagger-api/swagger-codegen/pull/7341/files#diff-cc3e8eb9a2b26248932102a87608fc60R7, you're hard-coding "import java.util.Arrays;".
My suggestion using fully-qualified name is to avoid hard-coding the import
There was a problem hiding this comment.
Yes there's a risk of having an unused import. But note that it's the case in a lot of other places of our templates.
Managing imports is really something not easy with code generators.
There was a problem hiding this comment.
Yup, we'll try to take care of the unused import in the future so this PR is good to merge.
PR checklist
./bin/to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.shand./bin/security/{LANG}-petstore.shif updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\.3.0.0branch for changes related to OpenAPI spec 3.0. Default:master.Description of the PR
For models with string fields in "byte" or "binary" format, the generated equals and hashCode methods are not correct. This change modifies the template to use Array.equals and Arrays.hash for byte array and binary variables.
Fixes #3731
This PR replaces/supersedes #6760
cc @bbdouglas @JFCote @sreeshas @jfiala @lukoyanov @cbornet
cc @brentryan (reporter of #3731)