Skip to content

Commit fd861fb

Browse files
authored
CUST-5203: add "maybe" to Event Status, use "maybe" instead of deprecated "tentative", and gracefully handle unknown status (#307)
1 parent 76e3a87 commit fd861fb

5 files changed

Lines changed: 130 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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ enum class EventStatus {
99
@Json(name = "confirmed")
1010
CONFIRMED,
1111

12+
@Json(name = "maybe")
13+
MAYBE,
14+
15+
@Deprecated(
16+
message = "Use MAYBE instead. TENTATIVE is a legacy alias and will be removed in a future release.",
17+
replaceWith = ReplaceWith("MAYBE"),
18+
)
1219
@Json(name = "tentative")
1320
TENTATIVE,
1421

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
9+
/**
10+
* Normalizes legacy event status values and avoids failing deserialization on new values.
11+
*/
12+
class EventStatusJsonAdapter {
13+
@FromJson
14+
fun fromJson(reader: JsonReader): EventStatus? {
15+
if (reader.peek() == JsonReader.Token.NULL) {
16+
return reader.nextNull()
17+
}
18+
19+
return when (reader.nextString()) {
20+
"confirmed" -> EventStatus.CONFIRMED
21+
"maybe", "tentative" -> EventStatus.MAYBE
22+
"cancelled" -> EventStatus.CANCELLED
23+
else -> null
24+
}
25+
}
26+
27+
@ToJson
28+
@Suppress("DEPRECATION")
29+
fun toJson(writer: JsonWriter, value: EventStatus?) {
30+
when (value) {
31+
null -> writer.nullValue()
32+
EventStatus.CONFIRMED -> writer.value("confirmed")
33+
EventStatus.MAYBE, EventStatus.TENTATIVE -> writer.value("maybe")
34+
EventStatus.CANCELLED -> writer.value("cancelled")
35+
}
36+
}
37+
}

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: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,90 @@ 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 tentative status as maybe`() {
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": "tentative",
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(EventStatus.MAYBE, event.status)
274+
}
275+
276+
@Test
277+
fun `Event deserializes unknown status as null`() {
278+
val adapter = JsonHelper.moshi().adapter(Event::class.java)
279+
val jsonBuffer =
280+
Buffer().writeUtf8(
281+
"""
282+
{
283+
"id": "5d3qmne77v32r8l4phyuksl2x",
284+
"grant_id": "41009df5-bf11-4c97-aa18-b285b5f2e386",
285+
"calendar_id": "7d93zl2palhxqdy6e5qinsakt",
286+
"object": "event",
287+
"status": "unexpected_status",
288+
"when": {
289+
"date": "2024-06-18",
290+
"object": "date"
291+
}
292+
}
293+
""".trimIndent(),
294+
)
295+
296+
val event = adapter.fromJson(jsonBuffer)!!
297+
298+
assertEquals(null, event.status)
299+
}
300+
301+
@Suppress("DEPRECATION")
302+
@Test
303+
fun `Event status serializes tentative as maybe`() {
304+
val adapter = JsonHelper.moshi().adapter(EventStatus::class.java)
305+
306+
assertEquals("\"maybe\"", adapter.toJson(EventStatus.MAYBE))
307+
assertEquals("\"maybe\"", adapter.toJson(EventStatus.TENTATIVE))
308+
}
309+
226310
@Test
227311
fun `CreateEventAutoConferencingProvider serializes properly`() {
228312
val adapter = JsonHelper.moshi().adapter(CreateEventAutoConferencingProvider::class.java)

0 commit comments

Comments
 (0)