8000 Kaml lacks support for YAML Complex Keys (mapping keys using sequences/mappings) · Issue #700 · charleskorn/kaml · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Kaml lacks support for YAML Complex Keys (mapping keys using sequences/mappings) #700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
HatoYuze opened this issue May 5, 2025 · 1 comment

Comments

@HatoYuze
Copy link
HatoYuze commented May 5, 2025

Describe the bug

According to the YAML 1.2 specification §2.2 (see Example 2.11. Mapping between Sequences), YAML allows using sequences or mappings as valid mapping keys (hereafter referred to as "Complex Keys"). These keys are marked with a ? indicator and span until the next : delimiter.

However, Kaml's current architecture doesn't support this specification. Specifically:

public data class YamlMap(
    val entries: Map<YamlScalar, YamlNode>, 
    override val path: YamlPath
)

The YamlMap.entries currently restricts keys to YamlScalar type, which prevents proper handling of complex keys using nested structures.

Observed Behavior:

  1. Attempt to deserialize a YAML mapping with complex keys, e.g.:
? [key1, key2]
: value
  1. Kaml throws:
Property name must not be a list, map, null or tagged value. 
(To use 'null' as a property name, enclose it in quotes.)

Reproduction repo

No response

Steps to reproduce

fun example2_11() {
    val input = """
? [ New York Yankees,
    Atlanta Braves ]
: [ 2001-07-02, 2001-08-12,
    2001-08-14 ]
    """.trimIndent()
    val result = Yaml.default.decodeFromString(
        MapSerializer(
            (ListSerializer(String.serializer())),
            (ListSerializer(String.serializer()))
        ), input
    )
    println(result)
}

Expected behaviour

The parser should output a structure equivalent to:

mapOf(
 listOf("New York Yankees", "Atlanta Braves") to
 listOf("2001-07-02", "2001-08-12", "2001-08-14")
)

In my code, it should print

{[New York Yankees, Atlanta Braves]=[2001-07-02, 2001-08-12, 2001-08-14]}

Actual behaviour

Exception in thread "main" MalformedYamlException at <root> on line 1, column 3: Property name must not be a list, map, null or tagged value. (To use 'null' as a property name, enclose it in quotes.)
	at com.charleskorn.kaml.YamlNodeReader.nonScalarMapKeyException(YamlNodeReader.kt:155)
	at com.charleskorn.kaml.YamlNodeReader.readMapKey(YamlNodeReader.kt:151)
	at com.charleskorn.kaml.YamlNodeReader.readMapping(YamlNodeReader.kt:116)
	at com.charleskorn.kaml.YamlNodeReader.readFromEvent(YamlNodeReader.kt:66)
	at com.charleskorn.kaml.YamlNodeReader.readNodeAndAnchor(YamlNodeReader.kt:43)
	at com.charleskorn.kaml.YamlNodeReader.readNode(YamlNodeReader.kt:39)
	at com.charleskorn.kaml.YamlNodeReader.read(YamlNodeReader.kt:37)
	at com.charleskorn.kaml.Yaml.parseToYamlNode$kaml(Yaml.kt:85)
	at com.charleskorn.kaml.Yaml.decodeFromSource(Yaml.kt:69)
	at com.charleskorn.kaml.Yaml.decodeFromString(Yaml.kt:59)

Version information

0.77.0

Any other information

No response

@charleskorn
Copy link
Owner

Makes sense to me, PRs are welcome 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0