-
-
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 2 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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import tools.jackson.databind.cfg.MapperConfig; | ||
import tools.jackson.databind.introspect.*; | ||
import tools.jackson.dataformat.xml.annotation.*; | ||
import tools.jackson.dataformat.xml.deser.FromXmlParser; | ||
|
||
/** | ||
* Extension of {@link JacksonAnnotationIntrospector} that is needed to support | ||
|
@@ -208,6 +209,10 @@ public PropertyName findNameForDeserialization(MapperConfig<?> config, Annotated | |
PropertyName pn = PropertyName.merge(_findXmlName(a), | ||
super.findNameForDeserialization(config, a)); | ||
if (pn == null) { | ||
if(_hasAnnotation(a, JacksonXmlText.class)){ | ||
return PropertyName.construct(FromXmlParser.DEFAULT_TEXT_PROPERTY); | ||
} | ||
|
||
if (_hasOneOf(a, ANNOTATIONS_TO_INFER_XML_PROP)) { | ||
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. This will look for 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 do not quite understand, the highlighted line only looks for 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. Sorry, I meant line 225 right after ( |
||
return PropertyName.USE_DEFAULT; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,7 +68,7 @@ public String extractScalarFromObject(JsonParser p, ValueDeserializer<?> deser, | |
final String propName = p.currentName(); | ||
JsonToken t = p.nextToken(); | ||
if (t == JsonToken.VALUE_STRING) { | ||
if (propName.equals("")) { | ||
if (FromXmlParser.DEFAULT_TEXT_PROPERTY.equals(propName)) { | ||
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 think this needs to actually consider Will change to do that. |
||
text = p.getString(); | ||
} | ||
} else { | ||
|
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. 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. Ok. Thank you for your help here @duoduobingbing . |
||
.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.
Quick notes: not just
_hasAnnotation
--JacksonXmlText.value()
must betrue
(specifyingfalse
can be used to disable annotation's effect).But I have related concern: there now won't be a way to override name of logical property to use as it's effectively hard-coded. Should there be a way to re-configure that? And if so, how?
(
MapperConfig
passed won't have access to XML-specific config mapper has. But maybe this introspector should have its own config or something.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.
For the first note: This has been fixed in the latest commit of this pull request
For the second note: I've added a config on the introspector so that this can be configured.