oas_rails 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +106 -6
- data/app/views/layouts/oas_rails/application.html.erb +0 -2
- data/lib/generators/oas_rails/config/templates/oas_rails_initializer.rb +2 -1
- data/lib/oas_rails/configuration.rb +3 -2
- data/lib/oas_rails/operation.rb +1 -1
- data/lib/oas_rails/route_extractor.rb +1 -0
- data/lib/oas_rails/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ef226b2db87f8eedd0553f817ec31387484ddc848130e231b4878565770407a
|
4
|
+
data.tar.gz: b6805acaa691160d12b40362e2365d94dd0e8e9c4a658bba4ee8543ed6ffdc37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91e1747ebe615671cde122bd7c569962a842ad436e7bd27bc1f532ce776fad76356f780e66aeb44ff483b431ec70d696adff18497ef0a6903bb3d7c3704af1d5
|
7
|
+
data.tar.gz: 10313de49b83276af04d3b6085685b377464650e06e1a5166cc65c540968218c47063caec6f033b29bb49c4691385a93f03d9cb6725303a227d171ce497eb784
|
data/README.md
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
+
![Gem Version](https://img.shields.io/gem/v/oas_rails)
|
2
|
+
![GitHub License](https://img.shields.io/github/license/a-chacon/oas_rails)
|
3
|
+
|
1
4
|
# Open API Specification For Rails
|
2
5
|
|
3
6
|
OasRails is a Rails engine for generating **automatic interactive documentation for your Rails APIs**. It generates an **OAS 3.1** document and displays it using **[RapiDoc](https://rapidocweb.com)**.
|
4
7
|
|
8
|
+
![Screenshot](https://raw.githubusercontent.com/a-chacon/oas_rails/0cfc9abb5be85e6bb3fc4669e29372be8f80a276/oas_rails_ui.png)
|
9
|
+
|
5
10
|
## Related Projects
|
6
11
|
|
7
12
|
- **[ApiPie](https://github.com/Apipie/apipie-rails)**: Doesn't support OAS 3.1, requires learning a DSL, lacks a nice UI
|
@@ -64,7 +69,8 @@ Then complete the created file with your data.
|
|
64
69
|
|
65
70
|
Almost every description in an OAS file supports simple markdown. The following tags are available for documenting your endpoints:
|
66
71
|
|
67
|
-
|
72
|
+
<details>
|
73
|
+
<summary style="font-weight: bold; font-size: 1.2em;">@summary</summary>
|
68
74
|
|
69
75
|
**Structure**: `@summary text`
|
70
76
|
|
@@ -73,7 +79,10 @@ Used to add a summary to the endpoint. It replaces the default summary/title of
|
|
73
79
|
**Example**:
|
74
80
|
`# @summary This endpoint creates a User`
|
75
81
|
|
76
|
-
|
82
|
+
</details>
|
83
|
+
|
84
|
+
<details>
|
85
|
+
<summary style="font-weight: bold; font-size: 1.2em;">@parameter</summary>
|
77
86
|
|
78
87
|
**Structure**: `@parameter name(position) [type] text`
|
79
88
|
|
@@ -83,7 +92,10 @@ Represents a parameter for the endpoint. The position can be: `header`, `path`,
|
|
83
92
|
`# @parameter page(query) [Integer] The page number.`
|
84
93
|
`# @parameter slug(path) [String!] Slug of the Project.`
|
85
94
|
|
86
|
-
|
95
|
+
</details>
|
96
|
+
|
97
|
+
<details>
|
98
|
+
<summary style="font-weight: bold; font-size: 1.2em;">@request_body</summary>
|
87
99
|
|
88
100
|
**Structure**: `@request_body text [type] structure`
|
89
101
|
|
@@ -92,7 +104,10 @@ Documents the request body needed by the endpoint. The structure is optional if
|
|
92
104
|
**Example**:
|
93
105
|
`# @request_body The user to be created [Hash] {user: {name: String, age: Integer, password: String}}`
|
94
106
|
|
95
|
-
|
107
|
+
</details>
|
108
|
+
|
109
|
+
<details>
|
110
|
+
<summary style="font-weight: bold; font-size: 1.2em;">@request_body_example</summary>
|
96
111
|
|
97
112
|
**Structure**: `@request_body_example text [type] structure`
|
98
113
|
|
@@ -101,7 +116,10 @@ Adds examples to the provided request body.
|
|
101
116
|
**Example**:
|
102
117
|
`# @request_body_example A complete User. [Hash] {user: {name: 'Luis', age: 30, password: 'MyWeakPassword123'}}`
|
103
118
|
|
104
|
-
|
119
|
+
</details>
|
120
|
+
|
121
|
+
<details>
|
122
|
+
<summary style="font-weight: bold; font-size: 1.2em;">@response</summary>
|
105
123
|
|
106
124
|
**Structure**: `@response text(code) [type] structure`
|
107
125
|
|
@@ -110,7 +128,10 @@ Documents the responses of the endpoint and overrides the default responses foun
|
|
110
128
|
**Example**:
|
111
129
|
`# @response User not found by the provided Id(404) [Hash] {success: Boolean, message: String}`
|
112
130
|
|
113
|
-
|
131
|
+
</details>
|
132
|
+
|
133
|
+
<details>
|
134
|
+
<summary style="font-weight: bold; font-size: 1.2em;">@tag</summary>
|
114
135
|
|
115
136
|
**Structure**: `@tag text`
|
116
137
|
|
@@ -119,8 +140,87 @@ Tags your endpoints. You can complete the tag documentation in the initializer f
|
|
119
140
|
**Example**:
|
120
141
|
`# @tag Users`
|
121
142
|
|
143
|
+
</details>
|
144
|
+
|
122
145
|
You can use these tags in your controller methods to enhance the automatically generated documentation. Remember to use markdown formatting in your descriptions for better readability in the generated OAS document.
|
123
146
|
|
147
|
+
### Example of documented endpoints
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
class UsersController < ApplicationController
|
151
|
+
before_action :set_user, only: %i[show update destroy]
|
152
|
+
|
153
|
+
# @summary Returns a list of Users.
|
154
|
+
#
|
155
|
+
# @parameter offset(query) [Integer] Used for pagination of response data (default: 25 items per response). Specifies the offset of the next block of data to receive.
|
156
|
+
# @parameter status(query) [Array<String>] Filter by status. (e.g. status[]=inactive&status[]=deleted).
|
157
|
+
# @parameter X-front(header) [String] Header for identify the front.
|
158
|
+
def index
|
159
|
+
@users = User.all
|
160
|
+
end
|
161
|
+
|
162
|
+
# @summary Get a user by id.
|
163
|
+
#
|
164
|
+
# This method show a User by ID. The id must exist of other way it will be returning a **`404`**.
|
165
|
+
#
|
166
|
+
# @parameter id(path) [Integer] Used for identify the user.
|
167
|
+
# @response Requested User(200) [Hash] {user: {name: String, email: String, created_at: DateTime }}
|
168
|
+
# @response User not found by the provided Id(404) [Hash] {success: Boolean, message: String}
|
169
|
+
# @response You don't have the right permission for access to this resource(403) [Hash] {success: Boolean, message: String}
|
170
|
+
def show
|
171
|
+
render json: @user
|
172
|
+
end
|
173
|
+
|
174
|
+
# @summary Create a User
|
175
|
+
#
|
176
|
+
# @request_body The user to be created. At least include an `email`. [User!]
|
177
|
+
# @request_body_example basic user [Hash] {user: {name: "Luis", email: "luis@gmail.ocom"}}
|
178
|
+
def create
|
179
|
+
@user = User.new(user_params)
|
180
|
+
|
181
|
+
if @user.save
|
182
|
+
render json: @user, status: :created
|
183
|
+
else
|
184
|
+
render json: { success: false, errors: @user.errors }, status: :unprocessable_entity
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
# A `user` can be updated with this method
|
189
|
+
# - There is no option
|
190
|
+
# - It must work
|
191
|
+
# @tags users, update
|
192
|
+
# @request_body User to be created [Hash] {user: { name: String, email: String, age: Integer}}
|
193
|
+
# @request_body_example Update user [Hash] {user: {name: "Luis", email: "luis@gmail.com"}}
|
194
|
+
# @request_body_example Complete User [Hash] {user: {name: "Luis", email: "luis@gmail.com", age: 21}}
|
195
|
+
def update
|
196
|
+
if @user.update(user_params)
|
197
|
+
render json: @user
|
198
|
+
else
|
199
|
+
render json: @user.errors, status: :unprocessable_entity
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
# @summary Delete a User
|
204
|
+
# Delete a user and his associated data.
|
205
|
+
def destroy
|
206
|
+
@user.destroy!
|
207
|
+
redirect_to users_url, notice: 'User was successfully destroyed.', status: :see_other
|
208
|
+
end
|
209
|
+
|
210
|
+
private
|
211
|
+
|
212
|
+
# Use callbacks to share common setup or constraints between actions.
|
213
|
+
def set_user
|
214
|
+
@user = User.find(params[:id])
|
215
|
+
end
|
216
|
+
|
217
|
+
# Only allow a list of trusted parameters through.
|
218
|
+
def user_params
|
219
|
+
params.require(:user).permit(:name, :email)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
```
|
223
|
+
|
124
224
|
## Contributing
|
125
225
|
|
126
226
|
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
|
@@ -44,6 +44,7 @@ OasRails.configure do |config|
|
|
44
44
|
config.tags = [{ name: "Users", description: "Manage the `amazing` Users table." }]
|
45
45
|
|
46
46
|
# config.default_tags_from = :namespace # Could be: :namespace or :controller
|
47
|
-
# config.
|
47
|
+
# config.autodiscover_request_body = true # Try to get request body for create and update methods based on the controller name.
|
48
48
|
# config.autodiscover_responses = true # Looks for renders in your source code and try to generate the responses.
|
49
|
+
# config.api_path = "/" # set this config if your api is in a different namespace other than /
|
49
50
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module OasRails
|
2
2
|
class Configuration
|
3
|
-
attr_accessor :info, :default_tags_from, :
|
3
|
+
attr_accessor :info, :default_tags_from, :autodiscover_request_body, :autodiscover_responses, :api_path
|
4
4
|
attr_reader :servers, :tags
|
5
5
|
|
6
6
|
def initialize(**kwargs)
|
@@ -9,8 +9,9 @@ module OasRails
|
|
9
9
|
@tags = []
|
10
10
|
@swagger_version = '3.1.0'
|
11
11
|
@default_tags_from = "namespace"
|
12
|
-
@
|
12
|
+
@autodiscover_request_body = true
|
13
13
|
@autodiscover_responses = true
|
14
|
+
@api_path = "/"
|
14
15
|
end
|
15
16
|
|
16
17
|
def default_servers
|
data/lib/oas_rails/operation.rb
CHANGED
@@ -89,7 +89,7 @@ module OasRails
|
|
89
89
|
|
90
90
|
def extract_request_body(oas_route:)
|
91
91
|
tag_request_body = oas_route.docstring.tags(:request_body).first
|
92
|
-
if tag_request_body.nil? && OasRails.config.
|
92
|
+
if tag_request_body.nil? && OasRails.config.autodiscover_request_body
|
93
93
|
oas_route.detect_request_body if %w[create update].include? oas_route.method
|
94
94
|
elsif !tag_request_body.nil?
|
95
95
|
RequestBody.from_tags(tag: tag_request_body, examples_tags: oas_route.docstring.tags(:request_body_example))
|
@@ -79,6 +79,7 @@ module OasRails
|
|
79
79
|
return false if route.defaults[:controller].nil?
|
80
80
|
return false if RAILS_DEFAULT_CONTROLLERS.any? { |default| route.defaults[:controller].start_with?(default) }
|
81
81
|
return false if RAILS_DEFAULT_PATHS.any? { |path| route.path.spec.to_s.include?(path) }
|
82
|
+
return false unless route.path.spec.to_s.start_with?(OasRails.config.api_path)
|
82
83
|
|
83
84
|
true
|
84
85
|
end
|
data/lib/oas_rails/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oas_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- a-chacon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-07-
|
11
|
+
date: 2024-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: esquema
|