Skip to content

Commit

Permalink
[Generator] Add headerFields and body to UndocumentedPayload (#488)
Browse files Browse the repository at this point in the history
### 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).
  • Loading branch information
czechboy0 authored Dec 15, 2023
1 parent 50cf5ac commit cd88cbe
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@ public struct Client: APIProtocol {
default:
return .undocumented(
statusCode: response.status.code,
.init()
.init(
headerFields: response.headerFields,
body: responseBody
)
)
}
}
Expand Down Expand Up @@ -316,7 +319,10 @@ public struct Client: APIProtocol {
default:
return .undocumented(
statusCode: response.status.code,
.init()
.init(
headerFields: response.headerFields,
body: responseBody
)
)
}
}
Expand Down Expand Up @@ -389,7 +395,10 @@ public struct Client: APIProtocol {
default:
return .undocumented(
statusCode: response.status.code,
.init()
.init(
headerFields: response.headerFields,
body: responseBody
)
)
}
}
Expand Down Expand Up @@ -441,7 +450,10 @@ public struct Client: APIProtocol {
default:
return .undocumented(
statusCode: response.status.code,
.init()
.init(
headerFields: response.headerFields,
body: responseBody
)
)
}
}
Expand Down Expand Up @@ -472,7 +484,10 @@ public struct Client: APIProtocol {
default:
return .undocumented(
statusCode: response.status.code,
.init()
.init(
headerFields: response.headerFields,
body: responseBody
)
)
}
}
Expand Down Expand Up @@ -544,7 +559,10 @@ public struct Client: APIProtocol {
default:
return .undocumented(
statusCode: response.status.code,
.init()
.init(
headerFields: response.headerFields,
body: responseBody
)
)
}
}
Expand Down Expand Up @@ -656,7 +674,10 @@ public struct Client: APIProtocol {
default:
return .undocumented(
statusCode: response.status.code,
.init()
.init(
headerFields: response.headerFields,
body: responseBody
)
)
}
}
Expand Down Expand Up @@ -787,7 +808,10 @@ public struct Client: APIProtocol {
default:
return .undocumented(
statusCode: response.status.code,
.init()
.init(
headerFields: response.headerFields,
body: responseBody
)
)
}
}
Expand Down Expand Up @@ -891,7 +915,10 @@ public struct Client: APIProtocol {
default:
return .undocumented(
statusCode: response.status.code,
.init()
.init(
headerFields: response.headerFields,
body: responseBody
)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4492,7 +4492,10 @@ final class SnippetBasedReferenceTests: XCTestCase {
default:
return .undocumented(
statusCode: response.status.code,
.init()
.init(
headerFields: response.headerFields,
body: responseBody
)
)
}
}
Expand Down Expand Up @@ -4675,7 +4678,10 @@ final class SnippetBasedReferenceTests: XCTestCase {
default:
return .undocumented(
statusCode: response.status.code,
.init()
.init(
headerFields: response.headerFields,
body: responseBody
)
)
}
}
Expand Down
7 changes: 5 additions & 2 deletions Tests/PetstoreConsumerTests/Test_Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cd88cbe

Please sign in to comment.