Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update URIValueFromNodeDecoder for DeepObject. #125

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ final class URIValueFromNodeDecoder {
/// - Throws: When a decoding error occurs.
func decodeRootIfPresent<T: Decodable>(_ type: T.Type = T.self) throws -> T? {
// The root is only nil if the node is empty.
if try currentElementAsArray().isEmpty { return nil }
if style != .deepObject, try currentElementAsArray().isEmpty {
return nil
}
return try decodeRoot(type)
}
}
Expand Down
24 changes: 24 additions & 0 deletions Tests/OpenAPIRuntimeTests/Conversion/Test_Converter+Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ final class Test_ServerConverterExtensions: Test_Runtime {
XCTAssertEqual(value, ["foo", "bar"])
}

func test_getOptionalQueryItemAsURI_deepObject_exploded() throws {
let query: Substring = "sort%5Bid%5D=ascending&sort%5Bname%5D=descending"
let value: [String: String]? = try converter.getOptionalQueryItemAsURI(
in: query,
style: .deepObject,
explode: true,
name: "sort",
as: [String: String].self
)
XCTAssertEqual(value, ["id": "ascending", "name": "descending"])
}

func test_getRequiredQueryItemAsURI_arrayOfStrings() throws {
let query: Substring = "search=foo&search=bar"
let value: [String] = try converter.getRequiredQueryItemAsURI(
Expand All @@ -195,6 +207,18 @@ final class Test_ServerConverterExtensions: Test_Runtime {
XCTAssertEqual(value, ["foo", "bar"])
}

func test_getRequiredQueryItemAsURI_deepObject_exploded() throws {
let query: Substring = "sort%5Bid%5D=ascending&sort%5Bname%5D=descending"
let value: [String: String] = try converter.getRequiredQueryItemAsURI(
in: query,
style: .deepObject,
explode: true,
name: "sort",
as: [String: String].self
)
XCTAssertEqual(value, ["id": "ascending", "name": "descending"])
}

func test_getOptionalQueryItemAsURI_date() throws {
let query: Substring = "search=\(testDateEscapedString)"
let value: Date? = try converter.getOptionalQueryItemAsURI(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ final class Test_URIValueFromNodeDecoder: Test_Runtime {
case blue
}

struct SimpleDeepObject: Decodable, Equatable {
let id: String
let name: String
}

// An empty string.
try test(["root": [""]], "", key: "root")

Expand Down Expand Up @@ -88,6 +93,14 @@ final class Test_URIValueFromNodeDecoder: Test_Runtime {
key: "root"
)

try test(
["id": ["ascending"], "name": ["descending"]],
SimpleDeepObject(id: "ascending", name: "descending"),
key: "sort",
style: .deepObject,
explode: true
)

func test<T: Decodable & Equatable>(
_ node: URIParsedNode,
_ expectedValue: T,
Expand Down
Loading