Skip to content

Commit 057c661

Browse files
simonjbeaumontczechboy0glbrnttGus Cairomariosangiorgio
committed
Initial commit
Co-authored-by: Honza Dvorsky <[email protected]> Co-authored-by: Si Beaumont <[email protected]> Co-authored-by: George Barnett <[email protected]> Co-authored-by: Gus Cairo <[email protected]> Co-authored-by: Mario Sangiorgio <[email protected]>
0 parents  commit 057c661

File tree

234 files changed

+23390
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

234 files changed

+23390
-0
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### Motivation
2+
3+
_[Explain here the context, and why you're making that change. What is the problem you're trying to solve.]_
4+
5+
### Modifications
6+
7+
_[Describe the modifications you've made.]_
8+
9+
### Result
10+
11+
_[After your change, what will change.]_
12+
13+
### Test Plan
14+
15+
_[Describe the steps you took, or will take, to qualify the change - such as adjusting tests and manual testing.]_

.gitignore

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/

.spi.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: 1
2+
builder:
3+
configs:
4+
- documentation_targets:
5+
- swift-openapi-generator

.swift-format

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"fileScopedDeclarationPrivacy" : {
3+
"accessLevel" : "private"
4+
},
5+
"indentation" : {
6+
"spaces" : 4
7+
},
8+
"indentConditionalCompilationBlocks" : false,
9+
"indentSwitchCaseLabels" : false,
10+
"lineBreakAroundMultilineExpressionChainComponents" : true,
11+
"lineBreakBeforeControlFlowKeywords" : false,
12+
"lineBreakBeforeEachArgument" : true,
13+
"lineBreakBeforeEachGenericRequirement" : true,
14+
"lineLength" : 120,
15+
"maximumBlankLines" : 1,
16+
"prioritizeKeepingFunctionOutputTogether" : false,
17+
"respectsExistingLineBreaks" : true,
18+
"rules" : {
19+
"AllPublicDeclarationsHaveDocumentation" : false,
20+
"AlwaysUseLowerCamelCase" : false,
21+
"AmbiguousTrailingClosureOverload" : true,
22+
"BeginDocumentationCommentWithOneLineSummary" : false,
23+
"DoNotUseSemicolons" : true,
24+
"DontRepeatTypeInStaticProperties" : false,
25+
"FileScopedDeclarationPrivacy" : true,
26+
"FullyIndirectEnum" : true,
27+
"GroupNumericLiterals" : true,
28+
"IdentifiersMustBeASCII" : true,
29+
"NeverForceUnwrap" : false,
30+
"NeverUseForceTry" : false,
31+
"NeverUseImplicitlyUnwrappedOptionals" : false,
32+
"NoAccessLevelOnExtensionDeclaration" : false,
33+
"NoAssignmentInExpressions" : true,
34+
"NoBlockComments" : true,
35+
"NoCasesWithOnlyFallthrough" : true,
36+
"NoEmptyTrailingClosureParentheses" : true,
37+
"NoLabelsInCasePatterns" : false,
38+
"NoLeadingUnderscores" : false,
39+
"NoParensAroundConditions" : true,
40+
"NoVoidReturnOnFunctionSignature" : true,
41+
"OneCasePerLine" : true,
42+
"OneVariableDeclarationPerLine" : true,
43+
"OnlyOneTrailingClosureArgument" : true,
44+
"OrderedImports" : false,
45+
"ReturnVoidInsteadOfEmptyTuple" : true,
46+
"UseEarlyExits" : true,
47+
"UseLetInEveryBoundCaseVariable" : false,
48+
"UseShorthandTypeNames" : true,
49+
"UseSingleLinePropertyGetter" : false,
50+
"UseSynthesizedInitializer" : true,
51+
"UseTripleSlashForDocumentationComments" : true,
52+
"UseWhereClausesInForLoops" : false,
53+
"ValidateDocumentationComments" : false
54+
},
55+
"spacesAroundRangeFormationOperators" : false,
56+
"tabWidth" : 8,
57+
"version" : 1
58+
}

CODE_OF_CONDUCT.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Code of Conduct
2+
3+
The code of conduct for this project can be found at https://swift.org/code-of-conduct.
4+
5+
<!-- Copyright (c) 2023 Apple Inc and the Swift Project authors. All Rights Reserved. -->

CONTRIBUTING.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
## Legal
2+
3+
By submitting a pull request, you represent that you have the right to license
4+
your contribution to Apple and the community, and agree by submitting the patch
5+
that your contributions are licensed under the Apache 2.0 license (see
6+
`LICENSE.txt`).
7+
8+
## How to submit a bug report
9+
10+
Please ensure to specify the following:
11+
12+
* Commit hash
13+
* Contextual information (e.g. what you were trying to achieve with swift-openapi-generator)
14+
* Simplest possible steps to reproduce
15+
* More complex the steps are, lower the priority will be.
16+
* A pull request with failing test case is preferred, but it's just fine to paste the test case into the issue description.
17+
* Anything that might be relevant in your opinion, such as:
18+
* Swift version or the output of `swift --version`
19+
* OS version and the output of `uname -a`
20+
* Network configuration
21+
22+
### Example
23+
24+
```
25+
Commit hash: b17a8a9f0f814c01a56977680cb68d8a779c951f
26+
27+
Context:
28+
While testing my application that uses with swift-openapi-generator, I noticed that ...
29+
30+
Steps to reproduce:
31+
1. ...
32+
2. ...
33+
3. ...
34+
4. ...
35+
36+
$ swift --version
37+
Swift version 4.0.2 (swift-4.0.2-RELEASE)
38+
Target: x86_64-unknown-linux-gnu
39+
40+
Operating system: Ubuntu Linux 16.04 64-bit
41+
42+
$ uname -a
43+
Linux beefy.machine 4.4.0-101-generic #124-Ubuntu SMP Fri Nov 10 18:29:59 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
44+
45+
My system has IPv6 disabled.
46+
```
47+
48+
## Writing a Patch
49+
50+
A good patch is:
51+
52+
1. Concise, and contains as few changes as needed to achieve the end result.
53+
2. Tested, ensuring that any tests provided failed before the patch and pass after it.
54+
3. Documented, adding API documentation as needed to cover new functions and properties.
55+
4. Accompanied by a great commit message, using our commit message template.
56+
57+
### Run `./scripts/soundness.sh`
58+
59+
The scripts directory contains a [soundness.sh script](https://github.com/apple/swift-openapi-generator/blob/main/scripts/soundness.sh)
60+
that enforces additional checks, like license headers and formatting style.
61+
62+
Please make sure to `./scripts/soundness.sh` before pushing a change upstream, otherwise it is likely the PR validation will fail
63+
on minor changes such as a missing `self.` or similar formatting issues.
64+
65+
For frequent contributors, we recommend adding the script as a [git pre-push hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks), which you can do via executing the following command in the project root directory:
66+
67+
```bash
68+
cat << EOF > .git/hooks/pre-push
69+
70+
if [[ -f "scripts/soundness.sh" ]]; then
71+
scripts/soundness.sh
72+
fi
73+
EOF
74+
```
75+
76+
Which makes the script execute, and only allow the `git push` to complete if the check has passed.
77+
78+
In the case of formatting issues, you can then `git add` the formatting changes, and attempt the push again.
79+
80+
## How to contribute your work
81+
82+
Please open a pull request at https://github.com/apple/swift-openapi-generator. Make sure the CI passes, and then wait for code review.

CONTRIBUTORS.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
For the purpose of tracking copyright, this is the list of individuals and
2+
organizations who have contributed source code to SwiftOpenAPIGenerator.
3+
4+
For employees of an organization/company where the copyright of work done
5+
by employees of that company is held by the company itself, only the company
6+
needs to be listed here.
7+
8+
## COPYRIGHT HOLDERS
9+
10+
- Apple Inc. (all contributors with '@apple.com')
11+
12+
### Contributors
13+
14+
- George Barnett <[email protected]>
15+
- Gus Cairo <[email protected]>
16+
- Honza Dvorsky <[email protected]>
17+
- Mario Sangiorgio <[email protected]>
18+
- Si Beaumont <[email protected]>
19+
20+
**Updating this list**
21+
22+
Please do not edit this file manually. It is generated using `./scripts/generate-contributors-list.sh`. If a name is misspelled or appearing multiple times: add an entry in `./.mailmap`

Examples/GreetingService/.gitignore

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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
.package(url: "https://github.com/apple/swift-openapi-generator", .upToNextMinor(from: "0.1.0")),
24+
.package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.1.0")),
25+
.package(url: "https://github.com/apple/swift-openapi-urlsession", .upToNextMinor(from: "0.1.0")),
26+
.package(url: "https://github.com/swift-server/swift-openapi-vapor", .upToNextMinor(from: "0.1.0")),
27+
.package(url: "https://github.com/vapor/vapor", from: "4.76.0"),
28+
],
29+
targets: [
30+
.executableTarget(
31+
name: "GreetingService",
32+
dependencies: [
33+
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
34+
.product(name: "OpenAPIVapor", package: "swift-openapi-vapor"),
35+
.product(name: "Vapor", package: "vapor"),
36+
],
37+
plugins: [
38+
.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")
39+
]
40+
),
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+
53+
.testTarget(
54+
name: "GreetingServiceMockTests",
55+
dependencies: [
56+
"GreetingService",
57+
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
58+
]
59+
),
60+
]
61+
)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 OpenAPIRuntime
15+
import OpenAPIVapor
16+
import Vapor
17+
18+
// User provides a type that adopts `APIProtocol` (a generated protocol).
19+
struct Handler: APIProtocol {
20+
21+
// This function is a protocol requirement, derived from the operation in the OpenAPI document.
22+
func getGreeting(
23+
_ input: Operations.getGreeting.Input
24+
) async throws -> Operations.getGreeting.Output {
25+
// Extract the query parameter from the input, using generated type-safe property.
26+
let name = input.query.name ?? "Stranger"
27+
28+
// Return a OK response (HTTP status 200) with a JSON body, using a type-safe output.
29+
return .ok(.init(body: .json(.init(message: "Hello, \(name)!"))))
30+
}
31+
}
32+
33+
@main
34+
struct Main {
35+
public static func main() throws {
36+
// Create a Vapor application.
37+
let app = Vapor.Application()
38+
39+
// Create a ServerTransport using the Vapor application.
40+
let transport = VaporTransport(routesBuilder: app)
41+
42+
// Create an instance of the handler type that conforms the generated APIProtocol.
43+
let handler = Handler()
44+
45+
// Call the generated protocol function on the handler to configure the Vapor application.
46+
try handler.registerHandlers(on: transport, serverURL: Servers.server1())
47+
48+
// Start the Vapor application, in the same way as if it was manually configured.
49+
try app.run()
50+
}
51+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
generate:
2+
- types
3+
- server
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../openapi.yaml
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 OpenAPIRuntime
15+
import OpenAPIURLSession
16+
17+
@main
18+
struct GreetingServiceClient {
19+
20+
static func main() async throws {
21+
// Create an instance of the generated client type.
22+
let client: APIProtocol = Client(
23+
// Server.server1() is generated, derived from the server URL in the OpenAPI document.
24+
serverURL: try Servers.server1(),
25+
// URLSessionTransport conforms to ClientTransport and is provided by a separate package.
26+
transport: URLSessionTransport()
27+
)
28+
29+
// Make an API call using generated type-safe Operations.getGreeting.Input.
30+
let response = try await client.getGreeting(.init(query: .init(name: "Jane")))
31+
32+
// Operations.getGreeting.Output is an enum that models all the documented responses.
33+
switch response {
34+
case .ok(let response):
35+
// The response body is also an enum value that models all the documented content-types.
36+
switch response.body {
37+
case .json(let greeting):
38+
// The associated enum value is a generated value type derived from the OpenAPI document.
39+
print(greeting.message)
40+
}
41+
// In the event the server responds with something that isn't in its documented API.
42+
case .undocumented(statusCode: let statusCode, let undocumentedPayload):
43+
print("Undocumented response \(statusCode) from server: \(undocumentedPayload).")
44+
}
45+
}
46+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
generate:
2+
- types
3+
- client
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../openapi.yaml

0 commit comments

Comments
 (0)