From cd88cbe95c764553c0e7e04c2c1fb25d8ba5ae34 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Fri, 15 Dec 2023 14:20:01 +0100 Subject: [PATCH] [Generator] Add headerFields and body to UndocumentedPayload (#488) ### Motivation Fixes https://github.com/apple/swift-openapi-generator/issues/299. ~Depends on https://github.com/apple/swift-openapi-runtime/pull/90 landing first and getting released, and the version dependency being bumped here.~ Runtime dependency bumped to 1.1.0. ### Modifications Adapted to the runtime changes, added `headerFields` and `body` properties to `UndocumentedPayload`. Now, the generated code actually also forwards the values to it. ### Result Easier access to the raw response data if an undocumented response is returned. ### Test Plan Adapted snippet and file-based reference tests, and also the unit tests of the generated code (PetstoreConsumerTests). --- Package.swift | 2 +- .../translateClientMethod.swift | 12 ++++- .../ReferenceSources/Petstore/Client.swift | 45 +++++++++++++++---- .../SnippetBasedReferenceTests.swift | 10 ++++- Tests/PetstoreConsumerTests/Test_Client.swift | 7 ++- 5 files changed, 61 insertions(+), 15 deletions(-) diff --git a/Package.swift b/Package.swift index 1a66c0d7..25fa5178 100644 --- a/Package.swift +++ b/Package.swift @@ -60,7 +60,7 @@ let package = Package( // Tests-only: Runtime library linked by generated code, and also // helps keep the runtime library new enough to work with the generated // code. - .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"), + .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.1.0"), .package(url: "https://github.com/apple/swift-http-types", from: "1.0.2"), // Build and preview docs diff --git a/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/translateClientMethod.swift b/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/translateClientMethod.swift index 14b9211e..853c373e 100644 --- a/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/translateClientMethod.swift +++ b/Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/translateClientMethod.swift @@ -112,7 +112,17 @@ extension ClientFileTranslator { .init( label: "statusCode", expression: .identifierPattern("response").dot("status").dot("code") - ), .init(label: nil, expression: .dot("init").call([])), + ), + .init( + label: nil, + expression: .dot("init") + .call([ + .init( + label: "headerFields", + expression: .identifierPattern("response").dot("headerFields") + ), .init(label: "body", expression: .identifierPattern("responseBody")), + ]) + ), ]) ) cases.append(.init(kind: .default, body: [.expression(undocumentedExpr)])) diff --git a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift index ee9fc11c..75c9bf22 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift @@ -274,7 +274,10 @@ public struct Client: APIProtocol { default: return .undocumented( statusCode: response.status.code, - .init() + .init( + headerFields: response.headerFields, + body: responseBody + ) ) } } @@ -316,7 +319,10 @@ public struct Client: APIProtocol { default: return .undocumented( statusCode: response.status.code, - .init() + .init( + headerFields: response.headerFields, + body: responseBody + ) ) } } @@ -389,7 +395,10 @@ public struct Client: APIProtocol { default: return .undocumented( statusCode: response.status.code, - .init() + .init( + headerFields: response.headerFields, + body: responseBody + ) ) } } @@ -441,7 +450,10 @@ public struct Client: APIProtocol { default: return .undocumented( statusCode: response.status.code, - .init() + .init( + headerFields: response.headerFields, + body: responseBody + ) ) } } @@ -472,7 +484,10 @@ public struct Client: APIProtocol { default: return .undocumented( statusCode: response.status.code, - .init() + .init( + headerFields: response.headerFields, + body: responseBody + ) ) } } @@ -544,7 +559,10 @@ public struct Client: APIProtocol { default: return .undocumented( statusCode: response.status.code, - .init() + .init( + headerFields: response.headerFields, + body: responseBody + ) ) } } @@ -656,7 +674,10 @@ public struct Client: APIProtocol { default: return .undocumented( statusCode: response.status.code, - .init() + .init( + headerFields: response.headerFields, + body: responseBody + ) ) } } @@ -787,7 +808,10 @@ public struct Client: APIProtocol { default: return .undocumented( statusCode: response.status.code, - .init() + .init( + headerFields: response.headerFields, + body: responseBody + ) ) } } @@ -891,7 +915,10 @@ public struct Client: APIProtocol { default: return .undocumented( statusCode: response.status.code, - .init() + .init( + headerFields: response.headerFields, + body: responseBody + ) ) } } diff --git a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift index 90f1e818..b64d058a 100644 --- a/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift +++ b/Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift @@ -4492,7 +4492,10 @@ final class SnippetBasedReferenceTests: XCTestCase { default: return .undocumented( statusCode: response.status.code, - .init() + .init( + headerFields: response.headerFields, + body: responseBody + ) ) } } @@ -4675,7 +4678,10 @@ final class SnippetBasedReferenceTests: XCTestCase { default: return .undocumented( statusCode: response.status.code, - .init() + .init( + headerFields: response.headerFields, + body: responseBody + ) ) } } diff --git a/Tests/PetstoreConsumerTests/Test_Client.swift b/Tests/PetstoreConsumerTests/Test_Client.swift index 4c95dc9e..a8195702 100644 --- a/Tests/PetstoreConsumerTests/Test_Client.swift +++ b/Tests/PetstoreConsumerTests/Test_Client.swift @@ -613,13 +613,16 @@ final class Test_Client: XCTestCase { } func testProbe_undocumented() async throws { - transport = .init { request, requestBody, baseURL, operationID in (.init(status: .serviceUnavailable), nil) } + transport = .init { request, requestBody, baseURL, operationID in (.init(status: .serviceUnavailable), "oh no") + } let response = try await client.probe(.init()) - guard case let .undocumented(statusCode, _) = response else { + guard case let .undocumented(statusCode, payload) = response else { XCTFail("Unexpected response: \(response)") return } XCTAssertEqual(statusCode, 503) + XCTAssertEqual(payload.headerFields, [:]) + try await XCTAssertEqualStringifiedData(payload.body, "oh no") } func testUploadAvatarForPet_200() async throws {