Uploaded image for project: 'Commons BeanUtils'
  1. Commons BeanUtils
  2. BEANUTILS-199

[beanutils] BeanUtils.setProperty is over-zealous at converting types

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.6
    • 1.8.0
    • None
    • Operating System: All
      Platform: All

    • 16525

    Description

      As of Release 1.6, BeanUtils.setProperty goes to great lengths to convert types
      into Strings and then from Strings into valid target types, without ever
      considering whether the original value was valid to start with.

      e.g:
      setProperty( bean, "object", new Integer(77) ) ;
      will set the "object" in the bean to the String "77".
      when what is intended is to set it to an Integer.

      copyProperty gets it right, but it doesn't understand nested properties, so it
      isn't useful for my situation.

      PropertyUtils does a better job, but it doesn't do any conversions, so it
      breaks in other situations.

      There doesn't seem to be a reasonable way to say:
      "set nested/indexed/mapped property 'X' in this bean to be value 'Y', and do
      whatever conversions are necessary to get it there"

      The options seem to be:
      "set simple property X to value Y with proper conversions" (BeanUtils.copyProperty)
      "set complex property X to value Y with too many conversions"
      (BeanUtils.setProperty)
      "set complex property X to value Y with no conversions" (PropertyUtils.setProperty)

      Is BeanUtils.setProperty wrong? Or do we need another method that set complex
      properties using the same conversion methds as "copyProperty" ?

      I have a patch (which I'll attach) which makes setProperty do something closer
      to what I think it should do, but I'm not sure if that's the correct approach.

      Attachments

        Activity

          People

            Unassigned Unassigned
            tim@adjective.org Tim Vernum
            Votes:
            5 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: