Skip to content

Commit 48a2718

Browse files
committed
Do not lose payloadJson in trigger
1 parent 0a932c6 commit 48a2718

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

src/main/kotlin/org/phoenixframework/Channel.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class Channel(
224224

225225
// Perform when the join reply is received
226226
this.on(Event.REPLY) { message ->
227-
this.trigger(replyEventName(message.ref), message.rawPayload, message.ref, message.joinRef)
227+
this.trigger(replyEventName(message.ref), message.rawPayload, message.ref, message.joinRef, message.payloadJson)
228228
}
229229
}
230230

@@ -383,18 +383,20 @@ class Channel(
383383
event: Event,
384384
payload: Payload = hashMapOf(),
385385
ref: String = "",
386-
joinRef: String? = null
386+
joinRef: String? = null,
387+
payloadJson: String = ""
387388
) {
388-
this.trigger(event.value, payload, ref, joinRef)
389+
this.trigger(event.value, payload, ref, joinRef, payloadJson)
389390
}
390391

391392
internal fun trigger(
392393
event: String,
393394
payload: Payload = hashMapOf(),
394395
ref: String = "",
395-
joinRef: String? = null
396+
joinRef: String? = null,
397+
payloadJson: String = ""
396398
) {
397-
this.trigger(Message(joinRef, ref, topic, event, payload))
399+
this.trigger(Message(joinRef, ref, topic, event, payload, payloadJson))
398400
}
399401

400402
internal fun trigger(message: Message) {

src/test/kotlin/org/phoenixframework/DefaultsTest.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,32 @@ internal class DefaultsTest {
9797
))
9898
}
9999

100+
101+
102+
@Test
103+
internal fun `decoder decodes an error`() {
104+
val v2Json = """
105+
["6","8","drivers:self","phx_reply",{"response":{"details":"invalid code specified"},"status":"error"}]
106+
""".trimIndent()
107+
108+
val message = Defaults.decode(v2Json)
109+
assertThat(message.payloadJson).isEqualTo("{\"details\":\"invalid code specified\"}")
110+
assertThat(message.rawPayload).isEqualTo(mapOf(
111+
"response" to mapOf(
112+
"details" to "invalid code specified"
113+
),
114+
"status" to "error"
115+
))
116+
assertThat(message.payload).isEqualTo(mapOf(
117+
"details" to "invalid code specified"
118+
))
119+
120+
}
121+
100122
@Test
101123
internal fun `decoder decodes a non-json payload`() {
102124
val v2Json = """
103-
[1,2,"room:lobby","phx_reply",{"response":"hello","status":"ok"}]
125+
["1","2","room:lobby","phx_reply",{"response":"hello","status":"ok"}]
104126
""".trimIndent()
105127

106128
val message = Defaults.decode(v2Json)

src/test/kotlin/org/phoenixframework/MessageTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class MessageTest {
1414
@Test
1515
internal fun `jsonParsing parses normal message`() {
1616
val json = """
17-
[null, "6", "my-topic", "update", {"user": "James S.", "message": "This is a test"}]
17+
[null,"6","my-topic","update",{"user":"James S.","message":"This is a test"}]
1818
""".trimIndent()
1919

2020
val message = Defaults.decode.invoke(json)
@@ -30,7 +30,7 @@ class MessageTest {
3030
@Test
3131
internal fun `jsonParsing parses a reply`() {
3232
val json = """
33-
[null, "6", "my-topic", "phx_reply", {"response": {"user": "James S.","message": "This is a test"},"status": "ok"}]
33+
[null,"6","my-topic","phx_reply",{"response":{"user":"James S.","message":"This is a test"},"status": "ok"}]
3434
""".trimIndent()
3535

3636
val message = Defaults.decode.invoke(json)

0 commit comments

Comments
 (0)