Skip to content

Commit df08c1e

Browse files
committed
Fix #306 (quoting needed for field names with linefeeds)
1 parent 416461a commit df08c1e

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

release-notes/CREDITS-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,6 @@ Falk Hanisch (mrpiggi@github)
194194
for the same POJO
195195
(2.13.1)
196196

197+
Esteban Ginez (eginez@github)
198+
#306: (yaml) Error when generating/serializing keys with multilines and colon
199+
(2.13.2)

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ No changes since 2.13
2222

2323
#303: (yaml) Update to SnakeYAML 1.30
2424
(suggested by PJ Fanning)
25+
#306: (yaml) Error when generating/serializing keys with multilines and colon
26+
(reported by Esteban G)
2527

2628
2.13.1 (19-Dec-2021)
2729

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/util/StringQuotingChecker.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,27 @@ protected boolean valueHasQuotableChar(String inputStr)
163163
}
164164
return false;
165165
}
166-
166+
167+
/**
168+
* Looks like we may get names with "funny characters" so.
169+
*
170+
* @since 2.13.2
171+
*/
172+
protected boolean nameHasQuotableChar(String inputStr)
173+
{
174+
// 31-Jan-2022, tatu: As per [dataformats-text#306] linefeed is
175+
// problematic. I'm sure there are likely other cases, but let's
176+
// start with the obvious ones, control characters
177+
final int end = inputStr.length();
178+
for (int i = 0; i < end; ++i) {
179+
int ch = inputStr.charAt(i);
180+
if (ch < 0x0020) {
181+
return true;
182+
}
183+
}
184+
return false;
185+
}
186+
167187
/**
168188
* Default {@link StringQuotingChecker} implementation used unless
169189
* custom implementation registered.
@@ -189,7 +209,10 @@ public Default() { }
189209
@Override
190210
public boolean needToQuoteName(String name)
191211
{
192-
return isReservedKeyword(name) || looksLikeYAMLNumber(name);
212+
return isReservedKeyword(name) || looksLikeYAMLNumber(name)
213+
// 31-Jan-2022, tatu: as per [dataformats-text#306] may also
214+
// have other characters requiring quoting...
215+
|| nameHasQuotableChar(name);
193216
}
194217

195218
/**

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/failing/NameQuoting306Test.java renamed to yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/NameQuoting306Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.dataformat.yaml.failing;
1+
package com.fasterxml.jackson.dataformat.yaml.deser;
22

33
import java.util.Collections;
44
import java.util.Map;

0 commit comments

Comments
 (0)