From 8aa68eda40a03bc5c3cda75066fa21a6000cdcda Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Sat, 15 Aug 2020 08:47:02 -0700 Subject: [PATCH 1/2] fix incorrectly required enum property on server variables. --- Sources/OpenAPIKit/Server.swift | 6 ++- Tests/OpenAPIKitTests/ServerTests.swift | 53 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/Sources/OpenAPIKit/Server.swift b/Sources/OpenAPIKit/Server.swift index c847d655a..52a18760e 100644 --- a/Sources/OpenAPIKit/Server.swift +++ b/Sources/OpenAPIKit/Server.swift @@ -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) @@ -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) diff --git a/Tests/OpenAPIKitTests/ServerTests.swift b/Tests/OpenAPIKitTests/ServerTests.swift index 5a919be46..520a7be89 100644 --- a/Tests/OpenAPIKitTests/ServerTests.swift +++ b/Tests/OpenAPIKitTests/ServerTests.swift @@ -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 = """ From 26a5a2e5213d779b5ada38d8e70ce1b079b45514 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Sat, 15 Aug 2020 09:28:39 -0700 Subject: [PATCH 2/2] Make assertion based on third party spec work regardless of parameter order. --- Tests/OpenAPIKitCompatibilitySuite/GoogleBooksAPITests.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Tests/OpenAPIKitCompatibilitySuite/GoogleBooksAPITests.swift b/Tests/OpenAPIKitCompatibilitySuite/GoogleBooksAPITests.swift index 663742d80..21667572b 100644 --- a/Tests/OpenAPIKitCompatibilitySuite/GoogleBooksAPITests.swift +++ b/Tests/OpenAPIKitCompatibilitySuite/GoogleBooksAPITests.swift @@ -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 {