Skip to content
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

Add YAMLGenerator.Feature.ALLOW_LONG_KEYS to allow writing keys longer than 128 characters (default) #244

Closed
msmsimondean opened this issue Feb 23, 2021 · 2 comments
Labels
yaml Issue related to YAML format backend
Milestone

Comments

@msmsimondean
Copy link

At the moment, a field name with a length of 128 characters or longer results in Jackson's YAML generator producing YAML like this:

---
? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
: anything

Using 1 less character in the field name results in the usual YAML syntax.

---
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: anything

Apparently the two YAML styles are called simple keys and complex keys. SnakeYAML provides an option in its DumperOptions to change the max field name length at which SnakeYAML will switch from a) using the simple key style to b) using the complex key style. Here it is:

https://github.com/asomov/snakeyaml/blob/88c727ce266533c1db50ac24fbd3a3f33f7fe5a0/src/main/java/org/yaml/snakeyaml/DumperOptions.java#L449-L463

    public int getMaxSimpleKeyLength() {
        return maxSimpleKeyLength;
    }

    /**
     * Define max key length to use simple key (without '?')
     * More info https://yaml.org/spec/1.1/#id934537
     * @param maxSimpleKeyLength - the limit after which the key gets explicit key indicator '?'
     */
    public void setMaxSimpleKeyLength(int maxSimpleKeyLength) {
        if(maxSimpleKeyLength > 1024) {
            throw new YAMLException("The simple key must not span more than 1024 stream characters. See https://yaml.org/spec/1.1/#id934537");
        }
        this.maxSimpleKeyLength = maxSimpleKeyLength;
    }

Please could Jackson's YAML lib provide a YAML feature for using a higher value for maxSimpleKeyLength. E.g. a LONG_SIMPLE_KEYS feature that sets maxSimpleKeyLength to something like 1024.

See https://bitbucket.org/asomov/snakeyaml/issues/431/increase-simple-key-length-to-1024-stream for details of what the DumperOptions setting was added to SnakeYAML. That issue mentions that a code change to Jackson's YAML lib would be needed to take advantage of the new setting when using Jackson. Thanks

@cowtowncoder cowtowncoder added the yaml Issue related to YAML format backend label May 27, 2021
@cowtowncoder
Copy link
Member

Sounds like a good idea to offer something that allows configuring snakeyaml options without exposing snakeyaml types. Would definitely accept & help with PR, not sure I have time to work on this at this point myself.

@cowtowncoder cowtowncoder changed the title Provide a way to influence SnakeYAML's maxSimpleKeyLength setting Add YAMLGenerator.Feature.ALLOW_LONG_KEYS to allow writing keys longer than 128 characters (default) Sep 14, 2022
@cowtowncoder cowtowncoder added this to the 2.14.0 milestone Sep 14, 2022
@cowtowncoder
Copy link
Member

Implemented via #334, will be in 2.14.0(-rc1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
yaml Issue related to YAML format backend
Projects
None yet
Development

No branches or pull requests

2 participants