-
Notifications
You must be signed in to change notification settings - Fork 85
Description
While regenerating an SDK to use stream-style serialization I found that the JsonMergePatchHelper pattern used to turn on and off patch serialization doesn't work correctly for polymorphic types.
When the models support JSON merge patch each model will create an access helper and add a boolean flag to determine whether JSON merge patch serialization should be used when calling toJson. This isn't necessary and can be a single instance on the super type by having the super type expose the patch serialization status to child types. So instead of:
public class Super {
static {
// Configure access helper
}
private boolean jsonMergePatch;
public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
if (jsonMergePatch) {
...
}
}
public class Child extends Super {
static {
// Configure access helper
}
private boolean jsonMergePatch;
public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
if (jsonMergePatch) {
...
}
}it can be simplified to
public class Super {
static {
// Configure access helper
}
private boolean jsonMergePatch;
boolean isJsonMergePatch() {
return jsonMergePatch;
}
public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
if (isJsonMergePatch()) {
...
}
}
public class Child extends Super {
public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
if (isJsonMergePatch()) {
...
}
}And since there is boilerplate code for each accessor this will simplify that to one instance per polymorphic model hierarchy.
Further investigation could be done to see if it is possible for all models within a single package could use the same access helper.