Skip to content

Commit

Permalink
Add release notes, minor changes to #180
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 16, 2020
1 parent c41a621 commit ee1a1d0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ Tyler Carpenter-Rivers (tyler2cr@github)
#7: Add `CsvParser.Feature.EMPTY_STRING_AS_NULL` to allow coercing empty Strings
into `null` values
(2.11.0)

* Reported, constributed fix for #180: (yaml) YAMLGenerator serializes string with special
chars unquoted when using `MINIMIZE_QUOTES` mode
(2.11.0)
7 changes: 5 additions & 2 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ Modules:

2.11.0 (not yet released)

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

2.10.4 (not yet released)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,6 @@ private Feature(boolean defaultState) {
"null", "Null", "NULL"
));

/**
* As per YAML <a href="https://yaml.org/spec/1.2/spec.html#id2788859">Plain Style</a>unquoted strings are
* restriced to a reduced charset and must be quoted in case they contain one of the following characters.
*/
private final static Set<String> SPECIAL_CHARS = new HashSet<>(Arrays.asList(
":", "#", "[", "]", "{", "}", ","
));

/*
/**********************************************************
/* Configuration
Expand Down Expand Up @@ -989,16 +981,28 @@ private boolean _valueNeedsQuoting(String name) {
case 'Y': // Y/Yes/YES
return MUST_QUOTE_VALUES.contains(name);
}
return stringContainsItemFromList(name, SPECIAL_CHARS.toArray(new String[]{}));
return _valueHasQuotableChar(name);
}

private static boolean stringContainsItemFromList(String inputStr, String[] items) {
for(int i =0; i < items.length; i++) {
if (inputStr.contains( items[i] )) {
/**
* As per YAML <a href="https://yaml.org/spec/1.2/spec.html#id2788859">Plain Style</a>unquoted
* strings are restricted to a reduced charset and must be quoted in case they contain
* one of the following characters.
*/
private static boolean _valueHasQuotableChar(String inputStr) {
for (int i = 0, end = inputStr.length(); i < end; ++i) {
switch (inputStr.charAt(i)) {
case ':':
case '#':
case '[':
case ']':
case '{':
case '}':
case ',':
return true;
default:
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,44 +92,39 @@ public void testMinimizeQuotesWithNulls() throws Exception
}

public void testMinimizeQuotesWithStringsContainingSpecialChars() throws Exception {
Map<String, Object> content = new HashMap<String, Object>();
content.put("key", "a:b");
Map<String, String> content;

content = Collections.singletonMap("key", "a:b");
String yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: \"a:b\"", yaml);

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

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

content.clear();
content.put("key", "a]b");
content = Collections.singletonMap("key", "a]b");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: \"a]b\"", yaml);

content.clear();
content.put("key", "a{b");
content = Collections.singletonMap("key", "a{b");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: \"a{b\"", yaml);

content.clear();
content.put("key", "a}b");
content = Collections.singletonMap("key", "a}b");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: \"a}b\"", yaml);

content.clear();
content.put("key", "a,b");
content = Collections.singletonMap("key", "a,b");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: \"a,b\"", yaml);
Expand Down

0 comments on commit ee1a1d0

Please sign in to comment.