File tree Expand file tree Collapse file tree 4 files changed +55
-0
lines changed
ktor-server/ktor-server-core
src/io/ktor/server/config
test/io/ktor/server/config
src/io/ktor/server/config
test/io/ktor/tests/config Expand file tree Collapse file tree 4 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,8 @@ internal class MapConfigDecoder(
59
59
return if (map.containsPrefix(fullPath)) {
60
60
currentPath = fullPath
61
61
newIndex
62
+ } else if (descriptor.isElementOptional(newIndex)) {
63
+ decodeElementIndex(descriptor)
62
64
} else {
63
65
CompositeDecoder .DECODE_DONE
64
66
}
Original file line number Diff line number Diff line change @@ -50,6 +50,14 @@ class MapDecoderTest {
50
50
val enumMap : Map <String , TestEnum >
51
51
)
52
52
53
+ @Serializable
54
+ data class Address (
55
+ val streetName : String ,
56
+ val postalCode : String ,
57
+ val unitNumber : String? = null ,
58
+ val municipality : String ,
59
+ )
60
+
53
61
@Test
54
62
fun testSimpleTypes () {
55
63
val map = mapOf (
@@ -206,4 +214,19 @@ class MapDecoderTest {
206
214
SimpleConfig .serializer().deserialize(decoder)
207
215
}
208
216
}
217
+
218
+ @Test
219
+ fun testOptionals () {
220
+ val map = mapOf (
221
+ " streetName" to " Test street" ,
222
+ " postalCode" to " 12345" ,
223
+ " municipality" to " Test municipality"
224
+ )
225
+ val decoder = MapConfigDecoder (map)
226
+ val config = Address .serializer().deserialize(decoder)
227
+ assertEquals(" Test street" , config.streetName)
228
+ assertEquals(" 12345" , config.postalCode)
229
+ assertEquals(" Test municipality" , config.municipality)
230
+ assertEquals(null , config.unitNumber)
231
+ }
209
232
}
Original file line number Diff line number Diff line change @@ -30,6 +30,8 @@ internal open class HoconDecoder(
30
30
return if (config.hasPath(fullPath)) {
31
31
currentPath = fullPath
32
32
newIndex
33
+ } else if (descriptor.isElementOptional(newIndex)) {
34
+ decodeElementIndex(descriptor)
33
35
} else {
34
36
CompositeDecoder .DECODE_DONE
35
37
}
Original file line number Diff line number Diff line change @@ -220,6 +220,26 @@ class HoconDecoderTest {
220
220
}
221
221
}
222
222
223
+ @Test
224
+ fun `default values` () {
225
+ val content = """
226
+ address {
227
+ streetName = "Main Street"
228
+ postalCode = "12345"
229
+ municipality = "Test City"
230
+ # unitNumber is intentionally omitted to test default value
231
+ }
232
+ """ .trimIndent()
233
+
234
+ val address = parseConfig(content)
235
+ .propertyOrNull(" address" )?.getAs<Address >()
236
+
237
+ assertNotNull(address)
238
+ assertEquals(" Main Street" , address.streetName)
239
+ assertEquals(" Test City" , address.municipality)
240
+ assertNull(address.unitNumber)
241
+ }
242
+
223
243
private fun parseConfig (content : String ): HoconApplicationConfig =
224
244
HoconApplicationConfig (ConfigFactory .parseString(content))
225
245
@@ -294,4 +314,12 @@ class HoconDecoderTest {
294
314
val enumValue : TestEnum ,
295
315
val enumList : List <TestEnum >
296
316
)
317
+
318
+ @Serializable
319
+ data class Address (
320
+ val streetName : String ,
321
+ val postalCode : String ,
322
+ val unitNumber : String? = null ,
323
+ val municipality : String ,
324
+ )
297
325
}
You can’t perform that action at this time.
0 commit comments