Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7302

LazyMap size returns incorrect result for un-built maps if same key set more than once

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.3.8, 2.3.9, 2.4.0
    • Fix Version/s: 2.3.10, 2.4.1
    • Component/s: JSON
    • Labels:
      None

      Description

      import groovy.json.internal.LazyMap
      
      def map = new LazyMap()
      map.someProperty = "1"
      map.someProperty = "2"
      map.someProperty = "3"
      println "M: ${map.size()}"  // prints 3  // buildIfNeeded hasn't been called
      println map.someProperty  // access map property, buildIfNeeded is called
      println "M: ${map.size()}" // prints 1
      

      LazyMap's put() adds values to it's internal array even if it's a redundant key, which leads to size() returning an incorrect result if the LazyMap hasn't been built yet. One fix would be to have size call buildIfNeeded() before returning the size.

      I ran into this after modifying json that was loaded via JsonSlurper.

        Attachments

          Activity

            People

            • Assignee:
              melix C├ędric Champeau
              Reporter:
              bastamper Brian Stamper
            • Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: