Skip to content

Commit 0055ce3

Browse files
authored
[surepetcare] Fix DateTimeParseException (openhab#16087)
Fixes openhab#16082 Signed-off-by: Jacob Laursen <[email protected]>
1 parent 90f66cb commit 0055ce3

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/utils/GsonZonedDateTimeTypeAdapter.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.lang.reflect.Type;
1616
import java.time.ZonedDateTime;
1717
import java.time.format.DateTimeFormatter;
18+
import java.time.format.DateTimeParseException;
1819

1920
import org.eclipse.jdt.annotation.NonNullByDefault;
2021
import org.eclipse.jdt.annotation.Nullable;
@@ -76,6 +77,11 @@ public JsonElement serialize(ZonedDateTime src, Type typeOfSrc, JsonSerializatio
7677
@Override
7778
public @Nullable ZonedDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
7879
throws JsonParseException {
79-
return ZONED_FORMATTER.parse(json.getAsString(), ZonedDateTime::from);
80+
String content = json.getAsString();
81+
try {
82+
return ZonedDateTime.parse(content);
83+
} catch (DateTimeParseException e) {
84+
throw new JsonParseException("Could not parse as ZonedDateTime: " + content, e);
85+
}
8086
}
8187
}

bundles/org.openhab.binding.surepetcare/src/test/java/org/openhab/binding/surepetcare/internal/data/SurePetcareTopologyTest.java

+48
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
import static org.junit.jupiter.api.Assertions.*;
1616

17+
import java.time.ZoneOffset;
18+
import java.time.ZonedDateTime;
19+
1720
import org.eclipse.jdt.annotation.NonNullByDefault;
1821
import org.junit.jupiter.api.Test;
1922
import org.openhab.binding.surepetcare.internal.SurePetcareConstants;
@@ -78,4 +81,49 @@ public void testGetUserNull() {
7881
fail("GSON returned null");
7982
}
8083
}
84+
85+
@Test
86+
public void testDateFormats() {
87+
String testResponse = """
88+
{
89+
"devices": [],
90+
"households": [
91+
{
92+
"id": 0,
93+
"name": "***",
94+
"share_code": "***",
95+
"created_user_id": 0,
96+
"timezone_id": 374,
97+
"version": "MTE=",
98+
"created_at": "2021-04-24T11:41:15+00:00",
99+
"updated_at": "2023-12-16T21:08:19.637892+00:00",
100+
"invites": [],
101+
"users": [],
102+
"timezone": {
103+
"id": 374,
104+
"name": "(UTC+02:00) Europe/Zurich",
105+
"timezone": "Europe/Zurich",
106+
"utc_offset": 7200,
107+
"created_at": "2017-08-03T08:35:34+00:00",
108+
"updated_at": "2017-08-03T08:37:15+00:00"
109+
}
110+
}
111+
],
112+
"pets": [],
113+
"photos": [],
114+
"tags": [],
115+
"user": {}
116+
}
117+
""";
118+
119+
SurePetcareTopology response = SurePetcareConstants.GSON.fromJson(testResponse, SurePetcareTopology.class);
120+
121+
assertNotNull(response);
122+
assertNotNull(response.households);
123+
assertEquals(1, response.households.size());
124+
assertEquals(ZonedDateTime.of(2021, 4, 24, 11, 41, 15, 0, ZoneOffset.UTC),
125+
response.households.get(0).createdAt);
126+
assertEquals(ZonedDateTime.of(2023, 12, 16, 21, 8, 19, 637892000, ZoneOffset.UTC),
127+
response.households.get(0).updatedAt);
128+
}
81129
}

0 commit comments

Comments
 (0)