groovy
  1. groovy
  2. GROOVY-4958

ConfigSlurper doesn't parse config files that contain $ signs in property values correctly

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.8.1
    • Fix Version/s: 1.8.5, 2.0-beta-2
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      1

      Description

      When a ConfigObject is written to file all values are quoted with ". This leads to problems if a value contains a $ for example.

      Consider the following code snipet:

      def config = new ConfigObject()
      config.instObject = '1. Dyn. Aktber. ($IO_AKTBER)   # Aktenbereich (V-AKTBER)'
      
      println "config before writing to file:    ${config}"
      
      def file = new File('C:/Temp/config.groovy')
      file.withWriter { writer ->
          config.writeTo(writer)
      }
      
      config = new ConfigSlurper().parse(file.getText())
      println "config after reloading from file: ${config}"
      

      The output is:

      config before writing to file:    [instObject:1. Dyn. Aktber. ($IO_AKTBER)   # Aktenbereich (V-AKTBER)]
      config after reloading from file: [IO_AKTBER:[:], instObject:1. Dyn. Aktber. ([:])   # Aktenbereich (V-AKTBER)]
      

      The according file content:

      instObject="1. Dyn. Aktber. ($IO_AKTBER)   # Aktenbereich (V-AKTBER)"
      

      As can be seen the two config objects differ widely. A solution could perhaps be writing properties quoted by ' or writing the properties the way they are quoted in the source code, e. g. config.property1 = "$property1" would become property1 = "$property1" in the file and config.property2 = '$property1' would become property2 = '$property1'. This way the programmer could control the desired behaviour.

        Activity

        Hide
        Roshan Dawrani added a comment -

        ConfigObject.writeTo() depends on inspect(), which writes strings in form "a" instead of 'a'.

        def list = ['a', 'b']
        println list.inspect() // produces ["a", "b"]. Wouldn't it be Groovier, more correct to produce ['a', 'b'], to avoid GString mix-up possibility?
        

        Attaching the patch for review that solves the ConfigObject issue reported here through this small change in inspect().

        Does anyone see any negative impact of this change?

        Show
        Roshan Dawrani added a comment - ConfigObject.writeTo() depends on inspect(), which writes strings in form "a" instead of 'a'. def list = ['a', 'b'] println list.inspect() // produces [ "a" , "b" ]. Wouldn't it be Groovier, more correct to produce ['a', 'b'], to avoid GString mix-up possibility? Attaching the patch for review that solves the ConfigObject issue reported here through this small change in inspect(). Does anyone see any negative impact of this change?
        Hide
        CÚdric Champeau added a comment -

        I think this is the right way to do this, but it seems your patch doesn't change the escaping of double quotes to escaping of simple quotes.

        Show
        CÚdric Champeau added a comment - I think this is the right way to do this, but it seems your patch doesn't change the escaping of double quotes to escaping of simple quotes.
        Hide
        Roshan Dawrani added a comment -

        Fixed. Escaping of double quotes is now replaced by that of single quotes.

        Thanks for reviewing the patch, Cedric.

        Show
        Roshan Dawrani added a comment - Fixed. Escaping of double quotes is now replaced by that of single quotes. Thanks for reviewing the patch, Cedric.

          People

          • Assignee:
            Roshan Dawrani
            Reporter:
            Erich Paul
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: