Skip to content

Commit da2d596

Browse files
Split client and server examples into separate packages (#311)
### Motivation The client and server examples currently live in a single Swift package. The majority of adopters will probably want to crib from an example of a client _or_ a server, and having them together runs the risk of them cargo-culting dependencies they don't need. The current setup also prevents us from updating the dependencies in the client example if the server transport releases are lagging. ### Modifications - Split the client and server examples into separate Swift packages. - Update the client example to the latest runtime and main generator. - Drop dependencies in server example to 0.2.0 family for now. ### Result Client example package is updated to latest dependencies and can build. ### Test Plan Tested that both packages can build locally. --------- Signed-off-by: Si Beaumont <[email protected]>
1 parent 58554e2 commit da2d596

File tree

10 files changed

+136
-18
lines changed

10 files changed

+136
-18
lines changed

Examples/GreetingService/Package.swift

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ let package = Package(
2020
.macOS(.v13)
2121
],
2222
dependencies: [
23-
.package(url: "https://github.com/apple/swift-openapi-generator", .upToNextMinor(from: "0.3.0")),
24-
.package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.3.0")),
25-
.package(url: "https://github.com/apple/swift-openapi-urlsession", .upToNextMinor(from: "0.3.0")),
26-
.package(url: "https://github.com/swift-server/swift-openapi-vapor", .upToNextMinor(from: "0.3.0")),
23+
.package(url: "https://github.com/apple/swift-openapi-generator", .upToNextMinor(from: "0.2.0")),
24+
.package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.2.0")),
25+
.package(url: "https://github.com/swift-server/swift-openapi-vapor", .upToNextMinor(from: "0.2.0")),
2726
.package(url: "https://github.com/vapor/vapor", from: "4.76.0"),
2827
],
2928
targets: [
@@ -38,18 +37,6 @@ let package = Package(
3837
.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")
3938
]
4039
),
41-
42-
.executableTarget(
43-
name: "GreetingServiceClient",
44-
dependencies: [
45-
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
46-
.product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"),
47-
],
48-
plugins: [
49-
.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")
50-
]
51-
),
52-
5340
.testTarget(
5441
name: "GreetingServiceMockTests",
5542
dependencies: [

Examples/GreetingService/Sources/GreetingService/openapi.yaml

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
openapi: '3.0.3'
2+
info:
3+
title: GreetingService
4+
version: 1.0.0
5+
servers:
6+
- url: https://example.com/api
7+
description: Example
8+
paths:
9+
/greet:
10+
get:
11+
operationId: getGreeting
12+
parameters:
13+
- name: name
14+
required: false
15+
in: query
16+
description: A name used in the returned greeting.
17+
schema:
18+
type: string
19+
responses:
20+
'200':
21+
description: A success response with a greeting.
22+
content:
23+
application/json:
24+
schema:
25+
$ref: '#/components/schemas/Greeting'
26+
components:
27+
schemas:
28+
Greeting:
29+
type: object
30+
properties:
31+
message:
32+
type: string
33+
required:
34+
- message

Examples/GreetingService/Sources/GreetingServiceClient/openapi.yaml

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.DS_Store
2+
.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
6+
DerivedData/
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.vscode
9+
/Package.resolved
10+
.ci/
11+
.docc-build/
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// swift-tools-version:5.8
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// This source file is part of the SwiftOpenAPIGenerator open source project
5+
//
6+
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
11+
//
12+
// SPDX-License-Identifier: Apache-2.0
13+
//
14+
//===----------------------------------------------------------------------===//
15+
import PackageDescription
16+
17+
let package = Package(
18+
name: "GreetingService",
19+
platforms: [
20+
.macOS(.v13)
21+
],
22+
dependencies: [
23+
// TODO: When swift-openapi-generator is tagged with 0.3.0, stop depending on main.
24+
// .package(url: "https://github.com/apple/swift-openapi-generator", .upToNextMinor(from: "0.3.0"),
25+
.package(url: "https://github.com/apple/swift-openapi-generator", branch: "main"),
26+
.package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.3.0")),
27+
.package(url: "https://github.com/apple/swift-openapi-urlsession", .upToNextMinor(from: "0.3.0")),
28+
],
29+
targets: [
30+
.executableTarget(
31+
name: "GreetingServiceClient",
32+
dependencies: [
33+
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
34+
.product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"),
35+
],
36+
plugins: [
37+
.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")
38+
]
39+
)
40+
]
41+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftOpenAPIGenerator open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
import GreetingService
15+
16+
// Mock operates on value types, and requires no concrete client or server transport.
17+
struct MockGreetingService: APIProtocol {
18+
func getGreeting(
19+
_ input: Operations.getGreeting.Input
20+
) async throws -> Operations.getGreeting.Output {
21+
let name = input.query.name ?? "<unknown>"
22+
return .ok(.init(body: .json(.init(message: "(mock) Hello, \(name)"))))
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftOpenAPIGenerator open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
import XCTest
15+
@testable import GreetingService
16+
17+
final class GreetingServiceMockTests: XCTestCase {
18+
func testWithMock() async throws {
19+
let client: APIProtocol = MockGreetingService()
20+
let response = try await client.getGreeting(.init(query: .init(name: "Jane")))
21+
XCTAssertEqual(response, .ok(.init(body: .json(.init(message: "(mock) Hello, Jane")))))
22+
}
23+
}

0 commit comments

Comments
 (0)