-
Notifications
You must be signed in to change notification settings - Fork 133
Add example package using shared types in client and server #592
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
Merged
simonjbeaumont
merged 3 commits into
apple:main
from
simonjbeaumont:sb/shared-types-example
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.DS_Store | ||
.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.vscode | ||
/Package.resolved | ||
.ci/ | ||
.docc-build/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// swift-tools-version:5.9 | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "shared-types-client-server-example", | ||
platforms: [.macOS(.v13)], | ||
products: [ | ||
.executable(name: "hello-world-client", targets: ["Client"]), | ||
.executable(name: "hello-world-server", targets: ["Server"]), | ||
], | ||
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"), | ||
.package(url: "https://github.com/swift-server/swift-openapi-hummingbird", from: "1.0.0"), | ||
], | ||
targets: [ | ||
.target( | ||
name: "Types", | ||
dependencies: [.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")], | ||
plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")] | ||
), | ||
.executableTarget( | ||
name: "Client", | ||
dependencies: [ | ||
"Types", .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), | ||
.product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), | ||
], | ||
plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")] | ||
), | ||
.executableTarget( | ||
name: "Server", | ||
dependencies: [ | ||
"Types", .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), | ||
.product(name: "OpenAPIHummingbird", package: "swift-openapi-hummingbird"), | ||
], | ||
plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")] | ||
), | ||
] | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Common types between client and server modules | ||
|
||
An example project using [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). | ||
|
||
> **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. | ||
|
||
## Overview | ||
|
||
This example shows how you can structure a Swift package to share the types | ||
from an OpenAPI document between a client and server module by having a common | ||
target that runs the generator in `types` mode only. | ||
|
||
This allows you to write extensions or other helper functions that use these | ||
types and use them in both the client and server code. | ||
|
||
## Usage | ||
|
||
Build and run the server using: | ||
|
||
```console | ||
% swift run hello-world-server | ||
Build complete! | ||
... | ||
info HummingBird : [HummingbirdCore] Server started and listening on 127.0.0.1:8080 | ||
``` | ||
|
||
Then, in another terminal window, run the client: | ||
|
||
```console | ||
% swift run hello-world-client | ||
Build complete! | ||
+––––––––––––––––––+ | ||
|+––––––––––––––––+| | ||
||Hello, Stranger!|| | ||
|+––––––––––––––––+| | ||
+––––––––––––––––––+ | ||
``` | ||
|
||
Note how the message is boxed twice: once by the server and once by the client, | ||
both using an extension on a shared type, defined in the `Types` module. |
24 changes: 24 additions & 0 deletions
24
Examples/shared-types-client-server-example/Sources/Client/HelloWorldURLSessionClient.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
import OpenAPIRuntime | ||
import OpenAPIURLSession | ||
import Foundation | ||
|
||
@main struct HelloWorldURLSessionClient { | ||
static func main() async throws { | ||
let client = Client(serverURL: URL(string: "http://localhost:8080/api")!, transport: URLSessionTransport()) | ||
let response = try await client.getGreeting() | ||
print(try response.ok.body.json.boxed().message) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
Examples/shared-types-client-server-example/Sources/Client/openapi-generator-config.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
generate: | ||
- client | ||
accessModifier: internal | ||
additionalImports: | ||
- Types |
1 change: 1 addition & 0 deletions
1
Examples/shared-types-client-server-example/Sources/Client/openapi.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../openapi.yaml |
36 changes: 36 additions & 0 deletions
36
Examples/shared-types-client-server-example/Sources/Server/HelloWorldHummingbirdServer.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
import OpenAPIRuntime | ||
import OpenAPIHummingbird | ||
import Hummingbird | ||
import Foundation | ||
import Types | ||
|
||
struct Handler: APIProtocol { | ||
func getGreeting(_ input: Operations.getGreeting.Input) async throws -> Operations.getGreeting.Output { | ||
let name = input.query.name ?? "Stranger" | ||
let message = Components.Schemas.Greeting(message: "Hello, \(name)!") | ||
return .ok(.init(body: .json(message.boxed()))) | ||
} | ||
} | ||
|
||
@main struct HelloWorldHummingbirdServer { | ||
static func main() async throws { | ||
let app = Hummingbird.HBApplication() | ||
let transport = HBOpenAPITransport(app) | ||
let handler = Handler() | ||
try handler.registerHandlers(on: transport, serverURL: URL(string: "/api")!) | ||
try await app.asyncRun() | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
Examples/shared-types-client-server-example/Sources/Server/openapi-generator-config.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
generate: | ||
- server | ||
accessModifier: internal | ||
additionalImports: | ||
- Types |
1 change: 1 addition & 0 deletions
1
Examples/shared-types-client-server-example/Sources/Server/openapi.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../openapi.yaml |
41 changes: 41 additions & 0 deletions
41
Examples/shared-types-client-server-example/Sources/Types/Extensions.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
extension Components.Schemas.Greeting { | ||
package func boxed(maxBoxWidth: Int = 80) -> Self { | ||
// Reflow the text. | ||
let maxTextLength = maxBoxWidth - 4 | ||
var reflowedLines: [Substring] = [] | ||
for var line in message.split(whereSeparator: \.isNewline) { | ||
while !line.isEmpty { | ||
let prefix = line.prefix(maxTextLength) | ||
reflowedLines.append(prefix) | ||
line = line.dropFirst(prefix.count) | ||
} | ||
} | ||
|
||
// Determine the box size (might be smaller than max). | ||
let longestLineCount = reflowedLines.map(\.count).max()! | ||
let horizontalEdge = "+\(String(repeating: "–", count: longestLineCount))+" | ||
|
||
var boxedMessageLines: [String] = [] | ||
boxedMessageLines.reserveCapacity(reflowedLines.count + 2) | ||
boxedMessageLines.append(horizontalEdge) | ||
for line in reflowedLines { | ||
boxedMessageLines.append("|\(line.padding(toLength: longestLineCount, withPad: " ", startingAt: 0))|") | ||
} | ||
boxedMessageLines.append(horizontalEdge) | ||
return Self(message: boxedMessageLines.joined(separator: "\n")) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Examples/shared-types-client-server-example/Sources/Types/openapi-generator-config.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
generate: | ||
- types | ||
accessModifier: package |
1 change: 1 addition & 0 deletions
1
Examples/shared-types-client-server-example/Sources/Types/openapi.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../openapi.yaml |
36 changes: 36 additions & 0 deletions
36
Examples/shared-types-client-server-example/Sources/openapi.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
openapi: '3.1.0' | ||
info: | ||
title: GreetingService | ||
version: 1.0.0 | ||
servers: | ||
- url: https://example.com/api | ||
description: Example service deployment. | ||
paths: | ||
/greet: | ||
get: | ||
operationId: getGreeting | ||
parameters: | ||
- name: name | ||
required: false | ||
in: query | ||
description: The name used in the returned greeting. | ||
schema: | ||
type: string | ||
responses: | ||
'200': | ||
description: A success response with a greeting. | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Greeting' | ||
components: | ||
schemas: | ||
Greeting: | ||
type: object | ||
description: A value with the greeting contents. | ||
properties: | ||
message: | ||
type: string | ||
description: The string representation of the greeting. | ||
required: | ||
- message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.