Skip to content
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

@XStreamInclude doesn't ignore tags #66

Open
phxql opened this issue Sep 6, 2016 · 6 comments
Open

@XStreamInclude doesn't ignore tags #66

phxql opened this issue Sep 6, 2016 · 6 comments
Assignees

Comments

@phxql
Copy link

phxql commented Sep 6, 2016

Hi,

I'm using XStream with ignoreUnknownElements() enabled. I'm trying to parse

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<test>
    <tags>
        <tag1></tag1>
        <tag2></tag2>
    </tags>
</test>
@XStreamInclude({Tag1.class, Tag2.class})
public interface Tag {}

@XStreamAlias("tag1")
public class Tag1 implements Tag {}

@XStreamAlias("tag2")
public class Tag2 implements Tag {}

So far, so good. If i add <tag3></tag3> to the <tags> list but not to the @XStreamInclude, i assumed XStream will ignore that element. Instead it raises an exception:

Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: 
---- Debugging information ----
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : tag3
class               : java.util.ArrayList
required-type       : java.util.ArrayList
converter-type      : com.thoughtworks.xstream.converters.collections.CollectionConverter
path                : /test/tags/tag3
line number         : 6
class[1]            : xstreaminclude.dto.Test
converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version             : 1.4.9

I've written a reproducer: https://github.com/phxql/xstreaminclude-reproducer

Is there a way to ignore unknown tags in a list?

@joehni joehni self-assigned this Sep 27, 2016
@joehni
Copy link
Member

joehni commented Sep 27, 2016

Hi,

the ignoreUnknownElements flag is only supported for members of a class, not for items of a collection/array/map. The converters handling these types have much less control over their items than the reflection based converters knowing exactly allowed field names and their types.

@phxql
Copy link
Author

phxql commented Sep 28, 2016

Okay. Whats the suggested solution for ignoring collection/array/map items?

@joehni
Copy link
Member

joehni commented Sep 29, 2016

Well, first I'd like to know, why do you expect elements in the list you do not know?

If they are elements, you do no longer support, then add a dummy class, add an alias for the "unknown" element name and add a converter that return such a dummy instance (or null - depends on your use case). You may later process the result and drop these elements from the collection.

If you want to protect from manipulation of the XML, this will not really help, because someone is always able to add arbitrary elements to the list that are valid ... at least for XStream:

<tags>
    <tag1></tag1>
    <tag2></tag2>
    <string>fake an element in tags</string>
</tags>

@phxql
Copy link
Author

phxql commented Sep 30, 2016

I can't control the XML (i read them from a webservice and they can add new list tags every moment) and don't want XStream to fail if there are additional elements. The ignoreUnknownElements() does that for members of a class, IMHO there should be an option for lists, too.

@joehni
Copy link
Member

joehni commented Oct 6, 2016

There's no general solution for all these collection types with the current release. Your best option is to derive from the CollectionConverter and to overload the addCurrentElementToCollection method. Catch the exception thrown when calling readItem and return immediately then.

@phxql
Copy link
Author

phxql commented Oct 7, 2016

Okay, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants