From a4cfe830ebe160f9e769089e19ff3af149f5ba31 Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Tue, 3 Oct 2023 08:16:09 +0100 Subject: [PATCH 1/2] Split client and server examples into separate packages Signed-off-by: Si Beaumont --- Examples/GreetingService/Package.swift | 19 ++------- .../Sources/GreetingService/openapi.yaml | 35 +++++++++++++++- .../GreetingServiceClient/openapi.yaml | 1 - Examples/GreetingServiceClient/.gitignore | 11 +++++ Examples/GreetingServiceClient/Package.swift | 42 +++++++++++++++++++ .../GreetingServiceClient.swift | 0 .../openapi-generator-config.yaml | 0 .../GreetingServiceClient}/openapi.yaml | 0 .../MockGreetingService.swift | 24 +++++++++++ .../MockGreetingServiceTests.swift | 23 ++++++++++ 10 files changed, 137 insertions(+), 18 deletions(-) mode change 120000 => 100644 Examples/GreetingService/Sources/GreetingService/openapi.yaml delete mode 120000 Examples/GreetingService/Sources/GreetingServiceClient/openapi.yaml create mode 100644 Examples/GreetingServiceClient/.gitignore create mode 100644 Examples/GreetingServiceClient/Package.swift rename Examples/{GreetingService => GreetingServiceClient}/Sources/GreetingServiceClient/GreetingServiceClient.swift (100%) rename Examples/{GreetingService => GreetingServiceClient}/Sources/GreetingServiceClient/openapi-generator-config.yaml (100%) rename Examples/{GreetingService => GreetingServiceClient/Sources/GreetingServiceClient}/openapi.yaml (100%) create mode 100644 Examples/GreetingServiceClient/Tests/GreetingServiceMockTests/MockGreetingService.swift create mode 100644 Examples/GreetingServiceClient/Tests/GreetingServiceMockTests/MockGreetingServiceTests.swift diff --git a/Examples/GreetingService/Package.swift b/Examples/GreetingService/Package.swift index 50c5588c..3685a655 100644 --- a/Examples/GreetingService/Package.swift +++ b/Examples/GreetingService/Package.swift @@ -20,10 +20,9 @@ let package = Package( .macOS(.v13) ], dependencies: [ - .package(url: "https://github.com/apple/swift-openapi-generator", .upToNextMinor(from: "0.3.0")), - .package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.3.0")), - .package(url: "https://github.com/apple/swift-openapi-urlsession", .upToNextMinor(from: "0.3.0")), - .package(url: "https://github.com/swift-server/swift-openapi-vapor", .upToNextMinor(from: "0.3.0")), + .package(url: "https://github.com/apple/swift-openapi-generator", .upToNextMinor(from: "0.2.0")), + .package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.2.0")), + .package(url: "https://github.com/swift-server/swift-openapi-vapor", .upToNextMinor(from: "0.2.0")), .package(url: "https://github.com/vapor/vapor", from: "4.76.0"), ], targets: [ @@ -38,18 +37,6 @@ let package = Package( .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator") ] ), - - .executableTarget( - name: "GreetingServiceClient", - dependencies: [ - .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), - .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), - ], - plugins: [ - .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator") - ] - ), - .testTarget( name: "GreetingServiceMockTests", dependencies: [ diff --git a/Examples/GreetingService/Sources/GreetingService/openapi.yaml b/Examples/GreetingService/Sources/GreetingService/openapi.yaml deleted file mode 120000 index 0ec1ca49..00000000 --- a/Examples/GreetingService/Sources/GreetingService/openapi.yaml +++ /dev/null @@ -1 +0,0 @@ -../../openapi.yaml \ No newline at end of file diff --git a/Examples/GreetingService/Sources/GreetingService/openapi.yaml b/Examples/GreetingService/Sources/GreetingService/openapi.yaml new file mode 100644 index 00000000..c670a499 --- /dev/null +++ b/Examples/GreetingService/Sources/GreetingService/openapi.yaml @@ -0,0 +1,34 @@ +openapi: '3.0.3' +info: + title: GreetingService + version: 1.0.0 +servers: + - url: https://example.com/api + description: Example +paths: + /greet: + get: + operationId: getGreeting + parameters: + - name: name + required: false + in: query + description: A 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 + properties: + message: + type: string + required: + - message diff --git a/Examples/GreetingService/Sources/GreetingServiceClient/openapi.yaml b/Examples/GreetingService/Sources/GreetingServiceClient/openapi.yaml deleted file mode 120000 index 0ec1ca49..00000000 --- a/Examples/GreetingService/Sources/GreetingServiceClient/openapi.yaml +++ /dev/null @@ -1 +0,0 @@ -../../openapi.yaml \ No newline at end of file diff --git a/Examples/GreetingServiceClient/.gitignore b/Examples/GreetingServiceClient/.gitignore new file mode 100644 index 00000000..f6f5465e --- /dev/null +++ b/Examples/GreetingServiceClient/.gitignore @@ -0,0 +1,11 @@ +.DS_Store +.build +/Packages +/*.xcodeproj +xcuserdata/ +DerivedData/ +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.vscode +/Package.resolved +.ci/ +.docc-build/ diff --git a/Examples/GreetingServiceClient/Package.swift b/Examples/GreetingServiceClient/Package.swift new file mode 100644 index 00000000..53a09bfd --- /dev/null +++ b/Examples/GreetingServiceClient/Package.swift @@ -0,0 +1,42 @@ +// swift-tools-version:5.8 +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftOpenAPIGenerator open source project +// +// Copyright (c) 2023 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: "GreetingService", + platforms: [ + .macOS(.v13) + ], + dependencies: [ + .package( + url: "https://github.com/apple/swift-openapi-generator", /* .upToNextMinor(from: "0.3.0") */ + branch: "main" + ), + .package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.3.0")), + .package(url: "https://github.com/apple/swift-openapi-urlsession", .upToNextMinor(from: "0.3.0")), + ], + targets: [ + .executableTarget( + name: "GreetingServiceClient", + dependencies: [ + .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), + .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), + ], + plugins: [ + .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator") + ] + ) + ] +) diff --git a/Examples/GreetingService/Sources/GreetingServiceClient/GreetingServiceClient.swift b/Examples/GreetingServiceClient/Sources/GreetingServiceClient/GreetingServiceClient.swift similarity index 100% rename from Examples/GreetingService/Sources/GreetingServiceClient/GreetingServiceClient.swift rename to Examples/GreetingServiceClient/Sources/GreetingServiceClient/GreetingServiceClient.swift diff --git a/Examples/GreetingService/Sources/GreetingServiceClient/openapi-generator-config.yaml b/Examples/GreetingServiceClient/Sources/GreetingServiceClient/openapi-generator-config.yaml similarity index 100% rename from Examples/GreetingService/Sources/GreetingServiceClient/openapi-generator-config.yaml rename to Examples/GreetingServiceClient/Sources/GreetingServiceClient/openapi-generator-config.yaml diff --git a/Examples/GreetingService/openapi.yaml b/Examples/GreetingServiceClient/Sources/GreetingServiceClient/openapi.yaml similarity index 100% rename from Examples/GreetingService/openapi.yaml rename to Examples/GreetingServiceClient/Sources/GreetingServiceClient/openapi.yaml diff --git a/Examples/GreetingServiceClient/Tests/GreetingServiceMockTests/MockGreetingService.swift b/Examples/GreetingServiceClient/Tests/GreetingServiceMockTests/MockGreetingService.swift new file mode 100644 index 00000000..a5d6761b --- /dev/null +++ b/Examples/GreetingServiceClient/Tests/GreetingServiceMockTests/MockGreetingService.swift @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftOpenAPIGenerator open source project +// +// Copyright (c) 2023 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 GreetingService + +// Mock operates on value types, and requires no concrete client or server transport. +struct MockGreetingService: APIProtocol { + func getGreeting( + _ input: Operations.getGreeting.Input + ) async throws -> Operations.getGreeting.Output { + let name = input.query.name ?? "" + return .ok(.init(body: .json(.init(message: "(mock) Hello, \(name)")))) + } +} diff --git a/Examples/GreetingServiceClient/Tests/GreetingServiceMockTests/MockGreetingServiceTests.swift b/Examples/GreetingServiceClient/Tests/GreetingServiceMockTests/MockGreetingServiceTests.swift new file mode 100644 index 00000000..545e7de1 --- /dev/null +++ b/Examples/GreetingServiceClient/Tests/GreetingServiceMockTests/MockGreetingServiceTests.swift @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftOpenAPIGenerator open source project +// +// Copyright (c) 2023 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 XCTest +@testable import GreetingService + +final class GreetingServiceMockTests: XCTestCase { + func testWithMock() async throws { + let client: APIProtocol = MockGreetingService() + let response = try await client.getGreeting(.init(query: .init(name: "Jane"))) + XCTAssertEqual(response, .ok(.init(body: .json(.init(message: "(mock) Hello, Jane"))))) + } +} From ab34fa6ed28775279011c5c9ba35828a4e3f7cad Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Tue, 3 Oct 2023 08:27:55 +0100 Subject: [PATCH 2/2] swift-format --- Examples/GreetingServiceClient/Package.swift | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Examples/GreetingServiceClient/Package.swift b/Examples/GreetingServiceClient/Package.swift index 53a09bfd..0cb8774d 100644 --- a/Examples/GreetingServiceClient/Package.swift +++ b/Examples/GreetingServiceClient/Package.swift @@ -20,10 +20,9 @@ let package = Package( .macOS(.v13) ], dependencies: [ - .package( - url: "https://github.com/apple/swift-openapi-generator", /* .upToNextMinor(from: "0.3.0") */ - branch: "main" - ), + // TODO: When swift-openapi-generator is tagged with 0.3.0, stop depending on main. + // .package(url: "https://github.com/apple/swift-openapi-generator", .upToNextMinor(from: "0.3.0"), + .package(url: "https://github.com/apple/swift-openapi-generator", branch: "main"), .package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.3.0")), .package(url: "https://github.com/apple/swift-openapi-urlsession", .upToNextMinor(from: "0.3.0")), ],