Skip to content

Commit

Permalink
Clean up patch serialization (#2772)
Browse files Browse the repository at this point in the history
Clean up patch serialization
  • Loading branch information
alzimmermsft authored May 17, 2024
1 parent 74b9e9f commit 0cea1dd
Show file tree
Hide file tree
Showing 53 changed files with 187 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@Fluent
public final class DocumentLanguage implements JsonSerializable<DocumentLanguage> {
/*
* Detected language. Value may an ISO 639-1 language code (ex. "en", "fr") or BCP 47 language tag (ex. "zh-Hans").
* Detected language. Value may an ISO 639-1 language code (ex. "en", "fr") or BCP 47 language tag (ex. "zh-Hans").
*/
private String locale;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public final class DocumentPage implements JsonSerializable<DocumentPage> {
private Float height;

/*
* The unit used by the width, height, and polygon properties. For images, the unit is "pixel". For PDF, the unit is "inch".
* The unit used by the width, height, and polygon properties. For images, the unit is "pixel". For PDF, the unit is
* "inch".
*/
private LengthUnit unit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public class ArtifactManifestPropertiesInternal implements JsonSerializable<Arti
private ArtifactOperatingSystem operatingSystem;

/*
* List of artifacts that are referenced by this manifest list, with information about the platform each supports. This list will be empty if this is a leaf manifest and not a manifest list.
* List of artifacts that are referenced by this manifest list, with information about the platform each supports.
* This list will be empty if this is a leaf manifest and not a manifest list.
*/
private List<ArtifactManifestPlatform> relatedArtifacts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public class ManifestAttributesBase implements JsonSerializable<ManifestAttribut
private ArtifactOperatingSystem operatingSystem;

/*
* List of artifacts that are referenced by this manifest list, with information about the platform each supports. This list will be empty if this is a leaf manifest and not a manifest list.
* List of artifacts that are referenced by this manifest list, with information about the platform each supports.
* This list will be empty if this is a leaf manifest and not a manifest list.
*/
private List<ArtifactManifestPlatform> relatedArtifacts;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
@Fluent
public final class ManifestListAttributes implements JsonSerializable<ManifestListAttributes> {
/*
* The MIME type of the referenced object. This will generally be application/vnd.docker.image.manifest.v2+json, but it could also be application/vnd.docker.image.manifest.v1+json
* The MIME type of the referenced object. This will generally be application/vnd.docker.image.manifest.v2+json, but
* it could also be application/vnd.docker.image.manifest.v1+json
*/
private String mediaType;

Expand All @@ -32,7 +33,8 @@ public final class ManifestListAttributes implements JsonSerializable<ManifestLi
private String digest;

/*
* The platform object describes the platform which the image in the manifest runs on. A full list of valid operating system and architecture values are listed in the Go language documentation for $GOOS and $GOARCH
* The platform object describes the platform which the image in the manifest runs on. A full list of valid
* operating system and architecture values are listed in the Go language documentation for $GOOS and $GOARCH
*/
private Platform platform;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public final class PathsV3R3RxOauth2TokenPostRequestbodyContentApplicationXWwwFo
private String service;

/*
* Which is expected to be a valid scope, and can be specified more than once for multiple scope requests. You obtained this from the Www-Authenticate response header from the challenge.
* Which is expected to be a valid scope, and can be specified more than once for multiple scope requests. You
* obtained this from the Www-Authenticate response header from the challenge.
*/
private String scope;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,20 @@ public final class Platform implements JsonSerializable<Platform> {
private String osVersion;

/*
* The optional os.features field specifies an array of strings, each listing a required OS feature (for example on Windows win32k
* The optional os.features field specifies an array of strings, each listing a required OS feature (for example on
* Windows win32k
*/
private List<String> osFeatures;

/*
* The optional variant field specifies a variant of the CPU, for example armv6l to specify a particular CPU variant of the ARM CPU.
* The optional variant field specifies a variant of the CPU, for example armv6l to specify a particular CPU variant
* of the ARM CPU.
*/
private String variant;

/*
* The optional features field specifies an array of strings, each listing a required CPU feature (for example sse4 or aes
* The optional features field specifies an array of strings, each listing a required CPU feature (for example sse4
* or aes
*/
private List<String> features;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
@Fluent
public final class OciAnnotations implements JsonSerializable<OciAnnotations> {
/*
* Date and time on which the image was built (string, date-time as defined by https://tools.ietf.org/html/rfc3339#section-5.6)
* Date and time on which the image was built (string, date-time as defined by
* https://tools.ietf.org/html/rfc3339#section-5.6)
*/
private OffsetDateTime createdOn;

Expand All @@ -46,7 +47,8 @@ public final class OciAnnotations implements JsonSerializable<OciAnnotations> {
private String source;

/*
* Version of the packaged software. The version MAY match a label or tag in the source code repository, may also be Semantic versioning-compatible
* Version of the packaged software. The version MAY match a label or tag in the source code repository, may also be
* Semantic versioning-compatible
*/
private String version;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ public AutoRestAzureSpecialParametersTestClientBuilder configuration(Configurati
}

/*
* The subscription id, which appears in the path, always modeled in credentials. The value is always '1234-5678-9012-3456'
* The subscription id, which appears in the path, always modeled in credentials. The value is always
* '1234-5678-9012-3456'
*/
@Generated
private String subscriptionId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public String jsonDeserializationMethod(String jsonReaderName) {
}

@Override
public String jsonSerializationMethodCall(String jsonWriterName, String fieldName, String valueGetter) {
public String jsonSerializationMethodCall(String jsonWriterName, String fieldName, String valueGetter,
boolean jsonMergePatch) {
return fieldName == null
? String.format("%s.writeBinary(%s)", jsonWriterName, valueGetter)
: String.format("%s.writeBinaryField(\"%s\", %s)", jsonWriterName, fieldName, valueGetter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,8 @@ public String jsonDeserializationMethod(String jsonReaderName) {
}

@Override
public String jsonSerializationMethodCall(String jsonWriterName, String fieldName, String valueGetter) {
public String jsonSerializationMethodCall(String jsonWriterName, String fieldName, String valueGetter,
boolean jsonMergePatch) {
if (!isSwaggerType && CoreUtils.isNullOrEmpty(serializationMethodBase)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,16 @@ public String jsonDeserializationMethod(String jsonReaderName) {
}

@Override
public String jsonSerializationMethodCall(String jsonWriterName, String fieldName, String valueGetter) {
String actualValueGetter = valueGetter + " == null ? null : " + valueGetter + "." + getToMethodName() + "()";
return elementType.asNullable().jsonSerializationMethodCall(jsonWriterName, fieldName, actualValueGetter);
public String jsonSerializationMethodCall(String jsonWriterName, String fieldName, String valueGetter,
boolean jsonMergePatch) {
// When JSON merge patch is being used the valueGetter will already be null checked as JSON merge patch needs to
// explicitly handle null values.
String actualValueGetter = jsonMergePatch
? valueGetter + "." + getToMethodName() + "()"
: valueGetter + " == null ? null : " + valueGetter + "." + getToMethodName() + "()";

return elementType.asNullable().jsonSerializationMethodCall(jsonWriterName, fieldName, actualValueGetter,
jsonMergePatch);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ public final String jsonDeserializationMethod(String jsonReaderName) {
}

@Override
public final String jsonSerializationMethodCall(String jsonWriterName, String fieldName, String valueGetter) {
public final String jsonSerializationMethodCall(String jsonWriterName, String fieldName, String valueGetter,
boolean jsonMergePatch) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ default boolean isNullable() {
* @param jsonWriterName The name of the {@link JsonWriter} performing serialization.
* @param fieldName The name of the JSON field, optional.
* @param valueGetter The value getter.
* @param jsonMergePatch Flag indicating if the serialization call is for a JSON merge patch operation.
* @return The method call that will handle JSON serialization, or null if it isn't supported directly.
*/
String jsonSerializationMethodCall(String jsonWriterName, String fieldName, String valueGetter);
String jsonSerializationMethodCall(String jsonWriterName, String fieldName, String valueGetter,
boolean jsonMergePatch);

/**
* Gets the method that handles XML deserialization for the type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ public String jsonDeserializationMethod(String jsonReaderName) {
}

@Override
public String jsonSerializationMethodCall(String jsonWriterName, String fieldName, String valueGetter) {
public String jsonSerializationMethodCall(String jsonWriterName, String fieldName, String valueGetter,
boolean jsonMergePatch) {
if (wrapSerializationWithObjectsToString) {
return fieldName == null
? String.format("%s.%s(Objects.toString(%s, null))", jsonWriterName, serializationMethodBase, valueGetter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private static void serializeJsonProperty(JavaBlock methodBlock, ClientModelProp
if (isJsonMergePatch) {
if (property.getClientType().isNullable()) {
methodBlock.ifBlock(String.format("updatedProperties.contains(\"%s\")", property.getName()), codeBlock -> {
codeBlock.ifBlock(String.format("%s==null", getPropertyGetterStatement(property, fromSuperType)), ifBlock -> {
codeBlock.ifBlock(String.format("%s == null", getPropertyGetterStatement(property, fromSuperType)), ifBlock -> {
ifBlock.line(String.format("jsonWriter.writeNullField(\"%s\");", property.getSerializedName()));
}).elseBlock(elseBlock -> {
serializeJsonProperty(codeBlock, property, serializedName, fromSuperType, isJsonMergePatch);
Expand Down Expand Up @@ -380,7 +380,7 @@ private static void serializeJsonProperty(JavaBlock methodBlock, ClientModelProp
// Attempt to determine whether the wire type is simple serialization.
// This is primitives, boxed primitives, a small set of string based models, and other ClientModels.
String fieldSerializationMethod = wireType.jsonSerializationMethodCall("jsonWriter", serializedName,
propertyValueGetter);
propertyValueGetter, isJsonMergePatch);
if (wireType == ClassType.BINARY_DATA) {
// Special handling for BinaryData (instead of using "serializationMethodBase" and "serializationValueGetterModifier")
// The reason is that some backend would fail the request on "null" value (e.g. OpenAI)
Expand Down Expand Up @@ -460,7 +460,8 @@ private static void serializeJsonContainerProperty(JavaBlock methodBlock, String
String callingWriterName = depth == 0 ? "jsonWriter" : (depth == 1) ? "writer" : "writer" + (depth - 1);
String lambdaWriterName = depth == 0 ? "writer" : "writer" + depth;
String elementName = depth == 0 ? "element" : "element" + depth;
String valueSerializationMethod = elementType.jsonSerializationMethodCall(lambdaWriterName, null, elementName);
String valueSerializationMethod = elementType.jsonSerializationMethodCall(lambdaWriterName, null, elementName,
isJsonMergePatch);
String serializeValue = depth == 0 ? propertyValueGetter
: ((depth == 1) ? "element" : "element" + (depth - 1));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_if" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_arguments" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_string_concatenation" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.format_line_comments" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.comment.format_line_comments" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.align_selector_in_method_invocation_on_expression_first_line" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_record_declaration" value="end_of_line"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_colon_in_labeled_statement" value="insert"/>
Expand Down Expand Up @@ -358,7 +358,7 @@
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_postfix_operator" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_arguments" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_cast" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.comment.format_block_comments" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.comment.format_block_comments" value="true"/>
<setting id="org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration" value="16"/>
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_declaration_throws" value="insert"/>
<setting id="org.eclipse.jdt.core.formatter.blank_lines_after_last_class_body_declaration" value="0"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
@Immutable
public final class OperationInner {
/*
* The name of the operation, as per Resource-Based Access Control (RBAC). Examples: "Microsoft.Compute/virtualMachines/write", "Microsoft.Compute/virtualMachines/capture/action"
* The name of the operation, as per Resource-Based Access Control (RBAC). Examples:
* "Microsoft.Compute/virtualMachines/write", "Microsoft.Compute/virtualMachines/capture/action"
*/
@JsonProperty(value = "name", access = JsonProperty.Access.WRITE_ONLY)
private String name;

/*
* Whether the operation applies to data-plane. This is "true" for data-plane operations and "false" for Azure Resource Manager/control-plane operations.
* Whether the operation applies to data-plane. This is "true" for data-plane operations and "false" for Azure
* Resource Manager/control-plane operations.
*/
@JsonProperty(value = "isDataAction", access = JsonProperty.Access.WRITE_ONLY)
private Boolean isDataAction;
Expand All @@ -34,7 +36,8 @@ public final class OperationInner {
private OperationDisplay display;

/*
* The intended executor of the operation; as in Resource Based Access Control (RBAC) and audit logs UX. Default value is "user,system"
* The intended executor of the operation; as in Resource Based Access Control (RBAC) and audit logs UX. Default
* value is "user,system"
*/
@JsonProperty(value = "origin", access = JsonProperty.Access.WRITE_ONLY)
private Origin origin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@
@Immutable
public final class OperationDisplay {
/*
* The localized friendly form of the resource provider name, e.g. "Microsoft Monitoring Insights" or "Microsoft Compute".
* The localized friendly form of the resource provider name, e.g. "Microsoft Monitoring Insights" or
* "Microsoft Compute".
*/
@JsonProperty(value = "provider")
private String provider;

/*
* The localized friendly name of the resource type related to this operation. E.g. "Virtual Machines" or "Job Schedule Collections".
* The localized friendly name of the resource type related to this operation. E.g. "Virtual Machines" or
* "Job Schedule Collections".
*/
@JsonProperty(value = "resource")
private String resource;

/*
* The concise, localized friendly name for the operation; suitable for dropdowns. E.g. "Create or Update Virtual Machine", "Restart Virtual Machine".
* The concise, localized friendly name for the operation; suitable for dropdowns. E.g.
* "Create or Update Virtual Machine", "Restart Virtual Machine".
*/
@JsonProperty(value = "operation")
private String operation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
@Fluent
public final class UserAssignedIdentities {
/*
* The set of user assigned identities associated with the resource. The userAssignedIdentities dictionary keys will be ARM resource ids in the form: '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName}. The dictionary values can be empty objects ({}) in requests.",
* The set of user assigned identities associated with the resource. The userAssignedIdentities dictionary keys will
* be ARM resource ids in the form:
* '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/
* userAssignedIdentities/{identityName}. The dictionary values can be empty objects ({}) in requests.",
*
* Additional properties
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ public Mono<Response<BinaryData>> updateWithResponse(long id, BinaryData request
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
Mono<Response<Void>> uploadFileWithResponse(String name, BinaryData request, RequestOptions requestOptions) {
// Protocol API requires serialization of parts with content-disposition and data, as operation 'uploadFile' is 'multipart/form-data'
// Protocol API requires serialization of parts with content-disposition and data, as operation 'uploadFile' is
// 'multipart/form-data'
return this.serviceClient.uploadFileWithResponseAsync(name, request, requestOptions);
}

Expand All @@ -228,7 +229,8 @@ Mono<Response<Void>> uploadFileWithResponse(String name, BinaryData request, Req
@Generated
@ServiceMethod(returns = ReturnType.SINGLE)
Mono<Response<Void>> uploadTodoWithResponse(BinaryData request, RequestOptions requestOptions) {
// Protocol API requires serialization of parts with content-disposition and data, as operation 'uploadTodo' is 'multipart/form-data'
// Protocol API requires serialization of parts with content-disposition and data, as operation 'uploadTodo' is
// 'multipart/form-data'
return this.serviceClient.uploadTodoWithResponseAsync(request, requestOptions);
}

Expand Down
Loading

0 comments on commit 0cea1dd

Please sign in to comment.