Skip to content

StringCollectionSerializer calls JsonGenerator.setCurrentValue(value), which messes up current value for sibling properties #2475

@bohnman

Description

@bohnman

Versions: 2.9.9 and 2.10.0.pr3

Given the following class definition:

public static class Item {
        private Collection<String> set;
        private String id;

        public Item(String id) {
            this.set = new HashSet<>();
            this.id = id;
        }

        public Collection<String> getSet() {
            return set;
        }

        public String getId() {
            return id;
        }
    }

You'll notice that when the serializer writes out the set and id properties, the current value on jgen is HashSet not the Item. I write a Jackson filtering library and I count on the current value being correct.

Doing some digging, I noticed that the StringCollectionSerializer sets the currentValue in the serialize method like so:

@Override
    public void serialize(Collection<String> value, JsonGenerator g,
            SerializerProvider provider) throws IOException
    {
        g.setCurrentValue(value);
        final int len = value.size();
        if (len == 1) {
            if (((_unwrapSingle == null) &&
                    provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
                    || (_unwrapSingle == Boolean.TRUE)) {
                serializeContents(value, g, provider);
                return;
            }
        }
        g.writeStartArray(len);
        serializeContents(value, g, provider);
        g.writeEndArray();
    }

However, the current value is not explicity set in IndexedStringListSerializer nor CollectionSerializer, and consequently they both don't exhibit this problem

I have attached a java class that exhibits this behavior as long as you're using Jackson version 2.9.9 or later
Test.java.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions