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..0cb8774d --- /dev/null +++ b/Examples/GreetingServiceClient/Package.swift @@ -0,0 +1,41 @@ +// 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: [ + // 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")), + ], + 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"))))) + } +}