Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tools.jackson.databind.*;
import tools.jackson.databind.deser.*;
import tools.jackson.databind.deser.bean.BeanDeserializerBase;
import tools.jackson.databind.deser.bean.BuilderBasedDeserializer;
import tools.jackson.databind.deser.std.DelegatingDeserializer;
import tools.jackson.databind.jsontype.TypeDeserializer;
import tools.jackson.databind.util.TokenBuffer;
Expand Down Expand Up @@ -33,6 +34,12 @@ public class XmlTextDeserializer

protected final ValueInstantiator _valueInstantiator;

/**
* A flag indicating the ValueInstantiator.createUsingDefault()
* will return an instance (and specifically not a builder)
*/
protected final boolean _canCreateUsingDefault;

/*
/**********************************************************************
/* Construction
Expand All @@ -51,6 +58,8 @@ public XmlTextDeserializer(BeanDeserializerBase delegate, SettableBeanProperty p
_xmlTextProperty = prop;
_xmlTextPropertyIndex = prop.getPropertyIndex();
_valueInstantiator = delegate.getValueInstantiator();
_canCreateUsingDefault = _valueInstantiator.canCreateUsingDefault() &&
!(delegate instanceof BuilderBasedDeserializer);
}

/**
Expand All @@ -65,6 +74,8 @@ public XmlTextDeserializer(BeanDeserializerBase delegate, int textPropIndex)
_xmlTextPropertyIndex = textPropIndex;
_valueInstantiator = delegate.getValueInstantiator();
_xmlTextProperty = delegate.findProperty(textPropIndex);
_canCreateUsingDefault = _valueInstantiator.canCreateUsingDefault() &&
!(delegate instanceof BuilderBasedDeserializer);
}

/**
Expand All @@ -82,6 +93,8 @@ public XmlTextDeserializer(ValueDeserializer<?> delegate,
_xmlTextPropertyIndex = textPropIndex;
_valueInstantiator = inner.getValueInstantiator();
_xmlTextProperty = inner.findProperty(textPropIndex);
_canCreateUsingDefault = _valueInstantiator.canCreateUsingDefault() &&
!(inner instanceof BuilderBasedDeserializer);
}

/*
Expand Down Expand Up @@ -122,7 +135,7 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt)
throws JacksonException
{
if (p.currentToken() == JsonToken.VALUE_STRING) {
if (_valueInstantiator.canCreateUsingDefault()) {
if (_canCreateUsingDefault) {
Object bean = _valueInstantiator.createUsingDefault(ctxt);
_xmlTextProperty.deserializeAndSet(p, ctxt, bean);
return bean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package tools.jackson.dataformat.xml.deser.builder;


import org.junit.jupiter.api.Test;


import tools.jackson.databind.annotation.JsonDeserialize;
import tools.jackson.databind.annotation.JsonPOJOBuilder;

import tools.jackson.dataformat.xml.XmlMapper;
import tools.jackson.dataformat.xml.XmlTestUtil;
import tools.jackson.dataformat.xml.annotation.*;

import static org.junit.jupiter.api.Assertions.*;

public class BuilderWithTextValueTest extends XmlTestUtil
{
@JsonDeserialize(builder = Outer.OuterBuilder.class)
public static class Outer {

final Inner inner;

final String title;

private Outer(Inner inner, String title) {
this.inner = inner;
this.title = title;
}

public static OuterBuilder builder() {
return new OuterBuilder();
}

@JsonPOJOBuilder(withPrefix = "")
public static class OuterBuilder {
private Inner inner;
private String title;

@JacksonXmlProperty(localName = "Inner")
public OuterBuilder inner(Inner inner) {
this.inner = inner;
return this;
}

@JacksonXmlProperty(localName = "Title")
public OuterBuilder title(String title) {
this.title = title;
return this;
}

public Outer build() {
return new Outer(inner, title);
}
}
}

@JsonDeserialize(builder = Inner.InnerBuilder.class)
public static class Inner {

final String value;

final String title;

private Inner(String value, String title) {
this.value = value;
this.title = title;
}

public static InnerBuilder builder() {
return new InnerBuilder();
}

@JsonPOJOBuilder(withPrefix = "")
public static class InnerBuilder {
private String value;
private String title;

@JacksonXmlText
public InnerBuilder value(String value) {
this.value = value;
return this;
}

@JacksonXmlProperty(isAttribute = true)
public InnerBuilder title(String title) {
this.title = title;
return this;
}

public Inner build() {
return new Inner(value, title);
}
}
}

@Test
public void testNestedBuilderWithTextValue() throws Exception
{
XmlMapper mapper = XmlMapper.builder().build();
final String XML = "<Outer><Inner>Value</Inner></Outer>";
Outer outer = mapper.readValue(XML, Outer.class);
assertEquals("Value", outer.inner.value);
}
}