FasterXML / jackson-datatype-joda

Extension module to properly support full datatype set of Joda datetime library
Apache License 2.0
140 stars 81 forks source link

`ObjectMapper.setDefaultLeniency()` is causing `NullPointerException` in `JacksonJodaDateFormat` #113

Closed brenuart closed 4 years ago

brenuart commented 5 years ago

(Jackson version: 2.10.1)

Consider the following example:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
mapper.setDefaultLeniency(false);
mapper.writeValueAsString(new org.joda.time.DateTime());

This code raises the following exception:

java.lang.NullPointerException
    at com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat.with(JacksonJodaDateFormat.java:123)
    at com.fasterxml.jackson.datatype.joda.ser.JodaDateSerializerBase.createContextual(JodaDateSerializerBase.java:94)
    at com.fasterxml.jackson.databind.SerializerProvider.handleSecondaryContextualization(SerializerProvider.java:1004)
    at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:524)
    at com.fasterxml.jackson.databind.SerializerProvider.findTypedValueSerializer(SerializerProvider.java:713)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:308)
    at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:4094)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3404)
...

This is caused by JsonFormat.Value#getPattern() returning null at line https://github.com/FasterXML/jackson-datatype-joda/blob/master/src/main/java/com/fasterxml/jackson/datatype/joda/cfg/JacksonJodaDateFormat.java#L123.

Note that getPattern returns an empty string when JsonFormat comes from an annotation, but null when created because of ObjectMapper.setDefaultLenciency().

cowtowncoder commented 5 years ago

Thank you for reporting this. I'll transfer it to Joda module.

brenuart commented 5 years ago

I was wondering if the issue belongs to the joda module or not. The problem is caused by the JsonFormat.Value containing a null pattern value when created by one of the with*() methods but having an empty string when used as an annotation. If this is normal and expected - then indeed the issue belongs to Joda...

cowtowncoder commented 5 years ago

@brenuart Ah. Ok, in that case I think I better have a look myself. It is possible change is needed in multiple places. Thank you for additional information!