Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/704b9a6e-d859-4706-b39f-91052b375834.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "704b9a6e-d859-4706-b39f-91052b375834",
"type": "misc",
"description": "Add unbound event stream payload deserialization path"
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,14 @@ class EventStreamParserGenerator(
} else {
if (unbound.isNotEmpty()) {
// all remaining members are bound to payload (but not explicitly bound via @eventPayload)
// FIXME - this would require us to generate a "partial" deserializer like we do for where the builder is passed in
TODO("support for unbound event stream members is not implemented")
// generate a payload deserializer specific to the unbound members (note this will be a deserializer
// for the overall event shape but only payload members will be considered for deserialization),
// and then assign each deserialized payload member to the current builder instance
val payloadDeserializeFn = sdg.payloadDeserializer(ctx, member, unbound)
writer.write("val tmp = #T(message.payload)", payloadDeserializeFn)
unbound.forEach {
writer.write("eb.#1L = tmp.#1L", it.defaultName())
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ structure MessageWithNoHeaderPayloadTraits {
someString: String,
}

structure MessageWithUnboundPayloadTraits {
@eventHeader header: String,
unboundString: String,
}

@streaming
union TestStream {
MessageWithBlob: MessageWithBlob,
Expand All @@ -73,5 +78,6 @@ union TestStream {
MessageWithHeaders: MessageWithHeaders,
MessageWithHeaderAndPayload: MessageWithHeaderAndPayload,
MessageWithNoHeaderPayloadTraits: MessageWithNoHeaderPayloadTraits,
MessageWithUnboundPayloadTraits: MessageWithUnboundPayloadTraits,
SomeError: SomeError,
}
26 changes: 26 additions & 0 deletions tests/codegen/event-stream/src/test/kotlin/EventStreamTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,30 @@ class EventStreamTests {
assertIs<TestStream.MessageWithNoHeaderPayloadTraits>(deserialized)
assertEquals(event, deserialized)
}

@Test
fun testSerializeMessageWithUnboundPayload() = runTest {
val event = TestStream.MessageWithUnboundPayloadTraits(
MessageWithUnboundPayloadTraits {
header = "a korf is a tiger"
unboundString = "a flix is comb"
},
)

val message = serializedMessage(event)

val headers = message.headers.associate { it.name to it.value }
assertEquals("event", headers[":message-type"]?.expectString())
assertEquals("MessageWithUnboundPayloadTraits", headers[":event-type"]?.expectString())
assertEquals("application/json", headers[":content-type"]?.expectString())

assertEquals("a korf is a tiger", headers["header"]?.expectString())

val expectedBody = """{"unboundString":"a flix is comb"}"""
assertJsonStringsEqual(expectedBody, message.payload.decodeToString())

val deserialized = deserializedEvent(message)
assertIs<TestStream.MessageWithUnboundPayloadTraits>(deserialized)
assertEquals(event, deserialized)
}
}