I'm encountering a (compatibility) problem with openapi, the java code generator (openapi-generator-maven-plugin. version: 6.0.1) and nullable properties.
I have defined an object which I use for PATCH endpoints and which resembles a picture and it's defined as a byte array byte[]. The openapi.yaml looks like this:
TestObject:
type: object
properties:
picture:
type: string
format: byte
nullable: true
The generated code looks like this:
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2022-09-08T11:52:30.791139+02:00[Europe/Malta]")
public class TestObject {
@JsonProperty("picture")
private JsonNullable<byte[]> picture = JsonNullable.undefined();
public TestObject picture(byte[] picture) {
this.picture = JsonNullable.of(picture);
return this;
}
/**
* Get picture
* @return picture
*/
@Schema(name = "picture", required = false)
public JsonNullable<byte[]> getPicture() {
return picture;
}
public void setPicture(JsonNullable<byte[]> picture) {
this.picture = picture;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TestObject testObject = (TestObject) o;
return Arrays.equals(this.picture, testObject.picture);
^--------- no suitable method found for equals(org.openapitools.jackson.nullable.JsonNullable<byte[]>,org.openapitools.jackson.nullable.JsonNullable<byte[]>)
}
private static <T> boolean equalsNullable(JsonNullable<T> a, JsonNullable<T> b) {
return a == b || (a != null && b != null && a.isPresent() && b.isPresent() && Objects.deepEquals(a.get(), b.get()));
}
@Override
public int hashCode() {
return Objects.hash(Arrays.hashCode(picture));
}
private static <T> int hashCodeNullable(JsonNullable<T> a) {
if (a == null) {
return 1;
}
return a.isPresent() ? Arrays.deepHashCode(new Object[]{a.get()}) : 31;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class TestObject {\n");
sb.append(" picture: ").append(toIndentedString(picture)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
The problem is that in the equals method (see my error added inline), the generator uses Arrays.equals instead of Objects.equals for comparing the JsonNullable<byte[]> objects.
Any ideas of how to fix this? I tried a different approaches/searched the internet and didn't find anything that would help my situation.
Edit:
Seems like this issue was fixed in openapi-generator 5.2.1 https://github.com/OpenAPITools/openapi-generator/pull/10012 but I am using 6.0.1 at this moment and still encounter this.
I've also tried to add other nullable properties in my yaml definition file and I see that the generated method
equalsNullableis never used,Objects.equals(a, b)is always the one that is being picked up.