Skip to content

Default typing adds typing info to collections even with @JsonTypeInfo(use = Id.NONE) override #1391

Description

@jmax01

Version: Jackson 2.8.2
JDK: 1.8.0_60

It appears that enableDefaultTypingAsProperty adds type info to collections regardless if @JsonTypeInfo(use = Id.NONE) is specified on the property.

 ({"treeSetStrings":["java.util.TreeSet",[]],"immutableSortedSetStrings":["com.google.common.collect.EmptyImmutableSortedSet",[]]})
import static org.junit.Assert.*;

import java.util.SortedSet;
import java.util.TreeSet;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
import com.google.common.collect.ImmutableSortedSet;

@SuppressWarnings("javadoc")
public class ObjectAndNonConcreteCollectionsTest {

    final static ObjectMapper WITH_OBJECT_AND_NON_CONCRETE = new ObjectMapper().enableDefaultTypingAsProperty(
            DefaultTyping.OBJECT_AND_NON_CONCRETE, "$type");

    final static ObjectMapper PLAIN_OBJECT_MAPPER = new ObjectMapper();

    static class MyClass {

        private final SortedSet<String> treeSetStrings = new TreeSet<>();

        private final SortedSet<String> immutableSortedSetStrings = ImmutableSortedSet.of();

        @JsonTypeInfo(use = Id.NONE)
        public SortedSet<String> getTreeSetStrings() {
            return this.treeSetStrings;
        }

        @JsonTypeInfo(use = Id.NONE)
        public SortedSet<String> getImmutableSortedSetStrings() {
            return this.immutableSortedSetStrings;
        }
    }

    @Test
    public void testCollectionTyping() throws JsonProcessingException {

        final MyClass toSerialize = new MyClass();

        final String fromPlainObjectMapper = PLAIN_OBJECT_MAPPER.writeValueAsString(toSerialize);
        System.out.println("\nfrom plain ObjectMapper:\n" + fromPlainObjectMapper);

        final String fromObjectMapperWithDefaultTyping = WITH_OBJECT_AND_NON_CONCRETE.writeValueAsString(toSerialize);
        System.out.println("\nfrom ObjectMapper with default typing:\n" + fromObjectMapperWithDefaultTyping);

        assertEquals(fromPlainObjectMapper, fromObjectMapperWithDefaultTyping);

    }

}

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    has-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issue

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions