Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch Workaround for Deserialization of Long Properties #40301

Merged
merged 5 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions sdk/batch/azure-compute-batch/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# Release History

## 1.0.0-beta.2 (Unreleased)

### Features Added

### Breaking Changes
## 1.0.0-beta.2 (2024-05-22)

### Bugs Fixed

### Other Changes
- Fixed a bug that caused `long` properties on models to be deserialized incorrectly.

## 1.0.0-beta.1 (2024-05-16)

Expand Down
2 changes: 1 addition & 1 deletion sdk/batch/azure-compute-batch/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "java",
"TagPrefix": "java/batch/azure-compute-batch",
"Tag": "java/batch/azure-compute-batch_aff1c3044f"
"Tag": "java/batch/azure-compute-batch_70c93036ed"
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
* @throws IllegalStateException If the deserialized JSON object was missing any required properties.
* @throws IOException If an error occurs while reading the BatchJobScheduleStatistics.
*/
@Generated
public static BatchJobScheduleStatistics fromJson(JsonReader jsonReader) throws IOException {
// TODO: Re-add @Generated tag here and re-generate SDK once the 2024-05-01 Batch Service API is released
return jsonReader.readObject(reader -> {
String url = null;
OffsetDateTime startTime = null;
Expand Down Expand Up @@ -381,19 +381,78 @@ public static BatchJobScheduleStatistics fromJson(JsonReader jsonReader) throws
} else if ("wallClockTime".equals(fieldName)) {
wallClockTime = reader.getNullable(nonNullReader -> Duration.parse(nonNullReader.getString()));
} else if ("readIOps".equals(fieldName)) {
readIOps = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String readIOpsStr = reader.getString();
try {
readIOps = Long.parseLong(readIOpsStr);
} catch (NumberFormatException e) {
throw new IOException("Expected numeric readIOps, but found: " + readIOpsStr, e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
readIOps = reader.getLong();
} else {
throw new IOException("Expected readIOps to be a number or string, but found other type");
}
} else if ("writeIOps".equals(fieldName)) {
writeIOps = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String writeIOpsStr = reader.getString();
try {
writeIOps = Long.parseLong(writeIOpsStr);
} catch (NumberFormatException e) {
throw new IOException("Expected numeric writeIOps, but found: " + writeIOpsStr, e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
writeIOps = reader.getLong();
} else {
throw new IOException("Expected writeIOps to be a number or string, but found other type");
}
} else if ("readIOGiB".equals(fieldName)) {
readIOGiB = reader.getDouble();
} else if ("writeIOGiB".equals(fieldName)) {
writeIOGiB = reader.getDouble();
} else if ("numSucceededTasks".equals(fieldName)) {
numSucceededTasks = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String numSucceededTasksStr = reader.getString();
try {
numSucceededTasks = Long.parseLong(numSucceededTasksStr);
} catch (NumberFormatException e) {
throw new IOException(
"Expected numeric numSucceededTasks, but found: " + numSucceededTasksStr, e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
numSucceededTasks = reader.getLong();
} else {
throw new IOException(
"Expected numSucceededTasks to be a number or string, but found other type");
}
} else if ("numFailedTasks".equals(fieldName)) {
numFailedTasks = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String numFailedTasksStr = reader.getString();
try {
numFailedTasks = Long.parseLong(numFailedTasksStr);
} catch (NumberFormatException e) {
throw new IOException("Expected numeric numFailedTasks, but found: " + numFailedTasksStr,
e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
numFailedTasks = reader.getLong();
} else {
throw new IOException("Expected numFailedTasks to be a number or string, but found other type");
}
} else if ("numTaskRetries".equals(fieldName)) {
numTaskRetries = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String numTaskRetriesStr = reader.getString();
try {
numTaskRetries = Long.parseLong(numTaskRetriesStr);
} catch (NumberFormatException e) {
throw new IOException("Expected numeric numTaskRetries, but found: " + numTaskRetriesStr,
e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
numTaskRetries = reader.getLong();
} else {
throw new IOException("Expected numTaskRetries to be a number or string, but found other type");
}
} else if ("waitTime".equals(fieldName)) {
waitTime = reader.getNullable(nonNullReader -> Duration.parse(nonNullReader.getString()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
* @throws IllegalStateException If the deserialized JSON object was missing any required properties.
* @throws IOException If an error occurs while reading the BatchJobStatistics.
*/
@Generated
public static BatchJobStatistics fromJson(JsonReader jsonReader) throws IOException {
// TODO: Re-add @Generated tag here and re-generate SDK once the 2024-05-01 Batch Service API is released
return jsonReader.readObject(reader -> {
String url = null;
OffsetDateTime startTime = null;
Expand Down Expand Up @@ -374,19 +374,78 @@ public static BatchJobStatistics fromJson(JsonReader jsonReader) throws IOExcept
} else if ("wallClockTime".equals(fieldName)) {
wallClockTime = reader.getNullable(nonNullReader -> Duration.parse(nonNullReader.getString()));
} else if ("readIOps".equals(fieldName)) {
readIOps = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String readIOpsStr = reader.getString();
try {
readIOps = Long.parseLong(readIOpsStr);
} catch (NumberFormatException e) {
throw new IOException("Expected numeric readIOps, but found: " + readIOpsStr, e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
readIOps = reader.getLong();
} else {
throw new IOException("Expected readIOps to be a number or string, but found other type");
}
} else if ("writeIOps".equals(fieldName)) {
writeIOps = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String writeIOpsStr = reader.getString();
try {
writeIOps = Long.parseLong(writeIOpsStr);
} catch (NumberFormatException e) {
throw new IOException("Expected numeric writeIOps, but found: " + writeIOpsStr, e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
writeIOps = reader.getLong();
} else {
throw new IOException("Expected writeIOps to be a number or string, but found other type");
}
} else if ("readIOGiB".equals(fieldName)) {
readIOGiB = reader.getDouble();
} else if ("writeIOGiB".equals(fieldName)) {
writeIOGiB = reader.getDouble();
} else if ("numSucceededTasks".equals(fieldName)) {
numSucceededTasks = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String numSucceededTasksStr = reader.getString();
try {
numSucceededTasks = Long.parseLong(numSucceededTasksStr);
} catch (NumberFormatException e) {
throw new IOException(
"Expected numeric numSucceededTasks, but found: " + numSucceededTasksStr, e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
numSucceededTasks = reader.getLong();
} else {
throw new IOException(
"Expected numSucceededTasks to be a number or string, but found other type");
}
} else if ("numFailedTasks".equals(fieldName)) {
numFailedTasks = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String numFailedTasksStr = reader.getString();
try {
numFailedTasks = Long.parseLong(numFailedTasksStr);
} catch (NumberFormatException e) {
throw new IOException("Expected numeric numFailedTasks, but found: " + numFailedTasksStr,
e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
numFailedTasks = reader.getLong();
} else {
throw new IOException("Expected numFailedTasks to be a number or string, but found other type");
}
} else if ("numTaskRetries".equals(fieldName)) {
numTaskRetries = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String numTaskRetriesStr = reader.getString();
try {
numTaskRetries = Long.parseLong(numTaskRetriesStr);
} catch (NumberFormatException e) {
throw new IOException("Expected numeric numTaskRetries, but found: " + numTaskRetriesStr,
e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
numTaskRetries = reader.getLong();
} else {
throw new IOException("Expected numTaskRetries to be a number or string, but found other type");
}
} else if ("waitTime".equals(fieldName)) {
waitTime = reader.getNullable(nonNullReader -> Duration.parse(nonNullReader.getString()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
* @throws IllegalStateException If the deserialized JSON object was missing any required properties.
* @throws IOException If an error occurs while reading the BatchPoolResourceStatistics.
*/
@Generated
public static BatchPoolResourceStatistics fromJson(JsonReader jsonReader) throws IOException {
// TODO: Re-add @Generated tag here and re-generate SDK once the 2024-05-01 Batch Service API is released
return jsonReader.readObject(reader -> {
OffsetDateTime startTime = null;
OffsetDateTime lastUpdateTime = null;
Expand Down Expand Up @@ -341,9 +341,31 @@ public static BatchPoolResourceStatistics fromJson(JsonReader jsonReader) throws
} else if ("peakDiskGiB".equals(fieldName)) {
peakDiskGiB = reader.getDouble();
} else if ("diskReadIOps".equals(fieldName)) {
diskReadIOps = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String diskReadIOpsStr = reader.getString();
try {
diskReadIOps = Long.parseLong(diskReadIOpsStr);
} catch (NumberFormatException e) {
throw new IOException("Expected numeric diskReadIOps, but found: " + diskReadIOpsStr, e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
diskReadIOps = reader.getLong();
} else {
throw new IOException("Expected diskReadIOps to be a number or string, but found other type");
}
} else if ("diskWriteIOps".equals(fieldName)) {
diskWriteIOps = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String diskWriteIOpsStr = reader.getString();
try {
diskWriteIOps = Long.parseLong(diskWriteIOpsStr);
} catch (NumberFormatException e) {
throw new IOException("Expected numeric diskWriteIOps, but found: " + diskWriteIOpsStr, e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
diskWriteIOps = reader.getLong();
} else {
throw new IOException("Expected diskWriteIOps to be a number or string, but found other type");
}
} else if ("diskReadGiB".equals(fieldName)) {
diskReadGiB = reader.getDouble();
} else if ("diskWriteGiB".equals(fieldName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
* @throws IllegalStateException If the deserialized JSON object was missing any required properties.
* @throws IOException If an error occurs while reading the BatchTaskStatistics.
*/
@Generated
public static BatchTaskStatistics fromJson(JsonReader jsonReader) throws IOException {
// TODO: Re-add @Generated tag here and re-generate SDK once the 2024-05-01 Batch Service API is released
return jsonReader.readObject(reader -> {
String url = null;
OffsetDateTime startTime = null;
Expand Down Expand Up @@ -306,9 +306,31 @@ public static BatchTaskStatistics fromJson(JsonReader jsonReader) throws IOExcep
} else if ("wallClockTime".equals(fieldName)) {
wallClockTime = reader.getNullable(nonNullReader -> Duration.parse(nonNullReader.getString()));
} else if ("readIOps".equals(fieldName)) {
readIOps = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String readIOpsStr = reader.getString();
try {
readIOps = Long.parseLong(readIOpsStr);
} catch (NumberFormatException e) {
throw new IOException("Expected numeric readIOps, but found: " + readIOpsStr, e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
readIOps = reader.getLong();
} else {
throw new IOException("Expected readIOps to be a number or string, but found other type");
}
} else if ("writeIOps".equals(fieldName)) {
writeIOps = reader.getLong();
if (reader.currentToken() == JsonToken.STRING) {
String writeIOpsStr = reader.getString();
try {
writeIOps = Long.parseLong(writeIOpsStr);
} catch (NumberFormatException e) {
throw new IOException("Expected numeric writeIOps, but found: " + writeIOpsStr, e);
}
} else if (reader.currentToken() == JsonToken.NUMBER) {
writeIOps = reader.getLong();
} else {
throw new IOException("Expected writeIOps to be a number or string, but found other type");
}
} else if ("readIOGiB".equals(fieldName)) {
readIOGiB = reader.getDouble();
} else if ("writeIOGiB".equals(fieldName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
* @throws IOException If an error occurs while reading the FileProperties.
*/
public static FileProperties fromJson(JsonReader jsonReader) throws IOException {
// TODO: Re-add @Generated tag here and re-generate SDK once the 2024-05-01 Batch Service API is released
return jsonReader.readObject(reader -> {
OffsetDateTime lastModified = null;
long contentLength = 0L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
import com.azure.core.http.rest.Response;
import com.azure.core.test.TestMode;
import com.azure.core.util.BinaryData;
import com.azure.json.JsonProviders;
import com.azure.json.JsonReader;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.concurrent.TimeoutException;

public class FileTests extends BatchClientTestBase {
Expand Down Expand Up @@ -137,4 +142,28 @@ public void canReadFromNode() throws Exception {
}
}
}

@Test
public void testDeserializationOfFileProperties() throws IOException {
String jsonResponse = "{"
+ "\"lastModified\":\"2022-01-01T00:00:00Z\","
+ "\"contentLength\":\"1024\","
+ "\"creationTime\":\"2022-01-01T01:00:00Z\","
+ "\"contentType\":\"application/json\","
+ "\"fileMode\":\"rw-r--r--\""
+ "}";

try (JsonReader jsonReader = JsonProviders.createReader(new StringReader(jsonResponse))) {
FileProperties fileProperties = FileProperties.fromJson(jsonReader);

Assertions.assertNotNull(fileProperties);
Assertions.assertEquals(OffsetDateTime.parse("2022-01-01T00:00:00Z"), fileProperties.getLastModified());
Assertions.assertEquals(1024, fileProperties.getContentLength());
Assertions.assertEquals(OffsetDateTime.parse("2022-01-01T01:00:00Z"), fileProperties.getCreationTime());
Assertions.assertEquals("application/json", fileProperties.getContentType());
Assertions.assertEquals("rw-r--r--", fileProperties.getFileMode());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Loading