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

YAML: consider starting # and ending : as quotable characters #465

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -58,10 +58,10 @@ public abstract class StringQuotingChecker
* Helper method that sub-classes may use to see if given String value is
* one of:
*<ul>
* <li>YAML 1.1 keyword representing
* <li>YAML 1.1 keyword representing
* <a href="https://yaml.org/type/bool.html">boolean</a>
* </li>
* <li>YAML 1.1 keyword representing
* <li>YAML 1.1 keyword representing
* <a href="https://yaml.org/type/null.html">null</a> value
* </li>
* <li>empty String (length 0)
Expand Down Expand Up @@ -142,20 +142,14 @@ protected boolean valueHasQuotableChar(String inputStr)
return true;
case '#':
// [dataformats-text#201]: limit quoting with MINIMIZE_QUOTES
if (i > 0) {
char d = inputStr.charAt(i-1);
if (' ' == d || '\t' == d) {
return true;
}
if (precededByBlank(inputStr, i)) {
return true;
}
break;
case ':':
// [dataformats-text#201]: limit quoting with MINIMIZE_QUOTES
if (i < (end-1)) {
char d = inputStr.charAt(i + 1);
if (' ' == d || '\t' == d) {
return true;
}
if (followedByBlank(inputStr, i)) {
return true;
}
break;
default:
Expand All @@ -164,6 +158,24 @@ protected boolean valueHasQuotableChar(String inputStr)
return false;
}

private boolean precededByBlank(String inputStr, int offset) {
if (offset == 0) {
return true;
}
return isBlank(inputStr.charAt(offset - 1));
}

private boolean followedByBlank(String inputStr, int offset) {
if (offset == inputStr.length() - 1) {
return true;
}
return isBlank(inputStr.charAt(offset + 1));
}

private boolean isBlank(char value) {
return (' ' == value || '\t' == value);
}

/**
* Looks like we may get names with "funny characters" so.
*
Expand Down Expand Up @@ -199,7 +211,7 @@ public static class Default
public Default() { }

public static Default instance() { return INSTANCE; }

/**
* Default implementation will call
* {@link #isReservedKeyword(String)} and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,22 @@ public void testMinimizeQuotesWithStringsContainingSpecialChars() throws Excepti
"key: f:off", yaml);


/* scenarios with single quoted scalars */
/* scenarios with double quoted scalars */

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

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

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


/* scenarios with double quoted scalars */
"key: \"#a\"", yaml);

content = Collections.singletonMap("key", "a[b");
yaml = MINIM_MAPPER.writeValueAsString(content).trim();
Expand Down