Skip to content

Commit b7ef808

Browse files
cowtowncoderpjankovsky
authored andcommitted
1 parent 530e16d commit b7ef808

File tree

6 files changed

+69
-7
lines changed

6 files changed

+69
-7
lines changed

release-notes/CREDITS-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ Yegor Borovikov (yborovikov@gitub)
3232
* Reported #81 (yaml): Jackson 2.9.5, 2.9.6 incompatible with snakeyaml 1.20, 1.21
3333
(2.9.7)
3434

35+
Gowtam Lal (baconmania@github)
36+
37+
* Reported #68: (yaml) When field names are reserved words, they should be
38+
written out with quotes
39+
(2.9.9)

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Modules:
1212

1313
#63: (yaml) `null` Object Id serialized as anchor for YAML
1414
(reported by jflefebvre06@github)
15+
#68: (yaml) When field names are reserved words, they should be written out with quotes
16+
(reported by Gowtam L)
1517
#90: (yaml) Exception when decoding Jackson-encoded `Base64` binary value in YAML
1618
(reported by Tanguy L)
1719
#122: (csv) `readValues(null)` causes infinite loop

yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import java.math.BigInteger;
66
import java.util.Arrays;
77
import java.util.Collections;
8+
import java.util.HashSet;
89
import java.util.Map;
10+
import java.util.Set;
911
import java.util.regex.Pattern;
1012

1113
import org.yaml.snakeyaml.DumperOptions;
@@ -169,6 +171,16 @@ private Feature(boolean defaultState) {
169171
protected final static Pattern PLAIN_NUMBER_P = Pattern.compile("[0-9]*(\\.[0-9]*)?");
170172
protected final static String TAG_BINARY = Tag.BINARY.toString();
171173

174+
/* As per <a href="https://yaml.org/type/bool.html">YAML Spec</a> there are a few
175+
* aliases for booleans, and we better quote such values as keys; although Jackson
176+
* itself has no problems dealing with them, some other tools do have.
177+
*/
178+
private final static Set<String> RESERVED_NAMES = new HashSet<>(Arrays.asList(
179+
"y", "Y", "yes", "Yes", "YES", "n", "N", "no", "No", "NO",
180+
"true", "True", "TRUE", "false", "False", "FALSE",
181+
"on", "On", "ON", "off", "Off", "OFF"
182+
));
183+
172184
/*
173185
/**********************************************************
174186
/* Configuration
@@ -188,9 +200,13 @@ private Feature(boolean defaultState) {
188200

189201
protected DumperOptions _outputOptions;
190202

191-
// for field names, leave out quotes
192-
private final static Character STYLE_NAME = null;
193-
203+
/**
204+
* Names are written unquoted, by default.
205+
*
206+
* @since 2.10
207+
*/
208+
private Character _styleName = null;
209+
194210
// numbers, booleans, should use implicit
195211
private final static Character STYLE_SCALAR = null;
196212
// Strings quoted for fun
@@ -429,7 +445,13 @@ public final void writeStringField(String fieldName, String value)
429445
private final void _writeFieldName(String name)
430446
throws IOException
431447
{
432-
_writeScalar(name, "string", STYLE_NAME);
448+
Character style = _styleName;
449+
if (style == null) {
450+
if (RESERVED_NAMES.contains(name)) {
451+
style = STYLE_QUOTED;
452+
}
453+
}
454+
_writeScalar(name, "string", style);
433455
}
434456

435457
/*

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/ModuleTestBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ protected void verifyException(Throwable e, String... matches)
168168
protected static String trimDocMarker(String doc)
169169
{
170170
if (doc.startsWith("---")) {
171-
doc = doc.substring(3).trim();
171+
doc = doc.substring(3);
172172
}
173-
return doc;
173+
return doc.trim();
174174
}
175175
}

yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/misc/ObjectIdTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;
88
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
99
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
10-
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
1110

1211
public class ObjectIdTest extends ModuleTestBase
1312
{
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fasterxml.jackson.dataformat.yaml.misc;
2+
3+
import java.util.*;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;
7+
8+
// [dataformats-text#68]: should quote reserved names
9+
public class ReservedNamesTest extends ModuleTestBase
10+
{
11+
private final ObjectMapper MAPPER = newObjectMapper();
12+
13+
public void testQuotingOfBooleanKeys() throws Exception
14+
{
15+
for (String value : new String[] {
16+
"true", "True",
17+
"false", "False",
18+
"yes", "y", "Y",
19+
"no", "n", "N",
20+
"on",
21+
"off",
22+
}) {
23+
_testQuotingOfBooleanKeys(value);
24+
}
25+
}
26+
27+
private void _testQuotingOfBooleanKeys(String key) throws Exception
28+
{
29+
final Map<String, Integer> input = Collections.singletonMap(key, 123);
30+
final String doc = trimDocMarker(MAPPER.writeValueAsString(input).trim());
31+
32+
assertEquals("\""+key+"\": 123", doc);
33+
}
34+
}

0 commit comments

Comments
 (0)