Issue Bug Closed

#94852Importing YAML files with numeric keys yields unexpected results

Johannes Nielsen 08/12/21, 15:33 updated 07/09/24, 12:45

Description

When using imports in YAML files (e.g. in forms framework), numeric keys are not respected because the YamlFileLoader calls ArrayUtility::replaceAndAppendScalarValuesRecursive which in turn calls array_merge, which does not respect numeric keys.
As an example, say one has three YAML files:

#imports.yaml:
imports:
  - { resource: './file1.yaml' }
  - { resource: './file2.yaml' }

#file1.yaml:
TYPO3:
  CMS:
    Form:
      prototypes:
        standard:
          formElementsDefinition:
            Form:
              formEditor:
                editors:
                  900:
                    selectOptions:
                      35:
                        value: 'FirstValue'
                        label: 'First option'

#file2.yaml:
TYPO3:
  CMS:
    Form:
      prototypes:
        standard:
          formElementsDefinition:
            Form:
              formEditor:
                editors:
                  900:
                    selectOptions:
                      45:
                        value: 'SecondValue'
                        label: 'Second option'

The expected result would be:

TYPO3:
  CMS:
    Form:
      prototypes:
        standard:
          formElementsDefinition:
            Form:
              formEditor:
                editors:
                  900: # 900 is respected as key
                    selectOptions:
                      35:
                        value: 'FirstValue'
                        label: 'First option'
                      45:
                        value: 'FirstValue'
                        label: 'First option'

However, the actual result is

TYPO3:
  CMS:
    Form:
      prototypes:
        standard:
          formElementsDefinition:
            Form:
              formEditor:
                editors:
                  # array_merge is called on the editors array, so that numeric keys get lost
                  0:
                    selectOptions:
                      35:
                        value: 'FirstValue'
                        label: 'First option'
                  1:
                    selectOptions:
                      45:
                        value: 'FirstValue'
                        label: 'First option'

As a workaround, one can add an empty non-numeric key to the second import – given that the order is known, which could be reversed if the corresponding feature-flag is set, so it would be better to add this to all imports:

#file2.yaml:
TYPO3:
  CMS:
    Form:
      prototypes:
        standard:
          formElementsDefinition:
            Form:
              formEditor:
                editors:
                  _: # This makes this an associative array and yields the desired result plus this empty property 
                  900:
                    selectOptions:
                      45:
                        value: 'SecondValue'
                        label: 'Second option'

Reviews 2

History 46

  1. Johannes Nielsen changed subject Importing YAML files with numberic keys yields unexpected results Importing YAML files with numeric keys yields unexpected results 08/13/21, 06:28

  2. Gerrit Code Review pushed patch set 1 for main to change 84980 status New Under Review 06/27/24, 22:13

  3. Gerrit Code Review pushed patch set 1 for 12.4 to change 84957 06/28/24, 04:44

  4. Georg Ringer commented and changed status Under Review Resolved done 0 % 100 % 06/28/24, 04:50

    Applied in changeset commit:911b01d0644026723366d8e0249dc9e492577706.

  5. Georg Ringer commented 06/28/24, 04:50

    Hi Johannes,

    the issue has been fixed in at least 12. I just added a test to avoid potential regressions

  6. Benni Mack changed status Resolved Closed 07/09/24, 12:45