Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenAPI documents for the toolchains and Swift evolution APIs #841

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@
/_posts/* @timsneath @tkremenek @shahmishal

/gsoc*/ @ktoso

/openapi/ @czechboy0
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to review future changes to the OpenAPI documents, but that'd require me to receive write access - I'll leave that up to the SWWG to decide whether that's valuable for me to have or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shahmishal if we're happy with this we'll need to add @czechboy0 to the access list for the repo

1 change: 1 addition & 0 deletions _includes/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<p class="privacy">
<a href="//www.apple.com/privacy/privacy-policy/">Privacy Policy</a>
<a href="//www.apple.com/legal/privacy/en-ww/cookies/">Cookies</a>
<a href="/openapi">API</a>
</p>
</div>
<div class="footer-other">
Expand Down
3 changes: 3 additions & 0 deletions openapi/TestSwiftOrgClient/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.build
.swiftpm
Package.resolved
38 changes: 38 additions & 0 deletions openapi/TestSwiftOrgClient/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// swift-tools-version: 6.0
import PackageDescription

let clientNames: [String] = [
"swiftorgClient",
"downloadswiftorgClient",
]

let package = Package(
name: "TestSwiftOrgClient",
platforms: [
.macOS(.v13),
],
dependencies: [
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"),
],
targets: [
.target(
name: "Shared",
path: "Shared"
)
] + clientNames.map { name in
.executableTarget(
name: name,
dependencies: [
"Shared",
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
.product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"),
],
path: name,
plugins: [
.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator"),
]
)
}
)
54 changes: 54 additions & 0 deletions openapi/TestSwiftOrgClient/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Test Swift.org client

This simple Swift package contains a Swift HTTP Client that can be used to test the Swift.org API.

It is generated from the OpenAPI document of Swift.org and generated using Swift OpenAPI Generator.

## Usage

You can run it either against the Swift.org API or against a locally running API.

By default, the tool runs against the production Swift.org APIs.

Run against production using:
```
SWIFTORG_SERVER_NAME=prod swift run swiftorgClient
DOWNLOADSWIFTORG_SERVER_NAME=prod swift run downloadswiftorgClient
```

Run against a locally running server using:
```
SWIFTORG_SERVER_NAME=local swift run swiftorgClient
```

The exact server URLs are defined in the OpenAPI document.

If the tool detects a deserialization error, possibly because the OpenAPI document does not correctly parse the returned data, it will exit with a non-zero exit code.

If all test cases run successfully, the tool exits with 0.

Sample output:

```
% SWIFTORG_SERVER_NAME=prod swift run swiftorgClient
Testing SwiftOrg API at https://www.swift.org/api/v1...
✅ listReleases
✅ listDevToolchains(main, amazonlinux2)
✅ listDevToolchains(main, centos7)
✅ listDevToolchains(main, macos)
✅ listDevToolchains(main, ubi9)
✅ listDevToolchains(main, ubuntu2004)
✅ listDevToolchains(main, ubuntu2204)
✅ listDevToolchains(main, windows10)
✅ listDevToolchains(6.0, amazonlinux2)
✅ listDevToolchains(6.0, centos7)
✅ listDevToolchains(6.0, macos)
✅ listDevToolchains(6.0, ubi9)
✅ listDevToolchains(6.0, ubuntu2004)
✅ listDevToolchains(6.0, ubuntu2204)
✅ listDevToolchains(6.0, windows10)
✅ listStaticSDKDevToolchains(main)
✅ listStaticSDKDevToolchains(6.0)
% DOWNLOADSWIFTORG_SERVER_NAME=prod swift run downloadswiftorgClient
✅ listProposals
```
32 changes: 32 additions & 0 deletions openapi/TestSwiftOrgClient/Shared/Tester.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Foundation

package struct Test: Sendable {
package var name: String
package var work: @Sendable () async throws -> Void

package init(name: String, work: @Sendable @escaping () async throws -> Void) {
self.name = name
self.work = work
}
}

package struct Tester {
package static func run(_ tests: [Test]) async throws {
var results: [(String, Error?)] = []
func runTest(_ test: Test) async {
do {
try await test.work()
results.append((test.name, nil))
print("✅ \(test.name)")
} catch {
results.append((test.name, error))
print("❌ \(test.name): \(String(describing: error))")
}
}
for test in tests {
await runTest(test)
}
let failed = results.contains(where: { $0.1 != nil })
exit(failed ? 1 : 0)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import OpenAPIRuntime
import HTTPTypes
import Foundation

// Workaround for issue: https://github.com/swiftlang/swift-org-website/issues/847
struct Issue847WorkaroundMiddleware: ClientMiddleware {
func intercept(
_ request: HTTPRequest,
body: HTTPBody?,
baseURL: URL,
operationID: String,
next: (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?)
) async throws -> (HTTPResponse, HTTPBody?) {
var (response, responseBody) = try await next(request, body, baseURL)
guard
operationID == "listProposals" &&
response.status.code == 200 &&
response.headerFields[.contentType] == "application/octet-stream"
else {
return (response, responseBody)
}
response.headerFields[.contentType] = "application/json"
return (response, responseBody)
}
}
43 changes: 43 additions & 0 deletions openapi/TestSwiftOrgClient/downloadswiftorgClient/Tool.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import OpenAPIURLSession
import Foundation
import Shared

enum DownloadSwiftOrgServerName: String {
case prod
}

@main
struct Tool {
static func main() async throws {
let serverURL: URL
if
let name = ProcessInfo.processInfo.environment["DOWNLOADSWIFTORG_SERVER_NAME"],
let serverName = DownloadSwiftOrgServerName(rawValue: name)
{
switch serverName {
case .prod:
serverURL = try Servers.Server1.url()
}
} else {
serverURL = try Servers.Server1.url()
}

print("Testing download.swift.org API at \(serverURL.absoluteString)...")

let client = Client(
serverURL: serverURL,
transport: URLSessionTransport(),
middlewares: [
Issue847WorkaroundMiddleware(),
]
)

let tests: [Test] = [
.init(
name: "listProposals",
work: { _ = try await client.listProposals().ok.body.json }
),
]
try await Tester.run(tests)
}
}
3 changes: 3 additions & 0 deletions openapi/TestSwiftOrgClient/openapi-generator-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
generate:
- types
- client
71 changes: 71 additions & 0 deletions openapi/TestSwiftOrgClient/swiftorgClient/Tool.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import OpenAPIURLSession
import Foundation
import Shared

enum SwiftOrgServerName: String {
case prod
case local
}

@main
struct Tool {
static func main() async throws {
let serverURL: URL
if
let name = ProcessInfo.processInfo.environment["SWIFTORG_SERVER_NAME"],
let serverName = SwiftOrgServerName(rawValue: name)
{
switch serverName {
case .prod:
serverURL = try Servers.Server1.url()
case .local:
serverURL = try Servers.Server2.url()
}
} else {
serverURL = try Servers.Server1.url()
}

print("Testing swift.org API at \(serverURL.absoluteString)...")

let client = Client(
serverURL: serverURL,
transport: URLSessionTransport()
)

var tests: [Test] = [
.init(
name: "listReleases",
work: { _ = try await client.listReleases().ok.body.json }
),
]
for branch in Components.Schemas.KnownSourceBranch.allCases {
for platform in Components.Schemas.KnownPlatformIdentifier.allCases {
tests.append(
.init(
name: "listDevToolchains(\(branch.rawValue), \(platform.rawValue))",
work: {
_ = try await client.listDevToolchains(.init(
path: .init(
branch: branch,
platform: platform
)
)).ok.body.json
}
)
)
}
}
for branch in Components.Schemas.KnownSourceBranch.allCases {
tests.append(
.init(
name: "listStaticSDKDevToolchains(\(branch.rawValue))",
work: {
_ = try await client.listStaticSDKDevToolchains(.init(path: .init(branch: branch))).ok.body.json
}
)
)
}

try await Tester.run(tests)
}
}
1 change: 1 addition & 0 deletions openapi/TestSwiftOrgClient/swiftorgClient/openapi.yaml
Loading