Skip to content

Commit 6d3485c

Browse files
author
Jonathan Wenger
committed
Fix datetime parser issue
1 parent 5a48a3d commit 6d3485c

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/main/java/Avalara/SDK/JSON.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import java.time.LocalDate;
1414
import java.time.OffsetDateTime;
1515
import java.time.format.DateTimeFormatter;
16-
import Avalara.SDK.model.A1099.V2.*;
16+
import Avalara.SDK.model.A1099.V2.*;
1717
import Avalara.SDK.model.EInvoicing.V1.*;
1818

1919
import okio.ByteString;
@@ -40,8 +40,7 @@ public class JSON {
4040

4141
@SuppressWarnings("unchecked")
4242
public static GsonBuilder createGson() {
43-
GsonFireBuilder fireBuilder = new GsonFireBuilder()
44-
;
43+
GsonFireBuilder fireBuilder = new GsonFireBuilder();
4544
GsonBuilder builder = fireBuilder.createGsonBuilder();
4645
return builder;
4746
}
@@ -55,10 +54,13 @@ private static String getDiscriminatorValue(JsonElement readElement, String disc
5554
}
5655

5756
/**
58-
* Returns the Java class that implements the OpenAPI schema for the specified discriminator value.
57+
* Returns the Java class that implements the OpenAPI schema for the specified
58+
* discriminator value.
5959
*
60-
* @param classByDiscriminatorValue The map of discriminator values to Java classes.
61-
* @param discriminatorValue The value of the OpenAPI discriminator in the input data.
60+
* @param classByDiscriminatorValue The map of discriminator values to Java
61+
* classes.
62+
* @param discriminatorValue The value of the OpenAPI discriminator in
63+
* the input data.
6264
* @return The Java class that implements the OpenAPI schema
6365
*/
6466
private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) {
@@ -71,12 +73,12 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri
7173

7274
public JSON() {
7375
gson = createGson()
74-
.registerTypeAdapter(Date.class, dateTypeAdapter)
75-
.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
76-
.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
77-
.registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
78-
.registerTypeAdapter(byte[].class, byteArrayAdapter)
79-
.create();
76+
.registerTypeAdapter(Date.class, dateTypeAdapter)
77+
.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
78+
.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
79+
.registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
80+
.registerTypeAdapter(byte[].class, byteArrayAdapter)
81+
.create();
8082
}
8183

8284
/**
@@ -104,7 +106,8 @@ public JSON setGson(Gson gson) {
104106
*
105107
* @param lenientOnJson Set it to true to ignore some syntax errors
106108
* @return JSON
107-
* @see <a href="https://www.javadoc.io/doc/com.google.code.gson/gson/2.8.5/com/google/gson/stream/JsonReader.html">https://www.javadoc.io/doc/com.google.code.gson/gson/2.8.5/com/google/gson/stream/JsonReader.html</a>
109+
* @see <a href=
110+
* "https://www.javadoc.io/doc/com.google.code.gson/gson/2.8.5/com/google/gson/stream/JsonReader.html">https://www.javadoc.io/doc/com.google.code.gson/gson/2.8.5/com/google/gson/stream/JsonReader.html</a>
108111
*/
109112
public JSON setLenientOnJson(boolean lenientOnJson) {
110113
isLenientOnJson = lenientOnJson;
@@ -134,7 +137,8 @@ public <T> T deserialize(String body, Type returnType) {
134137
try {
135138
if (isLenientOnJson) {
136139
JsonReader jsonReader = new JsonReader(new StringReader(body));
137-
// see https://www.javadoc.io/doc/com.google.code.gson/gson/2.8.5/com/google/gson/stream/JsonReader.html
140+
// see
141+
// https://www.javadoc.io/doc/com.google.code.gson/gson/2.8.5/com/google/gson/stream/JsonReader.html
138142
jsonReader.setLenient(true);
139143
return gson.fromJson(jsonReader, returnType);
140144
} else {
@@ -216,7 +220,13 @@ public OffsetDateTime read(JsonReader in) throws IOException {
216220
default:
217221
String date = in.nextString();
218222
if (date.endsWith("+0000")) {
219-
date = date.substring(0, date.length()-5) + "Z";
223+
date = date.substring(0, date.length() - 5) + "Z";
224+
}
225+
// Handle datetime strings without timezone information by appending 'Z'
226+
// This addresses A1099 API responses that return local datetime strings
227+
// like '2025-05-06T23:33:25.358690' without timezone indicators
228+
else if (!date.endsWith("Z") && !date.matches(".*[+-]\\d{2}:?\\d{2}$")) {
229+
date = date + "Z";
220230
}
221231
return OffsetDateTime.parse(date, formatter);
222232
}
@@ -283,7 +293,8 @@ public static class SqlDateTypeAdapter extends TypeAdapter<java.sql.Date> {
283293

284294
private DateFormat dateFormat;
285295

286-
public SqlDateTypeAdapter() {}
296+
public SqlDateTypeAdapter() {
297+
}
287298

288299
public SqlDateTypeAdapter(DateFormat dateFormat) {
289300
this.dateFormat = dateFormat;
@@ -336,7 +347,8 @@ public static class DateTypeAdapter extends TypeAdapter<Date> {
336347

337348
private DateFormat dateFormat;
338349

339-
public DateTypeAdapter() {}
350+
public DateTypeAdapter() {
351+
}
340352

341353
public DateTypeAdapter(DateFormat dateFormat) {
342354
this.dateFormat = dateFormat;

0 commit comments

Comments
 (0)