Skip to content

Commit e2acc6f

Browse files
tsp, json-merge-patch, fix the access issue on patch model property (#2702)
* use accessor pattern to enable json merge patch for model property. * regenerate * re-generate test code --------- Co-authored-by: actions-user <[email protected]>
1 parent be5cc37 commit e2acc6f

File tree

5 files changed

+22
-20
lines changed

5 files changed

+22
-20
lines changed

javagen/src/main/java/com/azure/autorest/template/StreamSerializationModelTemplate.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ private static void serializeJsonProperty(JavaBlock methodBlock, ClientModelProp
383383
propertyValueGetter);
384384
if (fieldSerializationMethod != null) {
385385
if (isJsonMergePatch && wireType instanceof ClassType && ((ClassType) wireType).isSwaggerType()) {
386-
methodBlock.line(propertyValueGetter + ".serializeAsJsonMergePatch(true);");
386+
methodBlock.line(String.format("JsonMergePatchHelper.get%1$sAccessor().prepareModelForJsonMergePatch(%2$s, true);", clientType.toString(), propertyValueGetter));
387387
}
388388
if (fromSuperType && clientType != wireType && clientType.isNullable()) {
389389
// If the property is from a super type and the client type is different from the wire type then a null
@@ -394,7 +394,7 @@ private static void serializeJsonProperty(JavaBlock methodBlock, ClientModelProp
394394
methodBlock.line(fieldSerializationMethod + ";");
395395
}
396396
if (isJsonMergePatch && wireType instanceof ClassType && ((ClassType) wireType).isSwaggerType()) {
397-
methodBlock.line(propertyValueGetter + ".serializeAsJsonMergePatch(false);");
397+
methodBlock.line(String.format("JsonMergePatchHelper.get%1$sAccessor().prepareModelForJsonMergePatch(%2$s, false);", clientType.toString(), propertyValueGetter));
398398
}
399399
} else if (wireType == ClassType.OBJECT) {
400400
methodBlock.line("jsonWriter.writeUntypedField(\"" + serializedName + "\", " + propertyValueGetter + ");");
@@ -482,11 +482,11 @@ private static void serializeJsonContainerProperty(JavaBlock methodBlock, String
482482
methodBlock.block("", codeBlock -> {
483483
codeBlock.ifBlock(elementName + "!=null", ifBlock -> {
484484
if (elementType instanceof ClassType && ((ClassType) elementType).isSwaggerType()) {
485-
codeBlock.line(elementName + ".serializeAsJsonMergePatch(true);");
485+
methodBlock.line(String.format("JsonMergePatchHelper.get%1$sAccessor().prepareModelForJsonMergePatch(%2$s, true);", ((ClassType) elementType).getName(), elementName));
486486
}
487487
ifBlock.line(valueSerializationMethod + ";");
488488
if (elementType instanceof ClassType && ((ClassType) elementType).isSwaggerType()) {
489-
codeBlock.line(elementName + ".serializeAsJsonMergePatch(false);");
489+
methodBlock.line(String.format("JsonMergePatchHelper.get%1$sAccessor().prepareModelForJsonMergePatch(%2$s, false);", ((ClassType) elementType).getName(), elementName));
490490
}
491491
}).elseBlock(elseBlock -> elseBlock.line(lambdaWriterName + ".writeNull();"));
492492
});

typespec-tests/src/main/java/com/cadl/flatten/models/UpdatePatchRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ private JsonWriter toJsonMergePatch(JsonWriter jsonWriter) throws IOException {
100100
if (this.patch == null) {
101101
jsonWriter.writeNullField("patch");
102102
} else {
103-
this.patch.serializeAsJsonMergePatch(true);
103+
JsonMergePatchHelper.getTodoItemPatchAccessor().prepareModelForJsonMergePatch(this.patch, true);
104104
jsonWriter.writeJsonField("patch", this.patch);
105-
this.patch.serializeAsJsonMergePatch(false);
105+
JsonMergePatchHelper.getTodoItemPatchAccessor().prepareModelForJsonMergePatch(this.patch, false);
106106
}
107107
}
108108
return jsonWriter.writeEndObject();

typespec-tests/src/main/java/com/cadl/patch/models/Resource.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ private JsonWriter toJsonMergePatch(JsonWriter jsonWriter) throws IOException {
353353
} else {
354354
jsonWriter.writeMapField("map", this.map, (writer, element) -> {
355355
if (element != null) {
356-
element.serializeAsJsonMergePatch(true);
356+
JsonMergePatchHelper.getInnerModelAccessor().prepareModelForJsonMergePatch(element, true);
357357
writer.writeJson(element);
358-
element.serializeAsJsonMergePatch(false);
358+
JsonMergePatchHelper.getInnerModelAccessor().prepareModelForJsonMergePatch(element, false);
359359
} else {
360360
writer.writeNull();
361361
}
@@ -387,9 +387,11 @@ private JsonWriter toJsonMergePatch(JsonWriter jsonWriter) throws IOException {
387387
if (this.innerModelProperty == null) {
388388
jsonWriter.writeNullField("wireNameForInnerModelProperty");
389389
} else {
390-
this.innerModelProperty.serializeAsJsonMergePatch(true);
390+
JsonMergePatchHelper.getInnerModelAccessor()
391+
.prepareModelForJsonMergePatch(this.innerModelProperty, true);
391392
jsonWriter.writeJsonField("wireNameForInnerModelProperty", this.innerModelProperty);
392-
this.innerModelProperty.serializeAsJsonMergePatch(false);
393+
JsonMergePatchHelper.getInnerModelAccessor()
394+
.prepareModelForJsonMergePatch(this.innerModelProperty, false);
393395
}
394396
}
395397
if (updatedProperties.contains("array")) {
@@ -403,9 +405,9 @@ private JsonWriter toJsonMergePatch(JsonWriter jsonWriter) throws IOException {
403405
if (this.fish == null) {
404406
jsonWriter.writeNullField("fish");
405407
} else {
406-
this.fish.serializeAsJsonMergePatch(true);
408+
JsonMergePatchHelper.getFishAccessor().prepareModelForJsonMergePatch(this.fish, true);
407409
jsonWriter.writeJsonField("fish", this.fish);
408-
this.fish.serializeAsJsonMergePatch(false);
410+
JsonMergePatchHelper.getFishAccessor().prepareModelForJsonMergePatch(this.fish, false);
409411
}
410412
}
411413
return jsonWriter.writeEndObject();

typespec-tests/src/main/java/com/cadl/patch/models/Salmon.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ private JsonWriter toJsonMergePatch(JsonWriter jsonWriter) throws IOException {
227227
} else {
228228
jsonWriter.writeMapField("hate", this.hate, (writer, element) -> {
229229
if (element != null) {
230-
element.serializeAsJsonMergePatch(true);
230+
JsonMergePatchHelper.getFishAccessor().prepareModelForJsonMergePatch(element, true);
231231
writer.writeJson(element);
232-
element.serializeAsJsonMergePatch(false);
232+
JsonMergePatchHelper.getFishAccessor().prepareModelForJsonMergePatch(element, false);
233233
} else {
234234
writer.writeNull();
235235
}
@@ -240,9 +240,9 @@ private JsonWriter toJsonMergePatch(JsonWriter jsonWriter) throws IOException {
240240
if (this.partner == null) {
241241
jsonWriter.writeNullField("partner");
242242
} else {
243-
this.partner.serializeAsJsonMergePatch(true);
243+
JsonMergePatchHelper.getFishAccessor().prepareModelForJsonMergePatch(this.partner, true);
244244
jsonWriter.writeJsonField("partner", this.partner);
245-
this.partner.serializeAsJsonMergePatch(false);
245+
JsonMergePatchHelper.getFishAccessor().prepareModelForJsonMergePatch(this.partner, false);
246246
}
247247
}
248248
return jsonWriter.writeEndObject();

typespec-tests/src/main/java/com/payload/jsonmergepatch/models/ResourcePatch.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ private JsonWriter toJsonMergePatch(JsonWriter jsonWriter) throws IOException {
290290
} else {
291291
jsonWriter.writeMapField("map", this.map, (writer, element) -> {
292292
if (element != null) {
293-
element.serializeAsJsonMergePatch(true);
293+
JsonMergePatchHelper.getInnerModelAccessor().prepareModelForJsonMergePatch(element, true);
294294
writer.writeJson(element);
295-
element.serializeAsJsonMergePatch(false);
295+
JsonMergePatchHelper.getInnerModelAccessor().prepareModelForJsonMergePatch(element, false);
296296
} else {
297297
writer.writeNull();
298298
}
@@ -324,9 +324,9 @@ private JsonWriter toJsonMergePatch(JsonWriter jsonWriter) throws IOException {
324324
if (this.innerModel == null) {
325325
jsonWriter.writeNullField("innerModel");
326326
} else {
327-
this.innerModel.serializeAsJsonMergePatch(true);
327+
JsonMergePatchHelper.getInnerModelAccessor().prepareModelForJsonMergePatch(this.innerModel, true);
328328
jsonWriter.writeJsonField("innerModel", this.innerModel);
329-
this.innerModel.serializeAsJsonMergePatch(false);
329+
JsonMergePatchHelper.getInnerModelAccessor().prepareModelForJsonMergePatch(this.innerModel, false);
330330
}
331331
}
332332
if (updatedProperties.contains("intArray")) {

0 commit comments

Comments
 (0)