Skip to content

Commit b35daeb

Browse files
committed
Update URIValueFromNodeDecoder for DeepObject.
### Motivation As discussed in [[Generator] Add support of deepObject style in query params #538](apple/swift-openapi-generator#538 (comment)), there is a misimplementation of the `decodeRootIfPresent` method in `URIValueFromNodeDecoder`. When using the `deepObject` style, the node for `rootValue` is correctly empty, containing only the sub-objects. Without this PR, the tests for [Add support of deepObject style in query params #538](apple/swift-openapi-generator#538) are failing. ### Modifications - Updated the `decodeRootIfPresent(_ type:) throws -> T` method to ignore whether `currentElementAsArray` is empty or not. ### Result - Enables the tests in the [Generator part of the PR](apple/swift-openapi-generator#538) to pass successfully. ### Test Plan This PR includes unit tests that validate the change to `decodeRootIfPresent(_ type:) throws -> T` within `Test_Converter+Server` as well as in `Test_serverConversionExtensions`.
1 parent daa2fb5 commit b35daeb

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

Sources/OpenAPIRuntime/URICoder/Decoding/URIValueFromNodeDecoder.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ final class URIValueFromNodeDecoder {
8282
/// - Throws: When a decoding error occurs.
8383
func decodeRootIfPresent<T: Decodable>(_ type: T.Type = T.self) throws -> T? {
8484
// The root is only nil if the node is empty.
85-
if try currentElementAsArray().isEmpty { return nil }
85+
if style != .deepObject, try currentElementAsArray().isEmpty {
86+
return nil
87+
}
8688
return try decodeRoot(type)
8789
}
8890
}

Tests/OpenAPIRuntimeTests/Conversion/Test_Converter+Server.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@ final class Test_ServerConverterExtensions: Test_Runtime {
171171
XCTAssertEqual(value, ["foo", "bar"])
172172
}
173173

174+
func test_getOptionalQueryItemAsURI_deepObject_exploded() throws {
175+
let query: Substring = "sort%5Bid%5D=ascending&sort%5Bname%5D=descending"
176+
let value: [String: String]? = try converter.getOptionalQueryItemAsURI(
177+
in: query,
178+
style: .deepObject,
179+
explode: true,
180+
name: "sort",
181+
as: [String: String].self
182+
)
183+
XCTAssertEqual(value, ["id": "ascending", "name": "descending"])
184+
}
185+
174186
func test_getRequiredQueryItemAsURI_arrayOfStrings() throws {
175187
let query: Substring = "search=foo&search=bar"
176188
let value: [String] = try converter.getRequiredQueryItemAsURI(
@@ -195,6 +207,18 @@ final class Test_ServerConverterExtensions: Test_Runtime {
195207
XCTAssertEqual(value, ["foo", "bar"])
196208
}
197209

210+
func test_getRequiredQueryItemAsURI_deepObject_exploded() throws {
211+
let query: Substring = "sort%5Bid%5D=ascending&sort%5Bname%5D=descending"
212+
let value: [String: String] = try converter.getRequiredQueryItemAsURI(
213+
in: query,
214+
style: .deepObject,
215+
explode: true,
216+
name: "sort",
217+
as: [String: String].self
218+
)
219+
XCTAssertEqual(value, ["id": "ascending", "name": "descending"])
220+
}
221+
198222
func test_getOptionalQueryItemAsURI_date() throws {
199223
let query: Substring = "search=\(testDateEscapedString)"
200224
let value: Date? = try converter.getOptionalQueryItemAsURI(

Tests/OpenAPIRuntimeTests/URICoder/Decoder/Test_URIValueFromNodeDecoder.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ final class Test_URIValueFromNodeDecoder: Test_Runtime {
3535
case blue
3636
}
3737

38+
struct SimpleDeepObject: Decodable, Equatable {
39+
let id: String
40+
let name: String
41+
}
42+
3843
// An empty string.
3944
try test(["root": [""]], "", key: "root")
4045

@@ -88,6 +93,14 @@ final class Test_URIValueFromNodeDecoder: Test_Runtime {
8893
key: "root"
8994
)
9095

96+
try test(
97+
["id": ["ascending"], "name": ["descending"]],
98+
SimpleDeepObject(id: "ascending", name: "descending"),
99+
key: "sort",
100+
style: .deepObject,
101+
explode: true
102+
)
103+
91104
func test<T: Decodable & Equatable>(
92105
_ node: URIParsedNode,
93106
_ expectedValue: T,

0 commit comments

Comments
 (0)