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