Skip to content

Commit

Permalink
Merge branch '2.11'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 16, 2020
2 parents 3cd78ea + a4501bc commit d692916
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 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 @@ -486,6 +486,7 @@ public void close() throws IOException
if (!isClosed()) {
// 11-Dec-2019, tatu: Should perhaps check if content is to be auto-closed...
// but need END_DOCUMENT regardless

_emitEndDocument();
_emit(new StreamEndEvent());
super.close();
Expand Down Expand Up @@ -957,7 +958,32 @@ private boolean _valueNeedsQuoting(String name) {
case 'N': // Null/NULL/N/No/NO
case 'T': // True/TRUE
case 'Y': // Y/Yes/YES
return MUST_QUOTE_VALUES.contains(name);
if (MUST_QUOTE_VALUES.contains(name)) {
return true;
}
break;
}
return _valueHasQuotableChar(name);
}

/**
* 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 All @@ -966,7 +992,6 @@ protected String _lf() {
return _outputOptions.getBestLineBreak();
}

// @since 2.10.2
protected void _emitStartDocument() throws IOException
{
Map<String,String> noTags = Collections.emptyMap();
Expand All @@ -976,12 +1001,10 @@ protected void _emitStartDocument() throws IOException
noTags));
}

// @since 2.10.2
protected void _emitEndDocument() throws IOException {
_emit(new DocumentEndEvent(false));
}

// @since 2.10.2
protected final void _emit(Event e) throws IOException {
_emitter.emit(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,49 @@ public void testMinimizeQuotesWithNulls() throws Exception
"key: nuLL", yaml);
}

public void testMinimizeQuotesWithStringsContainingSpecialChars() throws Exception {
Map<String, String> content;

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

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

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

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

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

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

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

// plus also some edge cases (wrt "false" etc checking
yaml = MINIM_MAPPER.writeValueAsString(Collections.singletonMap("key", "f:off")).trim();
assertEquals("---\n" +
"key: \"f:off\"", yaml);
}

public void testLiteralStringsMultiLine() throws Exception
{
Map<String, Object> content = new HashMap<String, Object>();
Expand Down

0 comments on commit d692916

Please sign in to comment.