Skip to content

Commit cd88cbe

Browse files
authored
[Generator] Add headerFields and body to UndocumentedPayload (#488)
### Motivation Fixes #299. ~Depends on apple/swift-openapi-runtime#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).
1 parent 50cf5ac commit cd88cbe

File tree

5 files changed

+61
-15
lines changed

5 files changed

+61
-15
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ let package = Package(
6060
// Tests-only: Runtime library linked by generated code, and also
6161
// helps keep the runtime library new enough to work with the generated
6262
// code.
63-
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
63+
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.1.0"),
6464
.package(url: "https://github.com/apple/swift-http-types", from: "1.0.2"),
6565

6666
// Build and preview docs

Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/translateClientMethod.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,17 @@ extension ClientFileTranslator {
112112
.init(
113113
label: "statusCode",
114114
expression: .identifierPattern("response").dot("status").dot("code")
115-
), .init(label: nil, expression: .dot("init").call([])),
115+
),
116+
.init(
117+
label: nil,
118+
expression: .dot("init")
119+
.call([
120+
.init(
121+
label: "headerFields",
122+
expression: .identifierPattern("response").dot("headerFields")
123+
), .init(label: "body", expression: .identifierPattern("responseBody")),
124+
])
125+
),
116126
])
117127
)
118128
cases.append(.init(kind: .default, body: [.expression(undocumentedExpr)]))

Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore/Client.swift

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,10 @@ public struct Client: APIProtocol {
274274
default:
275275
return .undocumented(
276276
statusCode: response.status.code,
277-
.init()
277+
.init(
278+
headerFields: response.headerFields,
279+
body: responseBody
280+
)
278281
)
279282
}
280283
}
@@ -316,7 +319,10 @@ public struct Client: APIProtocol {
316319
default:
317320
return .undocumented(
318321
statusCode: response.status.code,
319-
.init()
322+
.init(
323+
headerFields: response.headerFields,
324+
body: responseBody
325+
)
320326
)
321327
}
322328
}
@@ -389,7 +395,10 @@ public struct Client: APIProtocol {
389395
default:
390396
return .undocumented(
391397
statusCode: response.status.code,
392-
.init()
398+
.init(
399+
headerFields: response.headerFields,
400+
body: responseBody
401+
)
393402
)
394403
}
395404
}
@@ -441,7 +450,10 @@ public struct Client: APIProtocol {
441450
default:
442451
return .undocumented(
443452
statusCode: response.status.code,
444-
.init()
453+
.init(
454+
headerFields: response.headerFields,
455+
body: responseBody
456+
)
445457
)
446458
}
447459
}
@@ -472,7 +484,10 @@ public struct Client: APIProtocol {
472484
default:
473485
return .undocumented(
474486
statusCode: response.status.code,
475-
.init()
487+
.init(
488+
headerFields: response.headerFields,
489+
body: responseBody
490+
)
476491
)
477492
}
478493
}
@@ -544,7 +559,10 @@ public struct Client: APIProtocol {
544559
default:
545560
return .undocumented(
546561
statusCode: response.status.code,
547-
.init()
562+
.init(
563+
headerFields: response.headerFields,
564+
body: responseBody
565+
)
548566
)
549567
}
550568
}
@@ -656,7 +674,10 @@ public struct Client: APIProtocol {
656674
default:
657675
return .undocumented(
658676
statusCode: response.status.code,
659-
.init()
677+
.init(
678+
headerFields: response.headerFields,
679+
body: responseBody
680+
)
660681
)
661682
}
662683
}
@@ -787,7 +808,10 @@ public struct Client: APIProtocol {
787808
default:
788809
return .undocumented(
789810
statusCode: response.status.code,
790-
.init()
811+
.init(
812+
headerFields: response.headerFields,
813+
body: responseBody
814+
)
791815
)
792816
}
793817
}
@@ -891,7 +915,10 @@ public struct Client: APIProtocol {
891915
default:
892916
return .undocumented(
893917
statusCode: response.status.code,
894-
.init()
918+
.init(
919+
headerFields: response.headerFields,
920+
body: responseBody
921+
)
895922
)
896923
}
897924
}

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4492,7 +4492,10 @@ final class SnippetBasedReferenceTests: XCTestCase {
44924492
default:
44934493
return .undocumented(
44944494
statusCode: response.status.code,
4495-
.init()
4495+
.init(
4496+
headerFields: response.headerFields,
4497+
body: responseBody
4498+
)
44964499
)
44974500
}
44984501
}
@@ -4675,7 +4678,10 @@ final class SnippetBasedReferenceTests: XCTestCase {
46754678
default:
46764679
return .undocumented(
46774680
statusCode: response.status.code,
4678-
.init()
4681+
.init(
4682+
headerFields: response.headerFields,
4683+
body: responseBody
4684+
)
46794685
)
46804686
}
46814687
}

Tests/PetstoreConsumerTests/Test_Client.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,13 +613,16 @@ final class Test_Client: XCTestCase {
613613
}
614614

615615
func testProbe_undocumented() async throws {
616-
transport = .init { request, requestBody, baseURL, operationID in (.init(status: .serviceUnavailable), nil) }
616+
transport = .init { request, requestBody, baseURL, operationID in (.init(status: .serviceUnavailable), "oh no")
617+
}
617618
let response = try await client.probe(.init())
618-
guard case let .undocumented(statusCode, _) = response else {
619+
guard case let .undocumented(statusCode, payload) = response else {
619620
XCTFail("Unexpected response: \(response)")
620621
return
621622
}
622623
XCTAssertEqual(statusCode, 503)
624+
XCTAssertEqual(payload.headerFields, [:])
625+
try await XCTAssertEqualStringifiedData(payload.body, "oh no")
623626
}
624627

625628
func testUploadAvatarForPet_200() async throws {

0 commit comments

Comments
 (0)