Skip to content

Commit ee1a1d0

Browse files
committed
Add release notes, minor changes to #180
1 parent c41a621 commit ee1a1d0

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
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

+17-13
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,6 @@ private Feature(boolean defaultState) {
197197
"null", "Null", "NULL"
198198
));
199199

200-
/**
201-
* As per YAML <a href="https://yaml.org/spec/1.2/spec.html#id2788859">Plain Style</a>unquoted strings are
202-
* restriced to a reduced charset and must be quoted in case they contain one of the following characters.
203-
*/
204-
private final static Set<String> SPECIAL_CHARS = new HashSet<>(Arrays.asList(
205-
":", "#", "[", "]", "{", "}", ","
206-
));
207-
208200
/*
209201
/**********************************************************
210202
/* Configuration
@@ -989,16 +981,28 @@ private boolean _valueNeedsQuoting(String name) {
989981
case 'Y': // Y/Yes/YES
990982
return MUST_QUOTE_VALUES.contains(name);
991983
}
992-
return stringContainsItemFromList(name, SPECIAL_CHARS.toArray(new String[]{}));
984+
return _valueHasQuotableChar(name);
993985
}
994986

995-
private static boolean stringContainsItemFromList(String inputStr, String[] items) {
996-
for(int i =0; i < items.length; i++) {
997-
if (inputStr.contains( items[i] )) {
987+
/**
988+
* As per YAML <a href="https://yaml.org/spec/1.2/spec.html#id2788859">Plain Style</a>unquoted
989+
* strings are restricted to a reduced charset and must be quoted in case they contain
990+
* one of the following characters.
991+
*/
992+
private static boolean _valueHasQuotableChar(String inputStr) {
993+
for (int i = 0, end = inputStr.length(); i < end; ++i) {
994+
switch (inputStr.charAt(i)) {
995+
case ':':
996+
case '#':
997+
case '[':
998+
case ']':
999+
case '{':
1000+
case '}':
1001+
case ',':
9981002
return true;
1003+
default:
9991004
}
10001005
}
1001-
10021006
return false;
10031007
}
10041008

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

+9-14
Original file line numberDiff line numberDiff line change
@@ -92,44 +92,39 @@ public void testMinimizeQuotesWithNulls() throws Exception
9292
}
9393

9494
public void testMinimizeQuotesWithStringsContainingSpecialChars() throws Exception {
95-
Map<String, Object> content = new HashMap<String, Object>();
96-
content.put("key", "a:b");
95+
Map<String, String> content;
96+
97+
content = Collections.singletonMap("key", "a:b");
9798
String yaml = MINIM_MAPPER.writeValueAsString(content).trim();
9899
assertEquals("---\n" +
99100
"key: \"a:b\"", yaml);
100101

101-
content.clear();
102-
content.put("key", "a#b");
102+
content = Collections.singletonMap("key", "a#b");
103103
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
104104
assertEquals("---\n" +
105105
"key: \"a#b\"", yaml);
106106

107-
content.clear();
108-
content.put("key", "a[b");
107+
content = Collections.singletonMap("key", "a[b");
109108
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
110109
assertEquals("---\n" +
111110
"key: \"a[b\"", yaml);
112111

113-
content.clear();
114-
content.put("key", "a]b");
112+
content = Collections.singletonMap("key", "a]b");
115113
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
116114
assertEquals("---\n" +
117115
"key: \"a]b\"", yaml);
118116

119-
content.clear();
120-
content.put("key", "a{b");
117+
content = Collections.singletonMap("key", "a{b");
121118
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
122119
assertEquals("---\n" +
123120
"key: \"a{b\"", yaml);
124121

125-
content.clear();
126-
content.put("key", "a}b");
122+
content = Collections.singletonMap("key", "a}b");
127123
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
128124
assertEquals("---\n" +
129125
"key: \"a}b\"", yaml);
130126

131-
content.clear();
132-
content.put("key", "a,b");
127+
content = Collections.singletonMap("key", "a,b");
133128
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
134129
assertEquals("---\n" +
135130
"key: \"a,b\"", yaml);

0 commit comments

Comments
 (0)