From df08c1e77b1c5d3a980decf1392115f2da42484e Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Mon, 31 Jan 2022 18:22:24 -0800 Subject: [PATCH] Fix #306 (quoting needed for field names with linefeeds) --- release-notes/CREDITS-2.x | 3 +++ release-notes/VERSION-2.x | 2 ++ .../yaml/util/StringQuotingChecker.java | 27 +++++++++++++++++-- .../NameQuoting306Test.java | 2 +- 4 files changed, 31 insertions(+), 3 deletions(-) rename yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/{failing => deser}/NameQuoting306Test.java (92%) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index e0fadc8f..ea4050fc 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -194,3 +194,6 @@ Falk Hanisch (mrpiggi@github) for the same POJO (2.13.1) +Esteban Ginez (eginez@github) +#306: (yaml) Error when generating/serializing keys with multilines and colon + (2.13.2) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 799a6a0d..493930e2 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -22,6 +22,8 @@ No changes since 2.13 #303: (yaml) Update to SnakeYAML 1.30 (suggested by PJ Fanning) +#306: (yaml) Error when generating/serializing keys with multilines and colon + (reported by Esteban G) 2.13.1 (19-Dec-2021) diff --git a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/util/StringQuotingChecker.java b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/util/StringQuotingChecker.java index ddcabbeb..1c910f3b 100644 --- a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/util/StringQuotingChecker.java +++ b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/util/StringQuotingChecker.java @@ -163,7 +163,27 @@ protected boolean valueHasQuotableChar(String inputStr) } return false; } - + + /** + * Looks like we may get names with "funny characters" so. + * + * @since 2.13.2 + */ + protected boolean nameHasQuotableChar(String inputStr) + { + // 31-Jan-2022, tatu: As per [dataformats-text#306] linefeed is + // problematic. I'm sure there are likely other cases, but let's + // start with the obvious ones, control characters + final int end = inputStr.length(); + for (int i = 0; i < end; ++i) { + int ch = inputStr.charAt(i); + if (ch < 0x0020) { + return true; + } + } + return false; + } + /** * Default {@link StringQuotingChecker} implementation used unless * custom implementation registered. @@ -189,7 +209,10 @@ public Default() { } @Override public boolean needToQuoteName(String name) { - return isReservedKeyword(name) || looksLikeYAMLNumber(name); + return isReservedKeyword(name) || looksLikeYAMLNumber(name) + // 31-Jan-2022, tatu: as per [dataformats-text#306] may also + // have other characters requiring quoting... + || nameHasQuotableChar(name); } /** diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/failing/NameQuoting306Test.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/NameQuoting306Test.java similarity index 92% rename from yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/failing/NameQuoting306Test.java rename to yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/NameQuoting306Test.java index 33a89522..e26470b6 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/failing/NameQuoting306Test.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/NameQuoting306Test.java @@ -1,4 +1,4 @@ -package com.fasterxml.jackson.dataformat.yaml.failing; +package com.fasterxml.jackson.dataformat.yaml.deser; import java.util.Collections; import java.util.Map;