Skip to content

FIX #180: YAML Generator now quotes strings containing special chars #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ private Feature(boolean defaultState) {
"on", "On", "ON", "off", "Off", "OFF",
"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(
":", "#", "[", "]", "{", "}", ","
));

/*
/**********************************************************
Expand Down Expand Up @@ -480,7 +488,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(null, null));
super.close();
Expand Down Expand Up @@ -981,6 +989,16 @@ private boolean _valueNeedsQuoting(String name) {
case 'Y': // Y/Yes/YES
return MUST_QUOTE_VALUES.contains(name);
}
return stringContainsItemFromList(name, SPECIAL_CHARS.toArray(new String[]{}));
}

private static boolean stringContainsItemFromList(String inputStr, String[] items) {
for(int i =0; i < items.length; i++) {
if (inputStr.contains( items[i] )) {
return true;
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,50 @@ public void testMinimizeQuotesWithNulls() throws Exception
"key: nuLL", yaml);
}

public void testMinimizeQuotesWithStringsContainingSpecialChars() throws Exception {
Map<String, Object> content = new HashMap<String, Object>();
content.put("key", "a:b");
String yaml = MINIM_MAPPER.writeValueAsString(content).trim();
assertEquals("---\n" +
"key: \"a:b\"", yaml);

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

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

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

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

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

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

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