Skip to content

Commit 38ae270

Browse files
authored
fix: correctly serialize maps which use the Document type as a value (#1029)
1 parent 3a2a9c7 commit 38ae270

File tree

7 files changed

+93
-0
lines changed

7 files changed

+93
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "581b0e21-e59b-427c-a959-276f09276b88",
3+
"type": "bugfix",
4+
"description": "Correctly serialize maps which use the `Document` type as a value"
5+
}

runtime/serde/common/src/aws/smithy/kotlin/runtime/serde/Serializer.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,15 @@ public interface MapSerializer : PrimitiveSerializer {
328328
*/
329329
public fun entry(key: String, value: SdkSerializable?)
330330

331+
/**
332+
* Writes the key given in the descriptor, and then
333+
* serializes value.
334+
*
335+
* @param key
336+
* @param value
337+
*/
338+
public fun entry(key: String, value: Document?)
339+
331340
/**
332341
* Writes the field name given in the descriptor, and then
333342
* serializes the list field using the given block.

runtime/serde/serde-form-url/common/src/aws/smithy/kotlin/runtime/serde/formurl/FormUrlSerializer.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ private class FormUrlMapSerializer(
333333
value.serialize(FormUrlSerializer(buffer, nestedPrefix))
334334
}
335335

336+
override fun entry(key: String, value: Document?) =
337+
throw SerializationException("document values not supported by form-url serializer")
338+
336339
override fun listEntry(key: String, listDescriptor: SdkFieldDescriptor, block: ListSerializer.() -> Unit) {
337340
writeKey(key)
338341

runtime/serde/serde-json/common/src/aws/smithy/kotlin/runtime/serde/json/JsonSerializer.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ public class JsonSerializer : Serializer, ListSerializer, MapSerializer, StructS
197197
if (value != null) serializeInstant(value, format) else jsonWriter.writeNull()
198198
}
199199

200+
override fun entry(key: String, value: Document?) {
201+
jsonWriter.writeName(key)
202+
serializeDocument(value)
203+
}
204+
200205
override fun listEntry(key: String, listDescriptor: SdkFieldDescriptor, block: ListSerializer.() -> Unit) {
201206
jsonWriter.writeName(key)
202207
beginList(listDescriptor)

runtime/serde/serde-json/common/test/aws/smithy/kotlin/runtime/serde/json/JsonDeserializerTest.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,49 @@ class JsonDeserializerTest {
226226
actual.shouldContainExactly(expected)
227227
}
228228

229+
@Test
230+
fun itHandlesMapsOfDocuments() {
231+
val payload = """
232+
{
233+
"key1": {
234+
"number": 12,
235+
"string": "foo",
236+
"bool": true,
237+
"null": null
238+
},
239+
"key2": {
240+
"number": 4.5,
241+
"string": "bar",
242+
"bool": false,
243+
"null": null
244+
}
245+
}
246+
""".trimIndent().encodeToByteArray()
247+
val deserializer: Deserializer = JsonDeserializer(payload)
248+
val actual = deserializer.deserializeMap(SdkFieldDescriptor(SerialKind.Map)) {
249+
val map = mutableMapOf<String, Document>()
250+
while (hasNextEntry()) {
251+
map[key()] = deserializeDocument()
252+
}
253+
return@deserializeMap map
254+
}
255+
val expected = mapOf(
256+
"key1" to buildDocument {
257+
"number" to 12L
258+
"string" to "foo"
259+
"bool" to true
260+
"null" to null
261+
},
262+
"key2" to buildDocument {
263+
"number" to 4.5
264+
"string" to "bar"
265+
"bool" to false
266+
"null" to null
267+
},
268+
)
269+
actual.shouldContainExactly(expected)
270+
}
271+
229272
@Test
230273
fun itChecksNullValuesOfNonSparseMaps() {
231274
val payload = """

runtime/serde/serde-json/common/test/aws/smithy/kotlin/runtime/serde/json/JsonSerializerTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,31 @@ class JsonSerializerTest {
9696
assertEquals("""{"A1":{"b":{"value":1}},"A2":{"b":{"value":2}},"A3":{"b":{"value":3}}}""", json.toByteArray().decodeToString())
9797
}
9898

99+
@Test
100+
fun canSerializeMapOfDocuments() {
101+
val objs = mapOf(
102+
"A1" to buildDocument {
103+
"number" to 12L
104+
"string" to "foo"
105+
"bool" to true
106+
"null" to null
107+
},
108+
"B2" to buildDocument {
109+
"number" to 4.5
110+
"string" to "bar"
111+
"bool" to false
112+
"null" to null
113+
},
114+
)
115+
val json = JsonSerializer()
116+
json.serializeMap(testAnonObjDescriptor) {
117+
for (obj in objs) {
118+
entry(obj.key, obj.value)
119+
}
120+
}
121+
assertEquals("""{"A1":{"number":12,"string":"foo","bool":true,"null":null},"B2":{"number":4.5,"string":"bar","bool":false,"null":null}}""", json.toByteArray().decodeToString())
122+
}
123+
99124
@Test
100125
fun canSerializeMapOfLists() {
101126
val objs = mapOf(

runtime/serde/serde-xml/common/src/aws/smithy/kotlin/runtime/serde/xml/XmlSerializer.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ private class XmlMapSerializer(
259259

260260
override fun entry(key: String, value: Instant?, format: TimestampFormat): Unit = entry(key, value?.format(format))
261261

262+
override fun entry(key: String, value: Document?) =
263+
throw SerializationException("document values not supported by xml serializer")
264+
262265
override fun listEntry(key: String, listDescriptor: SdkFieldDescriptor, block: ListSerializer.() -> Unit) {
263266
writeEntry(key) {
264267
val ls = xmlSerializer.beginList(listDescriptor)

0 commit comments

Comments
 (0)