Skip to content

Commit 3767598

Browse files
committed
Fix #91
1 parent 0424524 commit 3767598

File tree

7 files changed

+50
-8
lines changed

7 files changed

+50
-8
lines changed

properties/src/main/java/com/fasterxml/jackson/dataformat/javaprop/JavaPropsGenerator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.fasterxml.jackson.core.base.GeneratorBase;
1010
import com.fasterxml.jackson.core.io.IOContext;
1111
import com.fasterxml.jackson.core.json.JsonWriteContext;
12-
import com.fasterxml.jackson.dataformat.javaprop.io.JPropEscapes;
1312
import com.fasterxml.jackson.dataformat.javaprop.io.JPropWriteContext;
1413
import com.fasterxml.jackson.dataformat.javaprop.util.Markers;
1514

@@ -206,6 +205,8 @@ public JsonStreamContext getOutputContext() {
206205
/**********************************************************************
207206
*/
208207

208+
// varies between impls so:
209+
// @Override public void writeFieldName(String name) throws IOException
209210
@Override
210211
public void writeFieldName(String name) throws IOException
211212
{
@@ -230,12 +231,11 @@ public void writeFieldName(String name) throws IOException
230231
_basePath.append(sep);
231232
}
232233
}
233-
// Note that escaping needs to be applied now...
234-
235-
JPropEscapes.appendKey(_basePath, name);
236-
// NOTE: we do NOT yet write the key; wait until we have value; just append to path
234+
_appendFieldName(_basePath, name);
237235
}
238236

237+
protected abstract void _appendFieldName(StringBuilder path, String name);
238+
239239
/*
240240
/**********************************************************
241241
/* Public API: structural output

properties/src/main/java/com/fasterxml/jackson/dataformat/javaprop/JavaPropsMapper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ public void writeValue(Properties targetProps, Object value, JavaPropsSchema sch
210210
if (targetProps == null) {
211211
throw new IllegalArgumentException("Can not pass null Properties as target");
212212
}
213-
JavaPropsGenerator g = ((JavaPropsFactory) getFactory())
214-
.createGenerator(targetProps);
213+
JavaPropsGenerator g = getFactory().createGenerator(targetProps);
215214
if (schema != null) {
216215
g.setSchema(schema);
217216
}
@@ -229,7 +228,7 @@ public void writeValue(Properties targetProps, Object value, JavaPropsSchema sch
229228
public Properties writeValueAsProperties(Object value)
230229
throws IOException
231230
{
232-
Properties props = new Properties();
231+
final Properties props = new Properties();
233232
writeValue(props, value);
234233
return props;
235234
}

properties/src/main/java/com/fasterxml/jackson/dataformat/javaprop/impl/PropertiesBackedGenerator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public PropertiesBackedGenerator(IOContext ctxt, Properties props,
3232
{
3333
super(ctxt, stdFeatures, codec);
3434
_props = props;
35+
36+
// Since this is not physically encoding properties, should NOT try
37+
// to attempt writing headers. Easy way is to just fake we already did it
38+
_headerChecked = true;
3539
}
3640

3741
/*
@@ -66,6 +70,12 @@ public void flush() throws IOException { }
6670
@Override
6771
protected void _releaseBuffers() { }
6872

73+
@Override
74+
protected void _appendFieldName(StringBuilder path, String name) {
75+
// No escaping should be applied
76+
path.append(name);
77+
}
78+
6979
/*
7080
/**********************************************************
7181
/* Internal methods; escaping writes

properties/src/main/java/com/fasterxml/jackson/dataformat/javaprop/impl/WriterBackedGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ protected void _flushBuffer() throws IOException
129129
}
130130
}
131131

132+
@Override
133+
protected void _appendFieldName(StringBuilder path, String name) {
134+
// Note that escaping needs to be applied now...
135+
JPropEscapes.appendKey(_basePath, name);
136+
// NOTE: we do NOT yet write the key; wait until we have value; just append to path
137+
}
138+
132139
/*
133140
/**********************************************************
134141
/* Internal methods; escaping writes

properties/src/test/java/com/fasterxml/jackson/dataformat/javaprop/PropertiesSupportTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,24 @@ public void testWithCustomSchema() throws Exception
5151
assertEquals(1, m2.size());
5252
assertEquals("foo", m2.get("z"));
5353
}
54+
55+
static class TestObject91 {
56+
Map<String, String> values = new HashMap<>();
57+
public Map<String, String> getValues() {
58+
return values;
59+
}
60+
public void setValues(Map<String, String> values) {
61+
this.values = values;
62+
}
63+
}
64+
65+
// [dataformats-text#91]
66+
public void testEscapingWithReadPropertiesAs() throws Exception
67+
{
68+
TestObject91 expected = new TestObject91();
69+
expected.values.put("foo:bar", "1");
70+
Properties properties = MAPPER.writeValueAsProperties(expected);
71+
TestObject91 actual = MAPPER.readPropertiesAs(properties, TestObject91.class);
72+
assertEquals(expected.values, actual.values);
73+
}
5474
}

release-notes/CREDITS-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ Gowtam Lal (baconmania@github)
3737
* Reported #68: (yaml) When field names are reserved words, they should be
3838
written out with quotes
3939
(2.9.9)
40+
41+
Dimitris Mandalidis (dmandalidis@github)
42+
* Reported #91: (properties) `JavaPropsGenerator#writeFieldName()` should not escape property keys
43+
(2.9.9)

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Modules:
1616
(reported by Gowtam L)
1717
#90: (yaml) Exception when decoding Jackson-encoded `Base64` binary value in YAML
1818
(reported by Tanguy L)
19+
#91: (properties) `JavaPropsGenerator#writeFieldName()` should not escape property keys
20+
(reported by Dimitris M)
1921
#122: (csv) `readValues(null)` causes infinite loop
2022
(reported by andyeko@github)
2123
#123: (yaml) YAML Anchor, reference fails with simple example

0 commit comments

Comments
 (0)