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

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.3.8, 2.3.9, 2.4.0
    • 2.3.10, 2.4.1
    • JSON
    • 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

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            melix Cédric Champeau
            bastamper Brian Stamper
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment