Skip to content

Commit 4db26e7

Browse files
committed
(no commit message provided)
1 parent edafaeb commit 4db26e7

File tree

3 files changed

+42
-36
lines changed

3 files changed

+42
-36
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
.idea
66
.DS_Store
77
.swiftpm/xcode/xcuserdata/danil.xcuserdatad/xcschemes/xcschememanagement.plist
8+
.aider*
9+
.env

Example/Sources/App/Controllers/OpenAPIController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct OpenAPIController: RouteCollection {
88

99
func boot(routes: RoutesBuilder) throws {
1010

11-
// generate OpenAPI documentation
11+
// Generate an OpenAPI documentation JSON
1212
routes.get("swagger", "swagger.json") { req in
1313
req.application.routes.openAPI(
1414
info: InfoObject(
@@ -32,6 +32,7 @@ struct OpenAPIController: RouteCollection {
3232
}
3333
.excludeFromOpenAPI()
3434

35+
// Generate a Stoplight page with the OpenAPI documentation
3536
routes.stoplightDocumentation(
3637
"stoplight",
3738
openAPIPath: "/swagger/swagger.json"

README.md

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,8 @@ The library is based on [SwiftOpenAPI](https://github.com/dankinsoid/SwiftOpenAP
77

88
## Usage
99

10-
### [Stoplight](https://stoplight.io) setup
11-
1. Describe all of your routes and register all controllers as described in [Vapor docs](https://docs.vapor.codes/basics/routing). Add OpenAPI details to each route using the `route.openAPI` method.
12-
2. Add a route via the `routes.stoplightDocumentation` to return an `OpenAPIObject` instance via the `routes.openAPI` method.
13-
14-
### [Swagger](https://swagger.io) setup
15-
1. Set up a [SwaggerUI page](https://github.com/swagger-api/swagger-ui) in your Vapor project downloading the `dist` folder and placing its content in the `Public/Swagger` directory.
16-
2. Describe all of your routes and register all controllers as described in [Vapor docs](https://docs.vapor.codes/basics/routing). Add OpenAPI details to each route using the `route.openAPI` method.
17-
3. Add a route to return a [SwaggerUI index.html](https://github.com/swagger-api/swagger-ui/blob/master/dist/index.html). Or configure your middlewares to use 'index.html' as default page.
18-
4. Add a route to return an `OpenAPIObject` instance via the `app.routes.openAPI` method. Make sure the path of this route matches the `swagger.json` URL in your SwaggerUI page method.
19-
20-
> [!IMPORTANT]
21-
All enums in your models must implement `CaseIterable`.
22-
23-
### Advanced Usage
24-
VaporToOpenAPI includes several advanced features that allow you to customize the generated OpenAPI documentation in various ways. Some of these features include:
25-
26-
- `openAPI`: This method is used to add OpenAPI metadata to a single Vapor route. It takes a variety of parameters, such as a summary and description of the operation, request and response bodies, query parameters, and more.\
27-
Here's an example of how you might use it to document a POST request:
28-
```swift
29-
routes.post("users") { req -> EventLoopFuture<User> in
30-
let user = try req.content.decode(User.self)
31-
return user.save(on: req.db).map { user }
32-
}
33-
.openAPI(
34-
summary: "Create User",
35-
description: "Create a new user with the provided data",
36-
body: .type(User.self),
37-
response: .type(User.self)
38-
)
39-
```
40-
- `openAPI` (on Routes): This method is used to generate an entire OpenAPI specification from your Vapor routes. It takes a variety of parameters, such as the title and description of your API, the available paths and operations, security requirements, and more.
10+
### Create a route with an `OpenAPI` specification.
11+
Add a route to return an `OpenAPIObject` via the `app.routes.openAPI` method. This method is used to generate an entire OpenAPI specification from all your Vapor routes. It takes a variety of parameters, such as the title and description of your API, the available paths and operations, security requirements, and more.
4112
```swift
4213
// generate OpenAPI documentation
4314
routes.get("Swagger", "swagger.json") { req in
@@ -51,6 +22,32 @@ routes.get("Swagger", "swagger.json") { req in
5122
}
5223
.excludeFromOpenAPI()
5324
```
25+
You also can configure a web page:
26+
- Use `stoplightDocumentation(openAPI:)` helper method to generate a [Stoplight](https://stoplight.io) page. This method also can return an `OpenAPIObject`.
27+
- [Here is an example](##short-example) how to configure SwaggerUI.
28+
29+
### Specify all details for routes
30+
Add `.openAPI` modifier to a route to specify a variety of parameters, such as a summary and description of the operation, request and response bodies, query parameters, headers and more.\
31+
Here's an example of how you might use it to document a `POST` request:
32+
```swift
33+
routes.post("users") { req -> EventLoopFuture<User> in
34+
let user = try req.content.decode(User.self)
35+
return user.save(on: req.db).map { user }
36+
}
37+
.openAPI(
38+
summary: "Create User",
39+
description: "Create a new user with the provided data",
40+
body: .type(User.self),
41+
response: .type(User.self)
42+
)
43+
```
44+
45+
> [!IMPORTANT]
46+
All enums in your models must implement `CaseIterable`.
47+
48+
## Advanced Usage
49+
VaporToOpenAPI includes several advanced features that allow you to customize the generated OpenAPI documentation in various ways. Some of these features include:
50+
5451
- `response`: This method is used to specify an additional response body for a Vapor route. It takes a variety of parameters, such as the status code, description, and response body type.
5552
- `groupedOpenAPI`: These methods are used to group Vapor routes together based on OpenAPI metadata, such as tags or security requirements.\
5653
Here's an example of how you might use it to group routes with the same security requirements:
@@ -113,7 +110,7 @@ struct Color: Codable, OpenAPIType {
113110
}
114111
```
115112

116-
#### Links
113+
#### Link
117114
Links are one of the new features of OpenAPI 3.0. Using links, you can describe how various values returned by one operation can be used as input for other operations.
118115
To create a Link:
119116
1. create `LinkKey` type identifying some reusable parameter.
@@ -126,7 +123,7 @@ enum PetID: LinkKey {
126123
```swift
127124
route.get("pet", use: getPet).openAPI(
128125
links: [
129-
Link(\Pet.id, in: .response): PetID.self
126+
Link("id", in: .response): PetID.self
130127
]
131128
)
132129

@@ -139,8 +136,14 @@ route.post("pet", ":petID", use: renamePer).openAPI(
139136

140137
## [Example project](Example/)
141138
## Short example
142-
### 1. SwaggerUI page
143-
Change `url` in [`swagger-initializer.js`](https://github.com/swagger-api/swagger-ui/blob/master/dist/swagger-initializer.js)
139+
### 1. SwaggerUI page
140+
141+
1. Set up a [SwaggerUI page](https://github.com/swagger-api/swagger-ui) in your Vapor project downloading the `dist` folder and placing its content in the `Public/Swagger` directory.
142+
2. Describe all of your routes and register all controllers as described in [Vapor docs](https://docs.vapor.codes/basics/routing). Add OpenAPI details to each route using the `route.openAPI` method.
143+
3. Add a route to return a [SwaggerUI index.html](https://github.com/swagger-api/swagger-ui/blob/master/dist/index.html). Or configure your middlewares to use 'index.html' as default page.
144+
4. Add a route to return an `OpenAPIObject` instance via the `app.routes.openAPI` method. Make sure the path of this route matches the `swagger.json` URL in your SwaggerUI page method.
145+
146+
5. Change `url` in [`swagger-initializer.js`](https://github.com/swagger-api/swagger-ui/blob/master/dist/swagger-initializer.js)
144147
```js
145148
window.onload = function() {
146149
//<editor-fold desc="Changeable Configuration Block">

0 commit comments

Comments
 (0)