Skip to content

EnvironmentVariablesProvider: split(separator:) leads to inconsistent boolean array decoding between whitespace-only and empty elements #122

@Adobels

Description

@Adobels

In EnvironmentVariablesProvider.swift:347, array decoding relies on:

string.split(separator: separator).map { $0.trimmed() }

This approach leads to inconsistent behavior when parsing comma-separated values of whitespace-only or empty elements.

Current behavior

split(separator:) only creates elements when the element separated with separator is a one or more characters string. Empty string, two commas one after another, does not produces an empty element.
Additionally, trimmed() applied to a string containing only a space returns the string unchanged.

Examples:

  • Input:
    "true, 1,,YES" → boolDecoder receives: [true, 1, YES]
  • Input:
    "true, 1, , YES" → boolDecoder receives: [true, 1, , YES] -> This leads to a nil value during decoding. Currently protected by a guard, resulting in a throw

Additional observations

", ," .components(separatedBy: ",")   // count == 3
", ," .split(separator: ",")          // count == 1

components(separatedBy:) behaves more homogeneously for:

  • empty strings
  • strings containing only whitespace

This makes it more predictable when parsing arrays of strings that are expected to represent boolean values.

Proposed solution

Replace:

string.split(separator: separator)

with:
string.components(separatedBy: separator)

Impact / considerations

This change would affect all array decodings, not only booleans, analyze the impact across other array types handled by EnvironmentVariablesProvider

Reproduction

SwiftFiddle playground demonstrating the behavior:
https://swiftfiddle.com/vqjd6ajtfbchtb34hzejvnakiq

This issue is a follow-up to a discussion here: #120 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions