Menu

#269 opencsv modifies BeanUtils shared instance which might cause unexpected behaviors

v1.0 (example)
open
None
5
2026-02-22
2026-02-17
No

opencsv reconfigures the shared instance of ConvertUtils within Apache Commons BeanUtils to throw exceptions when no value is provided, this can cause issues on projects using opencsv where this behavior is not expected.

By default, ConvertUtils.convert(null, Integer.class) returns 0. This is expected by most projects using Apache BeanUtils.

opencsv ConverterPrimitiveTypes class alters this shared instance behavior to throw exceptions instead:

BeanUtilsBean.getInstance().getConvertUtils().register(true, false, 0); // true for throwException

After this line is run, any other usage of ConvertUtils outside of opencsv sharing the same instance will then throw an exception, which is likely not the expected behavior.

ConvertUtils.convert(null, Integer.class) // now throws org.apache.commons.beanutils.ConversionException: No value specified for 'Integer'

Perhaps it would make sense for opencsv to use a new instance of BeanUtilsBean instead of modifying the default behavior of BeanUtilsBean.getInstance() which is shared within the classloader, avoiding unexpected side effects.

Discussion

  • Scott Conway

    Scott Conway - 2026-02-22
    • assigned_to: Scott Conway
     
  • Scott Conway

    Scott Conway - 2026-02-22

    Hello Felipe

    The defaults have been there since 2018 and we have tried twice to modify this class and finally had to revert it back to the current version.

    https://sourceforge.net/p/opencsv/bugs/242/

    https://sourceforge.net/p/opencsv/bugs/253/

    The biggest issue is that the current BeanUtilsBean.getInstance() doesn't just return the current instance with the current converters - it unregisters all the current converters. When created there was no way of determining if there was converters already registered. The newer version of beanutils may have corrected this and if so it might be possible to add in code to use existing converters instead of creating new ones.

     

Log in to post a comment.

MongoDB Logo MongoDB