Skip to content

Commit cd2be00

Browse files
Update and polish for README and DocC landing page (#479)
### Motivation Ahead of 1.0, we want to give the README and DocC landing page a refresh. ### Modifications Updates to the README and DocC landing page. ### Result Updates to the README and DocC landing page. ### Test Plan Review.
1 parent 1234e30 commit cd2be00

File tree

2 files changed

+254
-55
lines changed

2 files changed

+254
-55
lines changed

README.md

Lines changed: 134 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,170 @@ Generate Swift client and server code from an OpenAPI document.
88

99
## Overview
1010

11-
[OpenAPI][openapi] is an open specification for documenting HTTP APIs.
11+
[OpenAPI][openapi] is a specification for documenting HTTP services. An OpenAPI document is written in either YAML or JSON, and can be read by tools to help automate workflows, such as generating the necessary code to send and receive HTTP requests.
1212

1313
Swift OpenAPI Generator is a Swift package plugin that can generate the ceremony code required to make API calls, or implement API servers.
1414

15-
## Repository organization
15+
The code is generated at build-time, so it's always in sync with the OpenAPI document and doesn't need to be committed to your source repository.
1616

17-
The Swift OpenAPI Generator project is split across multiple repositories to enable extensibility and minimize dependencies in your project.
17+
## Features
18+
19+
- Works with OpenAPI Specification versions 3.0 and 3.1.
20+
- Streaming request and response bodies enabling use cases such as JSON event streams, and large payloads without buffering.
21+
- Support for JSON, multipart, URL-encoded form, base64, plain text, and raw bytes, represented as value types with type-safe properties.
22+
- Client, server, and middleware abstractions, decoupling the generated code from the HTTP client library and web framework.
23+
24+
To see these features in action, check out the list of [example projects][examples-generator].
25+
26+
## Usage
27+
28+
Swift OpenAPI Generator can be used to generate API clients and server stubs.
29+
30+
Below you can see some example code, or you can follow one of the [step-by-step tutorials][tutorials-generator].
31+
32+
### Using a generated API client
33+
34+
The generated `Client` type provides a method for each HTTP operation defined in the OpenAPI document[^example-openapi-yaml] and can be used with any HTTP library that provides an implementation of `ClientTransport`.
35+
36+
```swift
37+
import OpenAPIURLSession
38+
import Foundation
39+
40+
let client = Client(
41+
serverURL: URL(string: "http://localhost:8080/api")!,
42+
transport: URLSessionTransport()
43+
)
44+
let response = try await client.getGreeting()
45+
print(try response.ok.body.json.message)
46+
```
1847

19-
**swift-openapi-generator** ([source][repo-generator], [docs][docs-generator]) provides the plugin.
48+
### Using generated API server stubs
2049

21-
**swift-openapi-runtime** ([source][repo-runtime], [docs][docs-runtime]) provides a library with common types and abstractions used by the generated code.
50+
To implement a server, define a type that conforms to the generated `APIProtocol`, providing a method for each HTTP operation defined in the OpenAPI document[^example-openapi-yaml].
2251

23-
> See the generator in action in [Meet Swift OpenAPI Generator](https://developer.apple.com/wwdc23/10171) from WWDC23.
52+
The server can be used with any web framework that provides an implementation of `ServerTransport`, which allows you to register your API handlers with the HTTP server.
2453

25-
Choose one of the transports listed below, or create your own by adopting the `ClientTransport` or `ServerTransport` protocol:
54+
```swift
55+
import OpenAPIRuntime
56+
import OpenAPIVapor
57+
import Vapor
58+
59+
struct Handler: APIProtocol {
60+
func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output {
61+
let name = input.query.name ?? "Stranger"
62+
return .ok(.init(body: .json(.init(message: "Hello, \(name)!"))))
63+
}
64+
}
65+
66+
@main struct HelloWorldVaporServer {
67+
static func main() async throws {
68+
let app = Vapor.Application()
69+
let transport = VaporTransport(routesBuilder: app)
70+
let handler = Handler()
71+
try handler.registerHandlers(on: transport, serverURL: URL(string: "/api")!)
72+
try await app.execute()
73+
}
74+
}
75+
```
76+
77+
## Package ecosystem
78+
79+
The Swift OpenAPI Generator project is split across multiple repositories to enable extensibility and minimize dependencies in your project.
80+
81+
| Repository | Description |
82+
| ---------- | ----------- |
83+
| [apple/swift-openapi-generator][repo-generator] | Swift package plugin and CLI |
84+
| [apple/swift-openapi-runtime][repo-runtime] | Runtime library used by the generated code |
85+
| [apple/swift-openapi-urlsession][repo-urlsession] | `ClientTransport` using [URLSession][urlsession] |
86+
| [swift-server/swift-openapi-async-http-client][repo-ahc] | `ClientTransport` using [AsyncHTTPClient][ahc] |
87+
| [swift-server/swift-openapi-vapor][repo-vapor] | `ServerTransport` using [Vapor][vapor] |
88+
| [swift-server/swift-openapi-hummingbird][repo-hummingbird] | `ServerTransport` using [Hummingbird][hummingbird] |
2689

27-
| Repository | Type | Description |
28-
| ---------- | ---- | ----------- |
29-
| [apple/swift-openapi-urlsession][repo-urlsession] | Client | Uses `URLSession` from [Foundation][foundation]. |
30-
| [swift-server/swift-openapi-async-http-client][repo-ahc] | Client | Uses `HTTPClient` from [AsyncHTTPClient][ahc]. |
31-
| [swift-server/swift-openapi-vapor][repo-vapor] | Server | Uses [Vapor][vapor]. |
32-
| [swift-server/swift-openapi-hummingbird][repo-hummingbird] | Server | Uses [Hummingbird][hummingbird]. |
3390

3491
## Requirements and supported features
3592

3693
| Generator versions | Supported OpenAPI versions | Minimum Swift version |
37-
| -------- | ------- | ----- |
38-
| `1.0.0` ... `main` | 3.0, 3.1 | 5.9 |
94+
| ------------------ | -------------------------- | --------------------- |
95+
| `1.0.0` ... `main` | 3.0, 3.1 | 5.9 |
96+
97+
See also [Supported OpenAPI features][supported-openapi-features].
3998

4099
### Supported platforms and minimum versions
41100

42101
The generator is used during development and is supported on macOS and Linux.
43102

44-
The generated code, runtime library, and transports are supported on more platforms, listed below.
103+
The generated code, runtime library, and transports are supported on more
104+
platforms, listed below.
105+
106+
| Component | macOS | Linux | iOS | tvOS | watchOS | visionOS |
107+
| ----------------------------------: | :--- | :--- | :- | :-- | :----- | :------ |
108+
| Generator plugin and CLI | ✅ 10.15+ || ✖️ | ✖️ | ✖️ | ✖️ |
109+
| Generated code and runtime library | ✅ 10.15+ || ✅ 13+ | ✅ 13+ | ✅ 6+ | ✅ 1+ |
110+
111+
## Documentation and example projects
45112

46-
| Component | macOS | Linux | iOS | tvOS | watchOS | visionOS |
47-
| -: | :-: | :-: | :-: | :-: | :-: | :-: |
48-
| Generator plugin and CLI | ✅ 10.15+ ||||||
49-
| Generated code, runtime, transports | ✅ 10.15+ || ✅ 13+ | ✅ 13+ | ✅ 6+ | ✅ 1+ |
113+
To get started, check out the [documentation][docs-generator], which contains
114+
[step-by-step tutorials][tutorials-generator].
50115

51-
## Documentation
116+
You can also experiment with [example projects][examples-generator] that use
117+
Swift OpenAPI Generator and integrate with other packages in the ecosystem.
52118

53-
To get started, check out the full [documentation][docs-generator], which contains step-by-step tutorials!
119+
Or if you prefer to watch a video, check out [Meet Swift OpenAPI
120+
Generator](https://developer.apple.com/wwdc23/10171) from WWDC23.
54121

55122
[openapi]: https://openapis.org
56123
[repo-generator]: https://github.com/apple/swift-openapi-generator
57124
[docs-generator]: https://swiftpackageindex.com/apple/swift-openapi-generator/documentation
125+
[tutorials-generator]: https://swiftpackageindex.com/apple/swift-openapi-generator/tutorials/swift-openapi-generator
126+
[supported-openapi-features]: https://swiftpackageindex.com/apple/swift-openapi-generator/documentation/swift-openapi-generator/supported-openapi-features
127+
[examples-generator]: https://github.com/apple/swift-openapi-generator/blob/main/Examples/README.md
58128
[repo-runtime]: https://github.com/apple/swift-openapi-runtime
59-
[docs-runtime]: https://swiftpackageindex.com/apple/swift-openapi-runtime/documentation
60129
[repo-urlsession]: https://github.com/apple/swift-openapi-urlsession
61-
[foundation]: https://developer.apple.com/documentation/foundation
130+
[urlsession]: https://developer.apple.com/documentation/foundation/urlsession
62131
[repo-ahc]: https://github.com/swift-server/swift-openapi-async-http-client
63132
[ahc]: https://github.com/swift-server/async-http-client
64133
[repo-vapor]: https://github.com/swift-server/swift-openapi-vapor
65134
[vapor]: https://github.com/vapor/vapor
66135
[repo-hummingbird]: https://github.com/swift-server/swift-openapi-hummingbird
67136
[hummingbird]: https://github.com/hummingbird-project/hummingbird
137+
[^example-openapi-yaml]: <details><summary>Example OpenAPI document (click to expand)</summary>
138+
139+
```yaml
140+
openapi: '3.1.0'
141+
info:
142+
title: GreetingService
143+
version: 1.0.0
144+
servers:
145+
- url: https://example.com/api
146+
description: Example service deployment.
147+
paths:
148+
/greet:
149+
get:
150+
operationId: getGreeting
151+
parameters:
152+
- name: name
153+
required: false
154+
in: query
155+
description: The name used in the returned greeting.
156+
schema:
157+
type: string
158+
responses:
159+
'200':
160+
description: A success response with a greeting.
161+
content:
162+
application/json:
163+
schema:
164+
$ref: '#/components/schemas/Greeting'
165+
components:
166+
schemas:
167+
Greeting:
168+
type: object
169+
description: A value with the greeting contents.
170+
properties:
171+
message:
172+
type: string
173+
description: The string representation of the greeting.
174+
required:
175+
- message
176+
```
177+
</details>

0 commit comments

Comments
 (0)