Skip to content

Commit eb64d76

Browse files
committed
CUST-5203: add "maybe" to Event Status, and gracefully handle unknown status
1 parent 76e3a87 commit eb64d76

5 files changed

Lines changed: 78 additions & 1 deletion

File tree

src/main/kotlin/com/nylas/models/Event.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ data class Event(
110110
@Json(name = "reminders")
111111
val reminders: Reminders? = null,
112112
/**
113-
* Status of the event.
113+
* Status of the event. Possible values: "confirmed", "tentative", "cancelled", "maybe".
114114
*/
115115
@Json(name = "status")
116116
val status: EventStatus? = null,

src/main/kotlin/com/nylas/models/EventStatus.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ enum class EventStatus {
99
@Json(name = "confirmed")
1010
CONFIRMED,
1111

12+
@Json(name = "maybe")
13+
MAYBE,
14+
1215
@Json(name = "tentative")
1316
TENTATIVE,
1417

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.nylas.util
2+
3+
import com.nylas.models.EventStatus
4+
import com.squareup.moshi.FromJson
5+
import com.squareup.moshi.JsonReader
6+
import com.squareup.moshi.JsonWriter
7+
import com.squareup.moshi.ToJson
8+
import com.squareup.moshi.adapters.EnumJsonAdapter
9+
10+
/**
11+
* Handles EventStatus values without failing deserialization when the API returns a new status.
12+
*/
13+
class EventStatusJsonAdapter {
14+
private val delegate = EnumJsonAdapter.create(EventStatus::class.java).withUnknownFallback(null)
15+
16+
@FromJson
17+
fun fromJson(reader: JsonReader): EventStatus? = delegate.fromJson(reader)
18+
19+
@ToJson
20+
fun toJson(writer: JsonWriter, value: EventStatus?) {
21+
delegate.toJson(writer, value)
22+
}
23+
}

src/main/kotlin/com/nylas/util/JsonHelper.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class JsonHelper {
3636
.add(IMessageAdapter())
3737
.add(UpdateConnectorAdapter())
3838
.add(CredentialDataAdapter())
39+
.add(EventStatusJsonAdapter())
3940
.add(CreateAttachmentRequestAdapter())
4041
.add(MicrosoftAdminConsentCredentialDataAdapter())
4142
.add(GoogleServiceAccountCredentialDataAdapter())

src/test/kotlin/com/nylas/resources/EventsTests.kt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,56 @@ class EventsTests {
223223
assertEquals("2024-06-18", whenDate.date)
224224
}
225225

226+
@Test
227+
fun `Event deserializes maybe status properly`() {
228+
val adapter = JsonHelper.moshi().adapter(Event::class.java)
229+
val jsonBuffer =
230+
Buffer().writeUtf8(
231+
"""
232+
{
233+
"id": "5d3qmne77v32r8l4phyuksl2x",
234+
"grant_id": "41009df5-bf11-4c97-aa18-b285b5f2e386",
235+
"calendar_id": "7d93zl2palhxqdy6e5qinsakt",
236+
"object": "event",
237+
"status": "maybe",
238+
"when": {
239+
"date": "2024-06-18",
240+
"object": "date"
241+
}
242+
}
243+
""".trimIndent(),
244+
)
245+
246+
val event = adapter.fromJson(jsonBuffer)!!
247+
248+
assertEquals(EventStatus.MAYBE, event.status)
249+
}
250+
251+
@Test
252+
fun `Event deserializes unknown status as null`() {
253+
val adapter = JsonHelper.moshi().adapter(Event::class.java)
254+
val jsonBuffer =
255+
Buffer().writeUtf8(
256+
"""
257+
{
258+
"id": "5d3qmne77v32r8l4phyuksl2x",
259+
"grant_id": "41009df5-bf11-4c97-aa18-b285b5f2e386",
260+
"calendar_id": "7d93zl2palhxqdy6e5qinsakt",
261+
"object": "event",
262+
"status": "unexpected_status",
263+
"when": {
264+
"date": "2024-06-18",
265+
"object": "date"
266+
}
267+
}
268+
""".trimIndent(),
269+
)
270+
271+
val event = adapter.fromJson(jsonBuffer)!!
272+
273+
assertEquals(null, event.status)
274+
}
275+
226276
@Test
227277
fun `CreateEventAutoConferencingProvider serializes properly`() {
228278
val adapter = JsonHelper.moshi().adapter(CreateEventAutoConferencingProvider::class.java)

0 commit comments

Comments
 (0)