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

Strange behaviour of an empty item (but with whitespace between start/end tags) in List #191

Closed
Hronom opened this issue Apr 18, 2016 · 10 comments
Milestone

Comments

@Hronom
Copy link

Hronom commented Apr 18, 2016

Strange behaviour of empty item in list (v2.7.3)

So I have next xml file TestList.xml:

<TestList>
    <items>
        <item name="Item1"/>
        <item name="Item2">
        </item>
        <item name="Item3"/>
        <item name="Item4"/>
        <item name="Item5"/>
    </items>
</TestList>

But when I deserializing this file, I lost all items after item with name Item2, so items: Item3, Item4, Item5 is lost. If I change this xml file to next format:

<TestList>
    <items>
        <item name="Item1"/>
        <item name="Item2"/>
        <item name="Item3"/>
        <item name="Item4"/>
        <item name="Item5"/>
    </items>
</TestList>

All start working as expected.

In version 2.6.3 all work as expected...

Code listing

pom.xml

        <!-- Jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.7.3</version>
        </dependency>

App.java

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) throws IOException {
        Path testListPath = Paths.get("TestList.xml");
        ObjectMapper mapper = new XmlMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        TestList testList = mapper.readValue(Files.newInputStream(testListPath), TestList.class);
        System.out.println();
    }
}

TestList.java

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

import java.util.ArrayList;

public class TestList {
    @JacksonXmlElementWrapper(useWrapping = true, localName = "items")
    @JacksonXmlProperty(localName = "item")
    public ArrayList<ListItem> items = new ArrayList<>();
}

ListItem.java

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

public class ListItem {
    @JacksonXmlProperty(isAttribute = true)
    public String name;
}
@cowtowncoder
Copy link
Member

Looks like a bug.

I suspect this is because entry with Item2 has both attribute value and textual content. Although textual content is "empty" from human perspective, from XML perspective it has distinctive non-empty value (that is, String length above 0).

@Hronom
Copy link
Author

Hronom commented Apr 19, 2016

@cowtowncoder Maybe some kind of flag that can give different behaviours, Is there any flag to achieve behaviour like in 2.6.3 ?

@cowtowncoder
Copy link
Member

@Hronom this is not intentional, so I don't think there is any need to support existing behavior. Problem is more with figuring out to fix the problem. Did this behave differently with 2.6.3?

@Hronom
Copy link
Author

Hronom commented Apr 20, 2016

@cowtowncoder Yes when I change dependencies to 2.6.3:

        <!-- Jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.6.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.6.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.6.3</version>
        </dependency>

All works as expected.

@cowtowncoder
Copy link
Member

@Hronom thank you for additional information. This must then be a regression due to fixes included in 2.7; handling of empty Strings is a tricky problem.

@cowtowncoder
Copy link
Member

One thing on test: it's usually a good idea NOT to disable "fail on unknown properties". Enabling it often masks legitimate failures. Not a big deal here, just thought I'll mention it.

@Hronom
Copy link
Author

Hronom commented Apr 21, 2016

@cowtowncoder thanks for your advice, I think I enable it on real project.

@cowtowncoder
Copy link
Member

@Hronom right, there are cases where it's useful to disable it, but you can also just disable it for specific types with @JsonIgnoreProperties(ignoreUnknown=true).

I can reproduce this issue now with 2.7.3. Thanks!

cowtowncoder added a commit that referenced this issue Apr 21, 2016
@cowtowncoder
Copy link
Member

Guessing this might be due fix for #177, which would also affect 2.6.5.

@cowtowncoder cowtowncoder added this to the 2.7.4 milestone Apr 22, 2016
@cowtowncoder cowtowncoder changed the title Strange behaviour of empty item in list (v2.7.3) Strange behaviour of empty item in list Apr 22, 2016
@cowtowncoder cowtowncoder changed the title Strange behaviour of empty item in list Strange behaviour of an empty item (but with whitespace between start/end tags) in List Apr 22, 2016
@cowtowncoder
Copy link
Member

Was able to fix this with minor modification to #177 fix; and looks like this also took care of #178.

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

No branches or pull requests

2 participants