Skip to content

Commit

Permalink
Merge pull request #133 from mattpolzin/bugfix/server-variable-enum
Browse files Browse the repository at this point in the history
fix incorrectly required enum property on server variables.
  • Loading branch information
mattpolzin authored Aug 15, 2020
2 parents 4b5f646 + 26a5a2e commit 9c08102
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Sources/OpenAPIKit/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ extension OpenAPI.Server.Variable: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(`enum`, forKey: .enum)
if !`enum`.isEmpty {
try container.encode(`enum`, forKey: .enum)
}

try container.encode(`default`, forKey: .default)

Expand All @@ -164,7 +166,7 @@ extension OpenAPI.Server.Variable: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

`enum` = try container.decode([String].self, forKey: .enum)
`enum` = try container.decodeIfPresent([String].self, forKey: .enum) ?? []

`default` = try container.decode(String.self, forKey: .default)

Expand Down
3 changes: 1 addition & 2 deletions Tests/OpenAPIKitCompatibilitySuite/GoogleBooksAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ final class GoogleBooksAPICampatibilityTests: XCTestCase {

XCTAssertNotNil(addBooksParameters)
XCTAssertEqual(addBooksParameters?.count, 11)
XCTAssertEqual(addBooksParameters?.first?.description, "JSONP")
XCTAssertEqual(addBooksParameters?.first?.context, .query)
XCTAssert(addBooksParameters?.contains { $0.description == "JSONP" && $0.context == .query } ?? false)
}

func test_dereferencedComponents() throws {
Expand Down
53 changes: 53 additions & 0 deletions Tests/OpenAPIKitTests/ServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,59 @@ extension ServerTests {
)
}

func test_minimalServerVariable_decode() {
let serverData =
"""
{
"url": "https://hello.com",
"variables": {
"world": {
"default": "cool"
}
}
}
""".data(using: .utf8)!

let serverDecoded = try! orderUnstableDecode(Server.self, from: serverData)

XCTAssertEqual(
serverDecoded,
Server(
url: URL(string: "https://hello.com")!,
variables: [
"world": .init(
default: "cool"
)
]
)
)
}

func test_minimalServerVariable_encode() {
let server = Server(
url: URL(string: "https://hello.com")!,
variables: [
"world": .init(
default: "cool"
)
]
)
let encodedServer = try! orderUnstableTestStringFromEncoding(of: server)

assertJSONEquivalent(encodedServer,
"""
{
"url" : "https:\\/\\/hello.com",
"variables" : {
"world" : {
"default" : "cool"
}
}
}
"""
)
}

func test_maximalServer_decode() {
let serverData =
"""
Expand Down

0 comments on commit 9c08102

Please sign in to comment.