Skip to content

Commit

Permalink
Fixed #130
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 24, 2020
1 parent f0e05b8 commit 1e05426
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Modules:
2.12.0 (not yet released)

#71: (yaml) Hex number as an entry of an Object causing problem(s) with binding to POJO
#130: (yaml) Empty String deserialized as `null` instead of empty string
(reported by iulianrosca@github)
#175: (yaml) Add `YAMLGenerator.Feature.INDENT_ARRAYS_WITH_INDICATOR` to indent by 2 spaces
(requested by Jesper N; fix contributed by Damian S)
#199: (csv) Empty Lists can only be String-typed in CSV
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,30 @@ public Builder(YAMLMapper m) {
/******************************************************************
*/

// No Parser-features yet

public Builder enable(YAMLParser.Feature... features) {
for (YAMLParser.Feature f : features) {
_mapper.enable(f);
}
return this;
}

public Builder disable(YAMLParser.Feature... features) {
for (YAMLParser.Feature f : features) {
_mapper.disable(f);
}
return this;
}

public Builder configure(YAMLParser.Feature f, boolean state)
{
if (state) {
_mapper.enable(f);
} else {
_mapper.disable(f);
}
return this;
}

public Builder enable(YAMLGenerator.Feature... features) {
for (YAMLGenerator.Feature f : features) {
_mapper.enable(f);
Expand Down Expand Up @@ -71,7 +93,6 @@ public YAMLMapper(YAMLMapper base) {
super(base);
}

@SuppressWarnings("unchecked")
public static YAMLMapper.Builder builder() {
return new YAMLMapper.Builder(new YAMLMapper());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public class YAMLParser extends ParserBase
*/
public enum Feature implements FormatFeature // in 2.9
{
/**
* Feature that determines whether an empty {@link String} will be parsed
* as {@code null}. Logic is part of YAML 1.1
* <a href="https://yaml.org/type/null.html">Null Language-Independent Type</a>.
*<p>
* Feature is enabled by default in Jackson 2.12 for backwards-compatibility
* reasons.
*/
EMPTY_STRING_AS_NULL(true)
;

final boolean _defaultState;
Expand Down Expand Up @@ -84,6 +93,9 @@ private Feature(boolean defaultState) {

protected int _formatFeatures;

// @since 2.12
protected boolean _cfgEmptyStringsToNull;

/*
/**********************************************************************
/* Input sources
Expand Down Expand Up @@ -164,9 +176,9 @@ public YAMLParser(IOContext ctxt, BufferRecycler br,
_formatFeatures = formatFeatures;
_reader = reader;
_yamlParser = new ParserImpl(new StreamReader(reader));
_cfgEmptyStringsToNull = Feature.EMPTY_STRING_AS_NULL.enabledIn(formatFeatures);
}


@Override
public ObjectCodec getCodec() {
return _objectCodec;
Expand Down Expand Up @@ -260,11 +272,11 @@ protected void _closeInput() throws IOException {
_reader.close();
}
}

/*
/**********************************************************
/**********************************************************
/* FormatFeature support
/**********************************************************
/**********************************************************
*/

@Override
Expand All @@ -275,6 +287,7 @@ public int getFormatFeatures() {
@Override
public JsonParser overrideFormatFeatures(int values, int mask) {
_formatFeatures = (_formatFeatures & ~mask) | (values & mask);
_cfgEmptyStringsToNull = Feature.EMPTY_STRING_AS_NULL.enabledIn(_formatFeatures);
return this;
}

Expand All @@ -291,6 +304,7 @@ public JsonParser overrideFormatFeatures(int values, int mask) {
public JsonParser enable(YAMLParser.Feature f)
{
_formatFeatures |= f.getMask();
_cfgEmptyStringsToNull = Feature.EMPTY_STRING_AS_NULL.enabledIn(_formatFeatures);
return this;
}

Expand All @@ -301,6 +315,7 @@ public JsonParser enable(YAMLParser.Feature f)
public JsonParser disable(YAMLParser.Feature f)
{
_formatFeatures &= ~f.getMask();
_cfgEmptyStringsToNull = Feature.EMPTY_STRING_AS_NULL.enabledIn(_formatFeatures);
return this;
}

Expand Down Expand Up @@ -520,13 +535,11 @@ protected JsonToken _decodeScalar(ScalarEvent scalar) throws IOException
_textValue = value;
_cleanedTextValue = null;

// [dataformats-text#130]: uncomment for 2.13 either as-is, or
// behind a new feature
/*
if (value.isEmpty()) {
// [dataformats-text#130]: Allow determining whether empty String is
// coerced into null or not
if (!_cfgEmptyStringsToNull && value.isEmpty()) {
return JsonToken.VALUE_STRING;
}
*/

// we may get an explicit tag, if so, use for corroborating...
String typeTag = scalar.getTag();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.fasterxml.jackson.dataformat.yaml.failing;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;

// [dataformats-text#130]: Easy enough to fix, if we choose to,
// but due to timing cannot include in 2.12 (too close to release
Expand All @@ -19,13 +20,28 @@ public void setValue(String str) {
}
}

private final ObjectMapper MAPPER = newObjectMapper();
private final YAMLMapper MAPPER = newObjectMapper();

// [dataformats-text#130]
public void testEmptyValueToNull130() throws Exception
{
Value130 v = MAPPER.readerFor(Value130.class)
// by default, empy Strings are coerced:
assertTrue(MAPPER.getFactory().isEnabled(YAMLParser.Feature.EMPTY_STRING_AS_NULL));

{
Value130 v = MAPPER.readValue("value: \n", Value130.class);
assertNull(v.value);
v = MAPPER.readerFor(Value130.class)
.readValue("value: \n");
assertNull(v.value);
}

// but can change that:
{
Value130 v = MAPPER.readerFor(Value130.class)
.without(YAMLParser.Feature.EMPTY_STRING_AS_NULL)
.readValue("value: \n");
assertEquals("", v.value);
assertEquals("", v.value);
}
}
}

0 comments on commit 1e05426

Please sign in to comment.