Skip to content
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

Improve MINIMIZE_QUOTES handling to avoid quoting for some use of # and : #201

Merged
merged 1 commit into from
Jun 4, 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 @@ -175,7 +175,7 @@ private Feature(boolean defaultState) {
* aliases for booleans, and we better quote such values as keys; although Jackson
* itself has no problems dealing with them, some other tools do have.
*/
// 02-Apr-2019, tatu: Some names will look funny if escaped: let's leave out
// 02-Apr-2019, tatu: Some names will look funny if escaped: let's leave out
// single letter case (esp so 'y' won't get escaped)
private final static Set<String> MUST_QUOTE_NAMES = new HashSet<>(Arrays.asList(
// "y", "Y", "n", "N",
Expand Down Expand Up @@ -217,7 +217,7 @@ private Feature(boolean defaultState) {
protected DumperOptions _outputOptions;

protected final org.yaml.snakeyaml.DumperOptions.Version _docVersion;

// for field names, leave out quotes
private final static DumperOptions.ScalarStyle STYLE_UNQUOTED_NAME = DumperOptions.ScalarStyle.PLAIN;

Expand Down Expand Up @@ -964,7 +964,7 @@ private boolean _nameNeedsQuoting(String name) {
return true;
}
return false;
}
}

private boolean _valueNeedsQuoting(String name) {
switch (name.charAt(0)) { // caller ensures no empty String
Expand All @@ -990,19 +990,28 @@ private boolean _valueNeedsQuoting(String 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.
* one of the following characters or character combinations.
*/
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;
case '\t':
case ' ':
if (i < end - 1 && '#' == inputStr.charAt(i + 1)) {
return true;
}
break;
case ':':
if (i < end - 1 && (' ' == inputStr.charAt(i + 1) || '\t' == inputStr.charAt(i + 1))) {
return true;
}
break;
default:
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,50 @@ public void testMinimizeQuotesWithNulls() throws Exception
public void testMinimizeQuotesWithStringsContainingSpecialChars() throws Exception {
Map<String, String> content;

String yaml = null;

/* scenarios with plain scalars */

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

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

content = Collections.singletonMap("key", "a# b");
yaml = MINIM_MAPPER.writeValueAsString(content).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);


/* scenarios with single quoted scalars */

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

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

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


/* scenarios with double quoted scalars */

content = Collections.singletonMap("key", "a[b");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
Expand All @@ -128,10 +163,6 @@ public void testMinimizeQuotesWithStringsContainingSpecialChars() throws Excepti
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
Expand Down Expand Up @@ -182,7 +213,7 @@ public void testNonQuoteNumberStoredAsString() throws Exception
String yaml = MINIM_MAPPER.writeValueAsString(Collections.singletonMap("key", "20")).trim();
assertEquals("---\n" +
"key: 20", yaml);

yaml = MINIM_MAPPER.writeValueAsString(Collections.singletonMap("key", "2.0")).trim();
assertEquals("---\n" +
"key: 2.0", yaml);
Expand Down Expand Up @@ -213,7 +244,7 @@ public void testNumberKey() throws Exception
MINIM_MAPPER.writeValueAsString(stringKeyMap).trim());

// And then true Integer keys

final Map<Integer, String> intKeyMap = Collections.singletonMap(
Integer.valueOf(42), "answer");

Expand Down