Skip to content

Commit d692916

Browse files
committed
Merge branch '2.11'
2 parents 3cd78ea + a4501bc commit d692916

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

release-notes/CREDITS-2.x

+4
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,7 @@ Tyler Carpenter-Rivers (tyler2cr@github)
9898
#7: Add `CsvParser.Feature.EMPTY_STRING_AS_NULL` to allow coercing empty Strings
9999
into `null` values
100100
(2.11.0)
101+
102+
* Reported, constributed fix for #180: (yaml) YAMLGenerator serializes string with special
103+
chars unquoted when using `MINIMIZE_QUOTES` mode
104+
(2.11.0)

release-notes/VERSION-2.x

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ Modules:
1010

1111
2.11.0 (not yet released)
1212

13-
#7: Add `CsvParser.Feature.EMPTY_STRING_AS_NULL` to allow coercing empty Strings
13+
#7: (csv) Add `CsvParser.Feature.EMPTY_STRING_AS_NULL` to allow coercing empty Strings
1414
into `null` values
1515
(contributed by Tyler C-R)
16-
#115: JsonProperty index is not honored by CsvSchema builder
16+
#115: (csv) JsonProperty index is not honored by CsvSchema builder
1717
-- actually fixed by [databind#2555]
18+
#180: (yaml) YAMLGenerator serializes string with special chars unquoted when
19+
using `MINIMIZE_QUOTES` mode
20+
(reported, fix contributed by Timo R)
1821

1922
2.10.4 (not yet released)
2023

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ public void close() throws IOException
486486
if (!isClosed()) {
487487
// 11-Dec-2019, tatu: Should perhaps check if content is to be auto-closed...
488488
// but need END_DOCUMENT regardless
489+
489490
_emitEndDocument();
490491
_emit(new StreamEndEvent());
491492
super.close();
@@ -957,7 +958,32 @@ private boolean _valueNeedsQuoting(String name) {
957958
case 'N': // Null/NULL/N/No/NO
958959
case 'T': // True/TRUE
959960
case 'Y': // Y/Yes/YES
960-
return MUST_QUOTE_VALUES.contains(name);
961+
if (MUST_QUOTE_VALUES.contains(name)) {
962+
return true;
963+
}
964+
break;
965+
}
966+
return _valueHasQuotableChar(name);
967+
}
968+
969+
/**
970+
* As per YAML <a href="https://yaml.org/spec/1.2/spec.html#id2788859">Plain Style</a>unquoted
971+
* strings are restricted to a reduced charset and must be quoted in case they contain
972+
* one of the following characters.
973+
*/
974+
private static boolean _valueHasQuotableChar(String inputStr) {
975+
for (int i = 0, end = inputStr.length(); i < end; ++i) {
976+
switch (inputStr.charAt(i)) {
977+
case ':':
978+
case '#':
979+
case '[':
980+
case ']':
981+
case '{':
982+
case '}':
983+
case ',':
984+
return true;
985+
default:
986+
}
961987
}
962988
return false;
963989
}
@@ -966,7 +992,6 @@ protected String _lf() {
966992
return _outputOptions.getBestLineBreak();
967993
}
968994

969-
// @since 2.10.2
970995
protected void _emitStartDocument() throws IOException
971996
{
972997
Map<String,String> noTags = Collections.emptyMap();
@@ -976,12 +1001,10 @@ protected void _emitStartDocument() throws IOException
9761001
noTags));
9771002
}
9781003

979-
// @since 2.10.2
9801004
protected void _emitEndDocument() throws IOException {
9811005
_emit(new DocumentEndEvent(false));
9821006
}
9831007

984-
// @since 2.10.2
9851008
protected final void _emit(Event e) throws IOException {
9861009
_emitter.emit(e);
9871010
}

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ser/GeneratorWithMinimizeTest.java

+43
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,49 @@ public void testMinimizeQuotesWithNulls() throws Exception
100100
"key: nuLL", yaml);
101101
}
102102

103+
public void testMinimizeQuotesWithStringsContainingSpecialChars() throws Exception {
104+
Map<String, String> content;
105+
106+
content = Collections.singletonMap("key", "a:b");
107+
String yaml = MINIM_MAPPER.writeValueAsString(content).trim();
108+
assertEquals("---\n" +
109+
"key: \"a:b\"", yaml);
110+
111+
content = Collections.singletonMap("key", "a#b");
112+
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
113+
assertEquals("---\n" +
114+
"key: \"a#b\"", yaml);
115+
116+
content = Collections.singletonMap("key", "a[b");
117+
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
118+
assertEquals("---\n" +
119+
"key: \"a[b\"", yaml);
120+
121+
content = Collections.singletonMap("key", "a]b");
122+
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
123+
assertEquals("---\n" +
124+
"key: \"a]b\"", yaml);
125+
126+
content = Collections.singletonMap("key", "a{b");
127+
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
128+
assertEquals("---\n" +
129+
"key: \"a{b\"", yaml);
130+
131+
content = Collections.singletonMap("key", "a}b");
132+
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
133+
assertEquals("---\n" +
134+
"key: \"a}b\"", yaml);
135+
136+
yaml = MINIM_MAPPER.writeValueAsString(Collections.singletonMap("key", "a,b")).trim();
137+
assertEquals("---\n" +
138+
"key: \"a,b\"", yaml);
139+
140+
// plus also some edge cases (wrt "false" etc checking
141+
yaml = MINIM_MAPPER.writeValueAsString(Collections.singletonMap("key", "f:off")).trim();
142+
assertEquals("---\n" +
143+
"key: \"f:off\"", yaml);
144+
}
145+
103146
public void testLiteralStringsMultiLine() throws Exception
104147
{
105148
Map<String, Object> content = new HashMap<String, Object>();

0 commit comments

Comments
 (0)