From c67737ce525c89f7e0641db5c95509494f1d990b Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Wed, 2 Aug 2023 16:45:09 +0100 Subject: [PATCH 1/4] Add tests for responses with examples Signed-off-by: Si Beaumont --- .../SnippetBasedReferenceTests.swift | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift index 510db702..86bfb21c 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift @@ -772,6 +772,80 @@ final class SnippetBasedReferenceTests: XCTestCase { ) ) } + + func testResponseWithExampleWithSummaryAndValue() throws { + try self.assertResponsesTranslation( + """ + responses: + MyResponse: + description: Some response + content: + application/json: + schema: + type: string + examples: + application/json: + summary: "a hello response" + value: "hello" + """, + """ + public enum Responses { + public struct MyResponse: Sendable, Equatable, Hashable { + public struct Headers: Sendable, Equatable, Hashable { public init() {} } + public var headers: Components.Responses.MyResponse.Headers + @frozen public enum Body: Sendable, Equatable, Hashable { + case json(Swift.String) + } + public var body: Components.Responses.MyResponse.Body + public init( + headers: Components.Responses.MyResponse.Headers = .init(), + body: Components.Responses.MyResponse.Body + ) { + self.headers = headers + self.body = body + } + } + } + """ + ) + } + + func testResponseWithExampleWithOnlyValue() throws { + XCTExpectFailure("Throws decoding error", strict: true) + try self.assertResponsesTranslation( + """ + responses: + MyResponse: + description: Some response + content: + application/json: + schema: + type: string + examples: + application/json: + summary: "a hello response" + """, + """ + public enum Responses { + public struct MyResponse: Sendable, Equatable, Hashable { + public struct Headers: Sendable, Equatable, Hashable { public init() {} } + public var headers: Components.Responses.MyResponse.Headers + @frozen public enum Body: Sendable, Equatable, Hashable { + case json(Swift.String) + } + public var body: Components.Responses.MyResponse.Body + public init( + headers: Components.Responses.MyResponse.Headers = .init(), + body: Components.Responses.MyResponse.Body + ) { + self.headers = headers + self.body = body + } + } + } + """ + ) + } } extension SnippetBasedReferenceTests { From 6f7a5de051faa5551211638fdec56f6b1a7964f5 Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Thu, 3 Aug 2023 10:29:39 +0100 Subject: [PATCH 2/4] fixup: Use XCTAssertThrowsError instead of XCTExpectFailure Signed-off-by: Si Beaumont --- .../SnippetBasedReferenceTests.swift | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift index 86bfb21c..5f54b2a4 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift @@ -811,8 +811,7 @@ final class SnippetBasedReferenceTests: XCTestCase { } func testResponseWithExampleWithOnlyValue() throws { - XCTExpectFailure("Throws decoding error", strict: true) - try self.assertResponsesTranslation( + XCTAssertThrowsError(try self.assertResponsesTranslation( """ responses: MyResponse: @@ -844,7 +843,9 @@ final class SnippetBasedReferenceTests: XCTestCase { } } """ - ) + )) { error in + XCTAssert(error is DecodingError) + } } } From dd31421a16f3411ee76a9501a5027d9658808b72 Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Thu, 3 Aug 2023 11:46:36 +0100 Subject: [PATCH 3/4] swift-format --- .../SnippetBasedReferenceTests.swift | 62 ++++++++++--------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift index 5f54b2a4..385b14aa 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift @@ -811,39 +811,41 @@ final class SnippetBasedReferenceTests: XCTestCase { } func testResponseWithExampleWithOnlyValue() throws { - XCTAssertThrowsError(try self.assertResponsesTranslation( - """ - responses: - MyResponse: - description: Some response - content: - application/json: - schema: - type: string - examples: + XCTAssertThrowsError( + try self.assertResponsesTranslation( + """ + responses: + MyResponse: + description: Some response + content: application/json: - summary: "a hello response" - """, - """ - public enum Responses { - public struct MyResponse: Sendable, Equatable, Hashable { - public struct Headers: Sendable, Equatable, Hashable { public init() {} } - public var headers: Components.Responses.MyResponse.Headers - @frozen public enum Body: Sendable, Equatable, Hashable { - case json(Swift.String) - } - public var body: Components.Responses.MyResponse.Body - public init( - headers: Components.Responses.MyResponse.Headers = .init(), - body: Components.Responses.MyResponse.Body - ) { - self.headers = headers - self.body = body + schema: + type: string + examples: + application/json: + summary: "a hello response" + """, + """ + public enum Responses { + public struct MyResponse: Sendable, Equatable, Hashable { + public struct Headers: Sendable, Equatable, Hashable { public init() {} } + public var headers: Components.Responses.MyResponse.Headers + @frozen public enum Body: Sendable, Equatable, Hashable { + case json(Swift.String) + } + public var body: Components.Responses.MyResponse.Body + public init( + headers: Components.Responses.MyResponse.Headers = .init(), + body: Components.Responses.MyResponse.Body + ) { + self.headers = headers + self.body = body + } } } - } - """ - )) { error in + """ + ) + ) { error in XCTAssert(error is DecodingError) } } From 49c56b82d97ff31d86d27d455e69c86d83666e8d Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Thu, 3 Aug 2023 11:46:58 +0100 Subject: [PATCH 4/4] add comment tracking upstream issue --- .../SnippetBasedReferenceTests.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift index 385b14aa..2bebc6fd 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift @@ -811,6 +811,8 @@ final class SnippetBasedReferenceTests: XCTestCase { } func testResponseWithExampleWithOnlyValue() throws { + // This test currently throws because the parsing of ExampleObject is too strict: + // https://github.com/mattpolzin/OpenAPIKit/issues/286. XCTAssertThrowsError( try self.assertResponsesTranslation( """