Skip to content

Allow to use long keys in YAML #334

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

Merged
merged 1 commit into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ public enum Feature implements FormatFeature // since 2.9
* @since 2.9.6
*/
USE_PLATFORM_LINE_BREAKS(false),

/**
* Option passed to SnakeYAML to enable usage of key longer that 128 characters.
* If disabled, the max key length is set to 128 characters.
* <p>
* Default value is {@code false}.
*
* @since 2.14
*/
USE_LONG_KEYS(false),
;

protected final boolean _defaultState;
Expand Down Expand Up @@ -310,6 +320,10 @@ protected DumperOptions buildDumperOptions(int jsonFeatures, int yamlFeatures,
if (Feature.USE_PLATFORM_LINE_BREAKS.enabledIn(_formatFeatures)) {
opt.setLineBreak(DumperOptions.LineBreak.getPlatformLineBreak());
}

if (Feature.USE_LONG_KEYS.enabledIn(_formatFeatures)) {
opt.setMaxSimpleKeyLength(1024);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the setting that would need to be encapsulated in a separate class, with a method that gets opt passed in, sets the maximum length. Since that class is only loaded when method called (first "active use") it is possible
to avoid failure when YAMLGenerator class itself is initialized.
Class loading exception should trigger throwing of exception that suggests too old SnakeYAML dependency.

}
return opt;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ public void testArrayIndentation() throws Exception
assertEquals("- \"third\"", parts[3].trim());
}

//@since 2.14
public void testLongKeys() throws Exception
{
final String LONG_KEY = "key_longer_than_128_characters_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

ObjectMapper defaultMapper = YAMLMapper.builder().build();
final Object inputValue = Collections.singletonMap(LONG_KEY, "value");
assertEquals("---\n? " + LONG_KEY + "\n: \"value\"",
_trim(defaultMapper.writeValueAsString(inputValue)));

ObjectMapper longKeysMapper = YAMLMapper.builder().enable(YAMLGenerator.Feature.USE_LONG_KEYS).build();
assertEquals("---\n" + LONG_KEY + ": \"value\"",
_trim(longKeysMapper.writeValueAsString(inputValue)));
}

// @since 2.12
public void testYAMLSpecVersionDefault() throws Exception
{
Expand Down