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

Multiple calls to ObjectMapper#canSerialize(Object.class) returns different values. #703

Closed
flexfrank opened this issue Feb 12, 2015 · 5 comments
Milestone

Comments

@flexfrank
Copy link

In 2.5.1, the first call to ObjectMapper#canSerialize(Object.class) returns false.
However, it returns true from the second call.
It seems to be cause by cache mechanism in SerializerProvider class.

Sample code:

ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.canSerialize(Object.class));
System.out.println(mapper.canSerialize(Object.class));
System.out.println(mapper.canSerialize(Object.class));

output:

false
true
true
@flozano
Copy link
Contributor

flozano commented Feb 12, 2015

Reproduced here (and heavily affected), in 2.5.0.

@flozano
Copy link
Contributor

flozano commented Feb 12, 2015

In SerializerProvider:

    protected JsonSerializer<Object> _findExplicitUntypedSerializer(Class<?> runtimeType)
        throws JsonMappingException
    {        
        // Fast lookup from local lookup thingy works?
        JsonSerializer<Object> ser = _knownSerializers.untypedValueSerializer(runtimeType);
        if (ser == null) {
            // If not, maybe shared map already has it?
            ser = _serializerCache.untypedValueSerializer(runtimeType);
            if (ser == null) {
                ser = _createAndCacheUntypedSerializer(runtimeType);
                /* 18-Sep-2014, tatu: This is unfortunate patch over related change
                 *    that pushes creation of "unknown type" serializer deeper down
                 *    in BeanSerializerFactory; as a result, we need to "undo" creation
                 *    here.
                 */
                if (isUnknownTypeSerializer(ser)) {
                    return null;
                }
            }
        }
        return ser;
    }

In first execution of:

        JsonSerializer<Object> ser = _knownSerializers.untypedValueSerializer(runtimeType);

ser==null, whereas in subsequent executions, ser is an instance of UnknownSerializer.

I don't know if this is expected or not... but, if this is OK, it could be quickly fixed by moving this:

                if (isUnknownTypeSerializer(ser)) {
                    return null;
                }

out of the first if() clause.

flozano added a commit to flozano/jackson-databind that referenced this issue Feb 12, 2015
@flozano
Copy link
Contributor

flozano commented Feb 12, 2015

Proposed fix in #704

cowtowncoder added a commit that referenced this issue Feb 13, 2015
cowtowncoder added a commit that referenced this issue Feb 13, 2015
@cowtowncoder cowtowncoder added this to the 2.5.2 milestone Feb 13, 2015
@cowtowncoder
Copy link
Member

@flozano Thank you once again for the fix: I added a test and backported in 2.5 branch (to be included in 2.5.2)

@flozano
Copy link
Contributor

flozano commented Feb 13, 2015

np, thanks for making jackson!

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

3 participants