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

Deserialization of an empty list (with empty XML tag) results in null #458

Closed
MarekBak opened this issue Mar 18, 2021 · 3 comments
Closed

Comments

@MarekBak
Copy link

Used version:

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.12.2</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-xml</artifactId>
      <version>2.12.2</version>
    </dependency>

I'm still observing the same behaviour as in #124 At least with that version it is alwas one null element. With others it varies on class nesting.

@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY, getterVisibility=JsonAutoDetect.Visibility.NONE,
        setterVisibility=JsonAutoDetect.Visibility.NONE, creatorVisibility=JsonAutoDetect.Visibility.NONE)
@JsonIgnoreProperties(ignoreUnknown = true)
public class StreamingProxyChannel {
    @JacksonXmlProperty(localName = "streamingProxyChannelId")
    private String streamingProxyChannelId;

    public String getStreamingProxyChannelId() {
        return streamingProxyChannelId;
    }

    public void setStreamingProxyChannelId(String streamingProxyChannelId) {
        this.streamingProxyChannelId = streamingProxyChannelId;
    }
}


@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.ANY, getterVisibility=JsonAutoDetect.Visibility.NONE,
        setterVisibility=JsonAutoDetect.Visibility.NONE, creatorVisibility=JsonAutoDetect.Visibility.NONE)
@JacksonXmlRootElement(localName = "InputProxyChannelStatus")
@JsonIgnoreProperties(ignoreUnknown = true)
public class InputProxyChannelStatus {
    @JacksonXmlProperty(localName = "id")
    private String id;

    @JacksonXmlProperty(localName = "streamingProxyChannelIdList")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<StreamingProxyChannel> streamingProxyChannelIdList;
	
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
	
    public List<StreamingProxyChannel> getStreamingProxyChannelIdList() {
        return streamingProxyChannelIdList;
    }

    public void setStreamingProxyChannelIdList(List<StreamingProxyChannel> streamingProxyChannelIdList) {
        this.streamingProxyChannelIdList = streamingProxyChannelIdList;
    }
}


public class ModelTest {
    private final static XmlMapper xmlMapper = new XmlMapper();

    @Test
    public void inputProxyChannelStatusTest() throws IOException {
        String input = "<InputProxyChannelStatus>\n" +
                "<id>2</id>\n" +
                "<streamingProxyChannelIdList>\n" +
                "</streamingProxyChannelIdList>\n" +
                "</InputProxyChannelStatus>";
        InputProxyChannelStatus inputProxyChannelStatus = xmlMapper.readValue(input, InputProxyChannelStatus.class);
        Assert.assertEquals("List should be empty", 0, inputProxyChannelStatus.getStreamingProxyChannelIdList().size());
    }
}
@cowtowncoder
Copy link
Member

Ok, I hope to a have a look at this in near future; it does sound like a bug from your description.
Thank you for including version information as well: 2.12 does have a lot of List handling fixes.

@cowtowncoder
Copy link
Member

So the situation is a bit different than #124 since you are indicating that list value should be "unwrapped".
Because of that, this entry:

    <streamingProxyChannelIdList>
    </streamingProxyChannelIdList>

is used to construct an instance of streamingProxyChannelId, which is valid (just happens to be missing <streamingProxyChannelId> element.

Now, using useWrapping = true should work but I can see an error for that and will see why that occurs: I don't think that should fail (it should produce an empty List).

@cowtowncoder
Copy link
Member

I think that combination of #460 and use of wrapped-style explain the issue here; closing.

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