-
-
Notifications
You must be signed in to change notification settings - Fork 228
Naive attempt at fixing #306 #754
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
Changes from all commits
c53bc27
8989990
8d2e14c
c41f2dd
6ed7c4b
ff896b6
5c68f0a
3146147
2e8e2b5
feaf2e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package tools.jackson.dataformat.xml; | ||
|
||
import tools.jackson.databind.PropertyName; | ||
import tools.jackson.dataformat.xml.deser.FromXmlParser; | ||
|
||
import java.io.Serializable; | ||
|
||
public record JacksonXmlAnnotationIntrospectorConfig( | ||
boolean inferXmlTextPropertyName, | ||
PropertyName xmlTextPropertyName //Only honored if inferXmlTextPropertyName is false | ||
) implements Serializable { | ||
|
||
/** | ||
* Constructs a JacksonXmlAnnotationIntrospectorConfig with the default configuration | ||
* Does not infer the XmlTextPropertyName by default and uses {@link FromXmlParser#DEFAULT_TEXT_PROPERTY} for the {@link PropertyName}. | ||
*/ | ||
public JacksonXmlAnnotationIntrospectorConfig() { | ||
this(false, PropertyName.construct(FromXmlParser.DEFAULT_TEXT_PROPERTY)); | ||
} | ||
|
||
public JacksonXmlAnnotationIntrospectorConfig withInferXmlTextPropertyName(boolean inferXmlTextPropertyName) { | ||
return new JacksonXmlAnnotationIntrospectorConfig(inferXmlTextPropertyName, this.xmlTextPropertyName); | ||
} | ||
|
||
public JacksonXmlAnnotationIntrospectorConfig withXmlTextPropertyName(PropertyName xmlTextPropertyName) { | ||
return new JacksonXmlAnnotationIntrospectorConfig(this.inferXmlTextPropertyName, xmlTextPropertyName); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package tools.jackson.dataformat.xml.deser; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import tools.jackson.databind.DatabindException; | ||
import tools.jackson.databind.PropertyName; | ||
import tools.jackson.dataformat.xml.JacksonXmlAnnotationIntrospector; | ||
import tools.jackson.dataformat.xml.JacksonXmlAnnotationIntrospectorConfig; | ||
import tools.jackson.dataformat.xml.XmlMapper; | ||
import tools.jackson.dataformat.xml.XmlTestUtil; | ||
import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty; | ||
import tools.jackson.dataformat.xml.annotation.JacksonXmlText; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class DifferentDeserializationPropertyNameTest extends XmlTestUtil | ||
{ | ||
static class TestBean { | ||
@JacksonXmlProperty(localName = "wrong") | ||
String wrong; | ||
|
||
@JacksonXmlText | ||
String name; | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Test methods | ||
/********************************************************************** | ||
*/ | ||
|
||
@Test | ||
public void testWithExplicitProperty() { | ||
final XmlMapper mapper = XmlMapper.builder() | ||
.annotationIntrospector(new JacksonXmlAnnotationIntrospector(false, | ||
new JacksonXmlAnnotationIntrospectorConfig(false, new PropertyName("name")))) | ||
.build(); | ||
|
||
String xmlInput = "<testBean>ABC123</testBean>"; | ||
|
||
TestBean testBean = mapper.readValue(xmlInput, TestBean.class); | ||
|
||
assertEquals("ABC123", testBean.name); | ||
} | ||
|
||
@Test | ||
public void testWithInferName() { | ||
final XmlMapper mapper = XmlMapper.builder() | ||
.annotationIntrospector(new JacksonXmlAnnotationIntrospector(false, | ||
new JacksonXmlAnnotationIntrospectorConfig(true, null))) | ||
.build(); | ||
|
||
String xmlInput = "<testBean>DEF</testBean>"; | ||
|
||
TestBean testBean = mapper.readValue(xmlInput, TestBean.class); | ||
|
||
assertEquals("DEF", testBean.name); | ||
} | ||
|
||
@Test | ||
public void testWithDuplicateExplicitProperty() { | ||
final XmlMapper mapper = XmlMapper.builder() | ||
.annotationIntrospector(new JacksonXmlAnnotationIntrospector(false, | ||
new JacksonXmlAnnotationIntrospectorConfig(false, new PropertyName("wrong")))) | ||
.build(); | ||
|
||
String xmlInput = "<testBean>DEF</testBean>"; | ||
|
||
Exception result = assertThrows(DatabindException.class, () -> mapper.readValue(xmlInput, TestBean.class)); | ||
|
||
assertTrue(result.getMessage().contains("Multiple fields representing property \"wrong\"")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import tools.jackson.databind.node.JsonNodeType; | ||
import tools.jackson.databind.node.ObjectNode; | ||
import tools.jackson.dataformat.xml.XmlTestUtil; | ||
import tools.jackson.dataformat.xml.deser.FromXmlParser; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
|
@@ -43,7 +44,7 @@ public void testMixedContent() throws Exception | |
{ | ||
JsonNode fromXml = XML_MAPPER.readTree("<root>first<a>123</a>second<b>abc</b>last</root>"); | ||
final ObjectNode exp = XML_MAPPER.createObjectNode(); | ||
exp.putArray("") | ||
exp.putArray(FromXmlParser.DEFAULT_TEXT_PROPERTY) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohhhhhhh. This is NOT good -- what used to be something like:
now looks like:
which I don't think is what anyone likes to see :-( So for We need to figure out another way to handle the issue, I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I mean this PR was just a naive shot at solving #306 by changing the property name, if it has to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. I'll leave this open because I'd really like to figure out a way and feel there probably is a way (despite not seeing it yet). :) I appreciate your attempt; sorry it took me a while to sync up to what changes really mean. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No Problem, thanks for having a look at it. I will close this for now. Feel free to reopen or use the code in any way, shape or form you see fit. |
||
.add("first") | ||
.add("second") | ||
.add("last"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
import tools.jackson.databind.ObjectMapper; | ||
import tools.jackson.databind.json.JsonMapper; | ||
import tools.jackson.dataformat.xml.XmlTestUtil; | ||
import tools.jackson.dataformat.xml.deser.FromXmlParser; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
|
@@ -19,23 +20,23 @@ public class JsonNodeMixedContent403Test extends XmlTestUtil | |
public void testMixedContentBefore() throws Exception | ||
{ | ||
// First, before elements: | ||
assertEquals(JSON_MAPPER.readTree(a2q("{'':'before','a':'1','b':'2'}")), | ||
assertEquals(JSON_MAPPER.readTree(a2q(String.format("{'%s':'before','a':'1','b':'2'}", FromXmlParser.DEFAULT_TEXT_PROPERTY))), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and same here obviously. |
||
XML_MAPPER.readTree("<root>before<a>1</a><b>2</b></root>")); | ||
} | ||
|
||
@Test | ||
public void testMixedContentBetween() throws Exception | ||
{ | ||
// Second, between | ||
assertEquals(JSON_MAPPER.readTree(a2q("{'a':'1','':'between','b':'2'}")), | ||
assertEquals(JSON_MAPPER.readTree(a2q(String.format("{'a':'1','%s':'between','b':'2'}", FromXmlParser.DEFAULT_TEXT_PROPERTY))), | ||
XML_MAPPER.readTree("<root><a>1</a>between<b>2</b></root>")); | ||
} | ||
|
||
@Test | ||
public void testMixedContentAfter() throws Exception | ||
{ | ||
// and then after | ||
assertEquals(JSON_MAPPER.readTree(a2q("{'a':'1','b':'2','':'after'}")), | ||
assertEquals(JSON_MAPPER.readTree(a2q(String.format("{'a':'1','b':'2','%s':'after'}", FromXmlParser.DEFAULT_TEXT_PROPERTY))), | ||
XML_MAPPER.readTree("<root><a>1</a><b>2</b>after</root>")); | ||
} | ||
|
||
|
@@ -44,7 +45,7 @@ public void testMultipleMixedContent() throws Exception | |
{ | ||
// and then after | ||
assertEquals(JSON_MAPPER.readTree( | ||
a2q("{'':['first','second','third'],'a':'1','b':'2'}")), | ||
a2q(String.format("{'%s':['first','second','third'],'a':'1','b':'2'}", FromXmlParser.DEFAULT_TEXT_PROPERTY))), | ||
XML_MAPPER.readTree("<root>first<a>1</a>second<b>2</b>third</root>")); | ||
} | ||
|
||
|
@@ -57,7 +58,7 @@ public void testMixed226() throws Exception | |
+" mixed2</a>\n" | ||
+"</root>"; | ||
JsonNode fromJson = JSON_MAPPER.readTree( | ||
a2q("{'a':{'':['mixed1 ',' mixed2'],'b':'leaf'}}")); | ||
a2q(String.format("{'a':{'%s':['mixed1 ',' mixed2'],'b':'leaf'}}", FromXmlParser.DEFAULT_TEXT_PROPERTY))); | ||
assertEquals(fromJson, XML_MAPPER.readTree(XML)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will look for
@JacksonXmlText
OR@JacksonXmlElementWrapper
and handling will now differ -- former should use configured "text element name", latter should not (should return "use default").There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not quite understand, the highlighted line only looks for
@JacksonXmlText
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I meant line 225 right after (
if (_hasOneOf(...)
).