Him188 / yamlkt

Multiplatform YAML parser & serializer for kotlinx.serialization written in pure Kotlin
Apache License 2.0
132 stars 14 forks source link

KeyEmpty value causes yamlKT parser to nest susequent key/value pairs into Empty field #73

Open philip0402 opened 4 months ago

philip0402 commented 4 months ago

Example of Bug:

  val validYaml = "id: lgcbkyz4c6b5gnfsupczfkn\n" + 
                             "title: Truth\n" + 
                             "desc: \n" + 
                             "updated: 1704581169935\n" + 
                             "created: 1704049864411\n"
   val map: YamlMap = Yaml.decodeYamlMapFromString(validYaml)
   println("Parsed YAML map: $map")

Expected output:

ParsedYAMLmap:{id:lgcbkyz4c6b5gnfsupczfkn,title:Truth,desc: ,updated:1704581169935,created:1704049864411}

Actual output:

ParsedYAMLmap:{id:lgcbkyz4c6b5gnfsupczfkn,title:Truth,desc:{updated:1704581169935,created:1704049864411}}

Test which reproduces bug:

@Test
    fun testBlankDescription() {
        val validYaml = "id: lgcbkyz4c6b5gnfsupczfkn\n" + "title: Truth\n" + "desc: \n" + "updated: 1704581169935\n" + "created: 1704049864411\n"
        val map: YamlMap = Yaml.decodeYamlMapFromString(validYaml)
        println("Parsed YAML map: $map")
        assertTrue { map.containsKey(YamlPrimitive("desc")) }
        assertTrue { map.containsKey(YamlPrimitive("updated")) }
        assertTrue { map.containsKey(YamlPrimitive("created")) }
    }

Test Results:

Pass:

Assertion Errors:

Debugging Logic:

Function decodeYamlMapFromString is called. Stepping into the function implementation.

Screenshot 2024-06-28 at 2 37 03 PM

Function decodeYamlMapFromString calls decodeFromString. Stepping into the function implementation.

Note: The function was edited for debugging purposes. val yamlMap was created to keep track of the return value of the deserialize function call.

 deserializer.deserialize(YamlDecoder(configuration, TokenStream(string), serializersModule))

Screenshot 2024-06-28 at 2 38 29 PM

Function deserialize is called. Stepping into the function implementation. The program steps into the conditional code block and calls the deserialize function from the kotlinx library.

Screenshot 2024-06-28 at 3 02 25 PM

Screenshot 2024-06-28 at 3 03 43 PM

Results:

The issue seems to come from the parsing done by the deserialize function call made from the kotlinx library.

Moving forward:

The yamlkt parser should explicitly address the issue of empty values in the key/value mappings.