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 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
5 changes: 5 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,8 @@ Mathieu Lavigne (@mathieu-lavigne)
* Proposed #45 (and suggested implementation): (csv) Allow skipping ending line break
(`CsvGenerator.Feature.WRITE_LINEFEED_AFTER_LAST_ROW`)
(2.17.0)

Michael Edgar (@MikeEdgar)

* Contributed #465: (yaml) YAML: consider starting `#` and ending `:` as quotable characters
(2.17.0)
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Active Maintainers:
=== Releases ===
------------------------------------------------------------------------

#465: (yaml) YAML: consider starting `#` and ending `:` as quotable characters
(contributed by Michael E)

2.17.0-rc1 (26-Feb-2024)

#45: (csv) Allow skipping ending line break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Helper class that defines API used by
* {@link com.fasterxml.jackson.dataformat.yaml.YAMLGenerator}
* to check whether property names and String values need to be quoted or not.
* Also contains default logic implementation; may be sub-classes to provide
* Also contains default logic implementation; may be sub-classed to provide
* alternate implementation.
*
* @since 2.12
Expand Down 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,16 @@ 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;
}
// (but not recognized as comment unless starts line or preceded by whitespace)
if (precededOnlyByBlank(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;
}
// (but recognized as separator only if end-of-line or followed by whitespace)
if (followedOnlyByBlank(inputStr, i)) {
return true;
}
break;
default:
Expand All @@ -164,6 +160,27 @@ protected boolean valueHasQuotableChar(String inputStr)
return false;
}

// @since 2.17
protected boolean precededOnlyByBlank(String inputStr, int offset) {
if (offset == 0) {
return true;
}
return isBlank(inputStr.charAt(offset - 1));
}

// @since 2.17
protected boolean followedOnlyByBlank(String inputStr, int offset) {
if (offset == inputStr.length() - 1) {
return true;
}
return isBlank(inputStr.charAt(offset + 1));
}

// @since 2.17
protected boolean isBlank(char value) {
return (' ' == value || '\t' == value);
}

/**
* Looks like we may get names with "funny characters" so.
*
Expand Down Expand Up @@ -199,7 +216,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