这是indexloc提供的服务,不要输入任何密码
Skip to content

Decoding fails for array element objects when using index shorthand notation with URLEncodedFormDecoder #3330

@omochi

Description

@omochi

Describe the issue

When using the index shorthand notation for array elements, decoding the element objects with URLEncodedFormDecoder fails.

Vapor version

4.114.1

Operating system and version

macOS 15.3

Swift version

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)

Steps to reproduce

Suppose we have the following two types:

struct Collapsable: Codable, Equatable {
    var optional: Int?
    var array: [Int]
}

struct ClpArrayHolder: Codable, Equatable {
    var value: [Collapsable]
}

When decoding a ClpArrayHolder with URLEncodedFormDecoder, it works if the element index is explicitly specified, like this:

value[0][optional]=0

However, if the index shorthand notation is used:

value[][optional]=0

decoding fails and throws an exception:

caught error: "dataCorrupted(Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "value", intValue: nil)], debugDescription: "Expected an array but could not parse the data as an array", underlyingError: nil))"

Outcome

No response

Additional notes

I have uploaded a runnable example here for your reference if needed:
https://github.com/omochi/vapor/blob/0c4ddbc7605a33a3e8bb05af1f7e88547a526329/Tests/VaporTests/URLEncodedFormTests.swift#L38-L44

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions