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

MismatchedInputException when deserializing a polymorphic class with JacksonXmlElementWrapper #308

Closed
ser0l opened this issue Sep 4, 2018 · 3 comments
Milestone

Comments

@ser0l
Copy link

ser0l commented Sep 4, 2018

Using jackson 2.9.6, i get an exception:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token

When trying to deserialize a polymorphic object with an arraylist wrapper present, when type attribute is not positioned first in XML. See the following code for an example (incorrectDeserialization test throws the exception):

package com.wrappertest;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;

import static com.fasterxml.jackson.annotation.JsonSubTypes.*;
import static com.fasterxml.jackson.annotation.JsonTypeInfo.*;
import static org.junit.Assert.assertEquals;

public class ListWrapperWithPolymorphicTypesTest {
    /**************** classes ****************/
    public static class ListWrapper {
        @JacksonXmlElementWrapper(useWrapping = false)
        public ArrayList<Long> item;
    }

    @JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type")
    @JsonSubTypes({
            @Type(value = ChildClass.class, name = "ChildClass")
    })
    public static abstract class BaseClass {
        public Long baseId;
    }

    public static class ChildClass extends BaseClass {
        public ListWrapper someIds;
    }

    /**************** tests ****************/
    private final XmlMapper xmlMapper = new XmlMapper();

    @Test
    public void correctDeserialization() throws Exception {
        ChildClass value = (ChildClass) xmlMapper.readValue(goodXml(), BaseClass.class);
        doAsserts(value);
    }

    @Test
    public void incorrectDeserialization() throws Exception {
        ChildClass value = (ChildClass) xmlMapper.readValue(badXml(), BaseClass.class);
        doAsserts(value);
    }

    private void doAsserts(ChildClass value) {
        assertEquals(Long.valueOf(123), value.baseId);
        assertEquals(Arrays.asList(1L, 2L), value.someIds.item);
    }

    /** type attribute positioned first, works correctly */
    private String goodXml() {
        return "<root>" +
               "  <type>ChildClass</type>" +
               "  <baseId>123</baseId>" +
               "  <someIds>" +
               "    <item>1</item>" +
               "    <item>2</item>" +
               "  </someIds>" +
               "</root>";
    }

    /** type attribute positioned second, throws exception when parsed */
    private String badXml() {
        return "<root>" +
               "  <baseId>123</baseId>" +
               "  <type>ChildClass</type>" +
               "  <someIds>" +
               "    <item>1</item>" +
               "    <item>2</item>" +
               "  </someIds>" +
               "</root>";
    }
}

Full stack trace

@ailjushkin
Copy link

ailjushkin commented Oct 17, 2019

@cowtowncoder @prb @christophercurrie Hey guys, can you please take a look at this, thank you.

@Arcnor
Copy link

Arcnor commented Oct 17, 2019

@ailjushkin not sure why you mention me, I'm not a developer for this project. Good luck anyway!

@cowtowncoder cowtowncoder added this to the 2.10 milestone Nov 13, 2020
@cowtowncoder
Copy link
Member

Cannot reproduce any more; looks like this was fixed in 2.10.0. Possibly due to fix for #242.

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

4 participants