Skip to content

Commit 9c08102

Browse files
authored
Merge pull request #133 from mattpolzin/bugfix/server-variable-enum
fix incorrectly required enum property on server variables.
2 parents 4b5f646 + 26a5a2e commit 9c08102

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

Sources/OpenAPIKit/Server.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ extension OpenAPI.Server.Variable: Encodable {
150150
public func encode(to encoder: Encoder) throws {
151151
var container = encoder.container(keyedBy: CodingKeys.self)
152152

153-
try container.encode(`enum`, forKey: .enum)
153+
if !`enum`.isEmpty {
154+
try container.encode(`enum`, forKey: .enum)
155+
}
154156

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

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

167-
`enum` = try container.decode([String].self, forKey: .enum)
169+
`enum` = try container.decodeIfPresent([String].self, forKey: .enum) ?? []
168170

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

Tests/OpenAPIKitCompatibilitySuite/GoogleBooksAPITests.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ final class GoogleBooksAPICampatibilityTests: XCTestCase {
118118

119119
XCTAssertNotNil(addBooksParameters)
120120
XCTAssertEqual(addBooksParameters?.count, 11)
121-
XCTAssertEqual(addBooksParameters?.first?.description, "JSONP")
122-
XCTAssertEqual(addBooksParameters?.first?.context, .query)
121+
XCTAssert(addBooksParameters?.contains { $0.description == "JSONP" && $0.context == .query } ?? false)
123122
}
124123

125124
func test_dereferencedComponents() throws {

Tests/OpenAPIKitTests/ServerTests.swift

+53
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,59 @@ extension ServerTests {
7575
)
7676
}
7777

78+
func test_minimalServerVariable_decode() {
79+
let serverData =
80+
"""
81+
{
82+
"url": "https://hello.com",
83+
"variables": {
84+
"world": {
85+
"default": "cool"
86+
}
87+
}
88+
}
89+
""".data(using: .utf8)!
90+
91+
let serverDecoded = try! orderUnstableDecode(Server.self, from: serverData)
92+
93+
XCTAssertEqual(
94+
serverDecoded,
95+
Server(
96+
url: URL(string: "https://hello.com")!,
97+
variables: [
98+
"world": .init(
99+
default: "cool"
100+
)
101+
]
102+
)
103+
)
104+
}
105+
106+
func test_minimalServerVariable_encode() {
107+
let server = Server(
108+
url: URL(string: "https://hello.com")!,
109+
variables: [
110+
"world": .init(
111+
default: "cool"
112+
)
113+
]
114+
)
115+
let encodedServer = try! orderUnstableTestStringFromEncoding(of: server)
116+
117+
assertJSONEquivalent(encodedServer,
118+
"""
119+
{
120+
"url" : "https:\\/\\/hello.com",
121+
"variables" : {
122+
"world" : {
123+
"default" : "cool"
124+
}
125+
}
126+
}
127+
"""
128+
)
129+
}
130+
78131
func test_maximalServer_decode() {
79132
let serverData =
80133
"""

0 commit comments

Comments
 (0)